-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup
executable file
·93 lines (76 loc) · 1.96 KB
/
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
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
#!/bin/bash
#Output colors
NORMAL="\033[0;39m"
RED="\033[1;31m"
BLUE="\033[1;34m"
function log() {
printf "$BLUE ${BORG_ARCHIVE} > $1$NORMAL\n" $2
}
function error() {
printf "\n$RED ${BORG_ARCHIVE} >>> ERROR - $1$NORMAL\n" $2
}
function volume_snap() {
VOLUME_SNAP=$(docker volume create -d lvm --opt snapshot=$1 --opt --thinpool=${LVM_THIN} $1_snap)
log "Volume snapshot '${VOLUME_SNAP}' for ${VOLUME} created."
}
function volume_unsnap() {
VOLUME_SNAP=$(docker volume remove $1_snap)
log "Volume snapshot '${VOLUME_SNAP}' removed."
}
function volumes_backup() {
log "Backup Volumes: '${LVM_VOLUMES[*]}'"
for VOLUME in ${LVM_VOLUMES[@]}
do
BACKUP_VOLUMES="${BACKUP_VOLUMES} --mount src=${VOLUME}_snap,target=/borg/data/${BORG_ARCHIVE}/${VOLUME/${BORG_ARCHIVE}_/},volume-opt=nouuid,ro"
done
if [ -n "${BORG_PRUNE}" ]; then
BORG_PRUNE="-e BORG_PRUNE=1"
else
BORG_PRUNE=""
fi
docker run --rm \
${BORG_PRUNE} \
-e BORG_REPO="ssh://${SSH_USER}@${SSH_HOST}:23/./${SSH_PATH}" \
-e BORG_PASSCOMMAND="cat /borg/.borg-passphrase" \
-e ARCHIVE="${BORG_ARCHIVE}-{now:%Y-%m-%d}" \
-e PRUNE_PREFIX="${BORG_ARCHIVE}-" \
${BACKUP_VOLUMES} \
-v ${PASSPHRASE}:/borg/.borg-passphrase:ro \
-v ${BORG_CACHE}:/borg/cache \
-v ${BORG_CONFIG}:/borg/config \
-v ${BORG_KEYS}:/borg/config/keys \
borg-backup
if [ "$?" -eq 0 ]; then
return 0
else
return 1
fi
}
function backup() {
for VOLUME in ${LVM_VOLUMES[@]}
do
volume_snap ${VOLUME}
done
volumes_backup
DOCKER_RETURN=$?
for VOLUME in ${LVM_VOLUMES[@]}
do
volume_unsnap ${VOLUME}
done
return ${DOCKER_RETURN}
}
function arguments() {
while getopts ":v:volume:a:archive:c:check:" opt;
do
case "${opt}"
in
v|volume) LVM_VOLUMES=( "${LVM_VOLUMES[@]}" "${OPTARG}" ); ;;
a|archive) BORG_ARCHIVE=${OPTARG};;
c|check) BORG_CHECK=true;;
p|prune) BORG_PRUNE=true;;
esac
done
}
arguments "$@"
backup
exit