-
Notifications
You must be signed in to change notification settings - Fork 1
/
dockercache
executable file
·71 lines (60 loc) · 1.64 KB
/
dockercache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env bash
function cache_images() {
images_to_cache=$( docker images --format='{{.Repository}}:{{.Tag}},{{.ID}},{{.Size}}' | grep -v '<none>' | sort | uniq )
echo "Caching the following images"
echo "$images_to_cache"
if [ -n "$images_to_cache" ]; then
rm -rf $cached_images_path
mkdir $cached_images_path
i=0
for image_line in ${images_to_cache[@]}; do
i=$(expr $i + 1)
image_filename=$( echo "image_$i" )
image_name=$( echo "$image_line" | cut -d , -f 1 )
printf "Saving image $( echo ${image_name} ) to $( echo $cached_images_path/$image_filename.tar )\n"
docker save -o $cached_images_path/$image_filename.tar $(echo ${image_name})
done
else
echo "No images found."
fi
}
function restore_images() {
if [ -d $cached_images_path ] && [ -e $cached_image_metadata_path ]; then
echo "Restoring images ..."
cached_images=$(ls $cached_images_path);
for image_archive in ${cached_images[@]}; do
docker load -i $cached_images_path/$image_archive
done
echo "Images restored."
else
echo "No image backup found from $cached_images_path"
exit 1
fi
}
function docker-cache() {
echo "Running $CMD for $DIR"
usage="Usage: cat docker-cache | CMD=restore DIR=mydir bash"
if [ -z $CMD ]; then
echo "$usage"
exit 1
fi
if [ -z $DIR ]; then
echo "$usage"
exit 1
fi
destination=$DIR
cached_images_path="$destination/docker-images"
case "$CMD" in
"snapshot" )
cache_images
;;
"restore" )
restore_images
;;
* )
echo "'$CMD' action unknown"
echo "$usage"
;;
esac
}
docker-cache "$@"