-
Notifications
You must be signed in to change notification settings - Fork 14
/
docker_volume_backup.sh
executable file
·53 lines (42 loc) · 1.26 KB
/
docker_volume_backup.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
#!/bin/bash
compose_file_path=$1
project_name=${2,,}
backup_path=$3
backup_or_restore=$4
restore_date=$5
set -e
function backup_volume {
volume_name=$1
backup_destination=$2
date_suffix=$(date -I)
docker run --rm -v $volume_name:/data -v $backup_destination:/backup ubuntu tar -zcvf /backup/$volume_name-$date_suffix.tar /data
}
function restore_volume {
volume_name=$1
backup_destination=$2
date=$3
docker run --rm -v $volume_name:/data ubuntu find /data -mindepth 1 -delete
docker run --rm -v $volume_name:/data -v $backup_destination:/backup ubuntu tar -xvf /backup/$volume_name-$date.tar -C .
}
function main {
echo "Stopping running containers"
docker-compose -f $compose_file_path -p $project_name stop
echo "Mounting volumes and performing backup/restore..."
volumes=($(docker volume ls -f name=$project_name | awk '{if (NR > 1) print $2}'))
for v in "${volumes[@]}"
do
if [ "$backup_or_restore" == "backup" ]
then
echo "Perform backup"
backup_volume $v $backup_path
fi
if [ "$backup_or_restore" == "restore" ]
then
echo "Restore from backup"
restore_volume $v $backup_path $restore_date
fi
done
echo "Restarting containers"
docker-compose -f $compose_file_path -p $project_name start
}
main