-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup-volume-docker.sh
executable file
·178 lines (151 loc) · 4.93 KB
/
backup-volume-docker.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env bash
: '
Script created by Valentin DEVILLE (contact[@]valentin-deville.eu)
Available on Github: https://github.com/vdeville/docker-volume-backup
Thanks to loomchild for his tools https://github.com/loomchild/volume-backup
'
BACKUPS_DIR="/home/valentin/docker/backup/"
BACKUP_FLAG="backup_flag"
BACKUP_VOLUMES="volume1 volume2"
debug_log() {
echo "[$(date -u +%FT%T%z)] ${1}"
}
backup_volume() {
volume=$1
TEMP_CONTAINERS=()
# Check if the volume exist
exist=$(docker volume ls -f name=$volume -q)
if [[ -z $exist ]]; then
return;
fi
# Stop all containers that use the volume to be save
for container in $(docker ps --filter=volume=$volume -q); do
TEMP_CONTAINERS+=($container)
docker stop $container
debug_log "Container ${container} stopped"
done
# Make Backup
docker run -v $volume:/volume --rm loomchild/volume-backup backup - > $BACKUPS_DIR$volume-$(date '+%d-%h-%Y-%H%M%S').tar.bz2
debug_log "Volume ${volume} backuped"
# Restart before stopped containers
for container in ${TEMP_CONTAINERS[@]}; do
docker start $container
debug_log "Container ${container} started"
done
}
restore_volume() {
volume=$1
archive=$2
TEMP_CONTAINERS=()
# Stop all containers that use the volume to be save
for container in $(docker ps --filter=volume=$volume -q); do
TEMP_CONTAINERS+=($container)
docker stop $container
debug_log "Container ${container} stopped"
done
# Make Restore
cat $archive | docker run -i -v $volume:/volume --rm loomchild/volume-backup restore -f -
debug_log "Volume ${volume} restored"
# Restart before stopped containers
for container in ${TEMP_CONTAINERS[@]}; do
docker start $container
debug_log "Container ${container} started"
done
}
# Check if help is call
while getopts ":h" opt; do
case ${opt} in
h)
echo "Usage:"
echo " backup -v <volume_name> Backup single volume, 'CONFIG' to backup all volumes specified in the config or 'ALL' to backup all volumes with the backup label"
echo " restore -v <volume_name> <backup_file> Restore specific volume with backup file"
echo " list-backups [[-v] <volume_name>] List backups for a volume or by default for all volumes"
exit 0
;;
esac
done
shift $((OPTIND -1))
action=$1; shift # Backup, restore, list
case $action in
backup)
volume="ALL"
while getopts ":v:" opt; do
case ${opt} in
v)
volume=$OPTARG
;;
:)
echo "Invalid option: -$OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
if [[ $volume == "CONFIG" ]]; then
debug_log "Backuping volumes: ${BACKUP_VOLUMES}"
for volume in $BACKUP_VOLUMES; do
debug_log "Backuping ${volume}..."
backup_volume $volume
debug_log "Backup finished for ${volume}..."
done
elif [[ $volume == "ALL" ]]; then
# Get the list of volume to be backup
debug_log "Finding all volumes to be backup.."
for volume in $(docker volume ls -f=label=$BACKUP_FLAG=True -q); do
debug_log "Backuping ${volume}..."
backup_volume $volume
debug_log "Backup finished for ${volume}..."
done
else
debug_log "Backuping ${volume}..."
backup_volume $volume
debug_log "Backup finished for ${volume}..."
fi ;;
restore)
# Check if volume is supplied
if [[ $1 != -v* ]]; then echo "No volume option specified (-v), please use -h to see the help" && exit 1; fi
if [[ -z $3 ]]; then echo "No archive specified, please use -h to see the help" && exit 1; fi
while getopts ":v:" opt; do
case ${opt} in
v)
volume=$OPTARG
;;
:)
echo "Invalid option: -$OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
archive=$3
if [[ -f "$archive" ]]; then
debug_log "Restoring volume ${volume}.."
restore_volume $volume $archive
debug_log "Done."
elif [[ ! -f "$BACKUPS_DIR/$archive" ]]; then
debug_log "$archive not exist in current directory, but exist in ${BACKUPS_DIR}"
archive=$BACKUPS_DIR/$archive
debug_log "Restoring volume ${volume}.."
restore_volume $volume $archive
debug_log "Done."
else
debug_log "$archive not exist in ${BACKUPS_DIR}.. exiting."
exit 1
fi ;;
list-backups)
volume="ALL"
while getopts ":v:" opt; do
case ${opt} in
v)
volume=$OPTARG
;;
esac
done
if [[ $volume == "ALL" ]]; then
ls -lrt $BACKUPS_DIR | awk '{ print $NF }'
else
ls -lrt $BACKUPS_DIR$volume* | awk '{ print $NF }'
fi ;;
*)
echo "No valid action, add -h argument to view the help" 1>&2
exit 1
;;
esac