-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-volume-snapshot.sh
executable file
·42 lines (39 loc) · 1.57 KB
/
docker-volume-snapshot.sh
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
#!/bin/bash
set -e -o pipefail
programname=$(basename "$0")
display_usage() {
echo "usage: $programname (create|restore) source destination"
echo " create create snapshot file from docker volume"
echo " restore restore snapshot file to docker volume"
echo " source source path"
echo " destination destination path"
echo
echo "Tip: Supports tar's compression algorithms automatically"
echo " based on the file extension, for example .tar.gz"
echo
echo "Examples:"
echo "docker-volume-snapshot create xyz_volume xyz_volume.tar"
echo "docker-volume-snapshot create xyz_volume xyz_volume.tar.gz"
echo "docker-volume-snapshot restore xyz_volume.tar xyz_volume"
echo "docker-volume-snapshot restore xyz_volume.tar.gz xyz_volume"
}
case "$1" in
"create")
if [[ -z "$2" || -z "$3" ]]; then display_usage; exit 1; fi
directory=$(dirname "$3")
if [ "$directory" == "." ]; then directory=$(pwd); fi
filename=$(basename "$3")
docker run --rm -v "$2:/source" -v "$directory:/dest" busybox tar cvaf "/dest/$filename" -C /source .
;;
"restore")
if [[ -z "$2" || -z "$3" ]]; then display_usage; exit 1; fi
directory=$(dirname "$2")
if [ "$directory" == "." ]; then directory=$(pwd); fi
filename=$(basename "$2")
docker run --rm -v "$3:/dest" -v "$directory:/source" busybox tar xvf "/source/$filename" -C /dest
;;
*)
display_usage
exit 1 # Command to come out of the program with status 1
;;
esac