summaryrefslogtreecommitdiff
path: root/docker_tools/docker_backup_vol.sh
blob: 85bfb853cfc5506beab752b8306b8f92cb8feae4 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash

# Please read the README.md distributed along this script

#set -x

ALL_VOLS=`docker volume ls --format '{{.Name}}'`
TEMP_VOLS=
SKIP_VOLS=
VOLS=

if [ "$#" -eq 0 ]; then

    echo ""
    echo "-----------------------------------------"
    echo "Usage: $0"
    echo "--single-vol  [VOLUME]  You can explicitly include a volume (can occur multiple times)"
    echo "--exclude-vol [VOLUME]  You can explicitly exclude a volume (can occur multiple times)"
    echo "--all-vol               This will backup all volumes (as reported by 'docker volume ls')"
    echo "-----------------------------------------"
    echo ""

    exit
fi

while [[ $# -gt 0 ]]
do
key="$1"
case $key in
	--all-vol)
		TEMP_VOLS+=$ALL_VOLS
		shift
	;;
	--single-vol)
		TEMP_VOLS+=" $2"
		shift
		shift
	;;
	--exclude-vol)
		SKIP_VOLS+=" $2"
		shift
		shift
	;;
esac
done

#TODO: fix this inefficient shit!
for v in $TEMP_VOLS; do

	SKIP="0"

	for t in $VOLS; do

		#echo "test $v agains $t"
		if [ "$t" == $v ]; then
		#	echo "skip"
			SKIP=1
		fi
	done

	for t in $SKIP_VOLS; do

		#echo "test $v agains $t"
		if [ "$t" == $v ]; then
		#	echo "skip"
			SKIP=1
		fi
	done

	if [ "$SKIP" == "0" ]; then
		# echo "add $v"
		VOLS+=" $v"
	fi

done

for v in $VOLS; do

	TIMESTAMP=`date +"%Y%m%d_%H%M%S"`
	FILENAME=${TIMESTAMP}_volume_$v.tar.gz

	echo " +--> backing up volume: $v to file: $FILENAME"
	docker run --rm -it  -v $v:/IN:ro -v `pwd`:/OUT bash tar -C /IN -czf /OUT/$FILENAME .

done