-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup-volumes.sh
executable file
·69 lines (58 loc) · 1.97 KB
/
backup-volumes.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
#!/usr/bin/env bash
included_volumes="tools_vaultwarden-data streaming_jellyfin-data smarthome_zigbee2mqtt-data security_crowdsec-data"
function exists_in_list() {
LIST=$1
DELIMITER=$2
VALUE=$3
[[ "$LIST" =~ ($DELIMITER|^)$VALUE($DELIMITER|$) ]]
}
function mount_volumes {
volumes=$(docker volume ls --format '{{.Name}}')
for volume in $volumes
do
if !(exists_in_list "$included_volumes" " " $volume); then
continue
fi
# determine source and target folders for volume
printf "[%s] backing up volume $volume\n" "$(date)"
source=$(docker volume inspect --format '{{.Mountpoint}}' "$volume")
mount_path=/mnt/docker-volume-backups/$volume
sudo mkdir -p $mount_path
sudo mount --bind -o ro $source $mount_path
# pause associated containers to prevent data corruption during backup
containers=$(docker ps -a -f volume=$volume --format '{{.ID}}')
for cont in $containers
do
docker pause $cont
done
done
}
function unmount_volumes {
volumes=$(docker volume ls --format '{{.Name}}')
for volume in $volumes
do
if !(exists_in_list "$included_volumes" " " $volume); then
continue
fi
# determine source and mount folders for volume
source=$(docker volume inspect --format '{{.Mountpoint}}' "$volume")
mount_path=/mnt/docker-volume-backups/$volume
sudo umount $mount_path
sudo rm $mount_path -rf
# resume associated containers
containers=$(docker ps -a -f volume=$volume --format '{{.ID}}')
for cont in $containers
do
docker unpause $cont
done
done
}
if [ "$#" -ne 1 ]; then
echo "Please tell me to either mount or unmount."
exit
fi
case $1 in
"mount") mount_volumes ;;
"unmount") unmount_volumes ;;
*) echo "Please tell me to either mount or unmount." ;;
esac