-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_multiple_docker_volumes.sh
103 lines (90 loc) · 2.49 KB
/
backup_multiple_docker_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
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
#!/bin/bash
# exit when any command fails(disabled for now)
# set -e
# Usage:
# -o is output dir so is mandatory
# -v is folowed by the volume names comma seperated without spaces
# -p is followed by the prefix of volumes you want to backup
# eg1. ./backup_budibase_volumes.sh -v 'mysql_data,redis_data' -o '/tmp/backup'
# eg2. ./backup_budibase_volumes.sh -p 'budibase' -o '/tmp/backup'
# Read command options and their values
while [ -n "$1" ]
do
case "$1" in
-v)
VOLUMES="$2"
echo "Found the -v option, with parameter value $VOLUMES"
if [ -z "$VOLUMES" ];
then
echo "\$VOLUMES is empty"
exit 1;
elif [[ $VOLUMES == '-p' || $VOLUMES == '-o' ]];
then
echo "\$VOLUMES is empty"
exit 1;
fi
shift ;;
-p)
VOLUME_PREFIX="$2"
echo "Found the -p option, with parameter value $VOLUME_PREFIX"
if [ -z "$VOLUME_PREFIX" ];
then
echo "\$VOLUME_PREFIX is empty"
exit 1;
elif [[ $VOLUME_PREFIX == '-v' || $VOLUME_PREFIX == '-o' ]];
then
echo "\$VOLUME_PREFIX is empty"
exit 1;
fi
shift ;;
-o)
OUTPUT_DIR="$2"
echo "Found the -o option, with parameter value $OUTPUT_DIR"
if [ -z "$OUTPUT_DIR" ]
then
echo "\$OUTPUT_DIR is empty"
exit 1;
elif [[ $OUTPUT_DIR == '-v' || $OUTPUT_DIR == '-p' ]];
then
echo "\$OUTPUT_DIR is empty"
exit 1;
fi
shift ;;
*) echo "$1 is not an option"
exit 1;;
esac
shift
done
function backup_volumes_from_comma_sep_string_list() {
echo "Backup user specified docker volumes"
IFS=', ' read -r -a dockervolumesarray <<< "$VOLUMES"
for vol in "${dockervolumesarray[@]}"
do
echo "Backing up docker volume: ${vol} at: ${BACKUP_DIR}";
./backup_docker_volume.sh ${vol} ${BACKUP_DIR};
done
}
function backup_all_volumes_with_prefix() {
echo "Backup all docker volumes with user specified prefix ${VOLUME_PREFIX}"
docker volume ls --format '{{.Name}}'| grep ^${VOLUME_PREFIX} | while read -r vol ; do
echo "Backing up docker volume: ${vol} at: ${BACKUP_DIR}";
./backup_docker_volume.sh ${vol} ${BACKUP_DIR};
done
}
function create_volume_backup_directory() {
echo "Creating backup directory"
if [ ! -d "${OUTPUT_DIR}" ] ; then
echo "> Error: output directory doesn't exist at '${OUTPUT_DIR}'"
exit 1
fi
echo "Creating backup path $BACKUP_DIR"
mkdir -p ${BACKUP_DIR}
}
DATE=$(date +%Y%m%d)
DATETIME=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="$OUTPUT_DIR/$DATE/$DATETIME-docker_volumes_backup"
./stop_budi.sh
create_volume_backup_directory
backup_volumes_from_comma_sep_string_list
backup_all_volumes_with_prefix
./start_budi_with__minus_d_option.sh