-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-backup
executable file
·53 lines (46 loc) · 969 Bytes
/
docker-backup
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
#!/usr/bin/env bash
set -e
BASE_DIR=`pwd`
BASE_NAME=${BASE_DIR##*/}
PREFIX=${BASE_NAME//_/}
function backup_one {
docker run -v ${PREFIX}_$1:/data:ro -v `pwd`/_backup:/backup --rm busybox \
/bin/sh -c "cd /data && tar -czf /backup/$1.tar.gz *"
}
function restore_one {
docker run -v ${PREFIX}_$1:/data -v `pwd`/_backup:/backup --rm busybox \
/bin/sh -c "cd /data rm -rf * && tar xfz /backup/$1.tar.gz"
}
function backup {
for volume in `docker volume ls -q -f name=${PREFIX}_db_*`; do
local name=${volume//${PREFIX}_/}
echo "backing up ${name}"
backup_one ${name}
done
}
function restore {
for file in _backup/*.tar.gz ; do
local volume=${file##*/}
name=${volume%%.*}
echo "restoring ${name}"
restore_one ${name}
done
}
#command switch
case "$1" in
backup)
shift
if [ "$#" -ne 1 ]; then
backup
else
backup_one $2
fi
;;
restore)
shift
if [ "$#" -ne 1 ]; then
restore
else
restore_one $2
fi;;
esac