-
Notifications
You must be signed in to change notification settings - Fork 72
/
backup
executable file
·113 lines (93 loc) · 2.48 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
set -eo pipefail
function run_commands {
COMMANDS=$1
while IFS= read -r cmd; do echo "$cmd" && eval "$cmd" ; done < <(printf '%s\n' "$COMMANDS")
}
function run_exit_commands {
set +e
set +o pipefail
run_commands "${POST_COMMANDS_EXIT:-}"
}
function replace_spaces {
echo "${1//\\ /\\}"
}
function replace_spaces_back {
echo "${1//\\/ }"
}
function check_bool {
local var="$1"
if [ -n "$var" ]; then
if [ "$var" = "true" ] || [ "$var" = "1" ]; then
echo "true"
return
fi
fi
echo "false"
}
trap run_exit_commands EXIT
run_commands "${PRE_COMMANDS:-}"
IFS=" " read -r -a RESTIC_BACKUP_SOURCES <<< "$(replace_spaces "${RESTIC_BACKUP_SOURCES:-/data}")"
RESTIC_BACKUP_TAGS="${RESTIC_BACKUP_TAGS:-}"
IFS=" " read -r -a RESTIC_BACKUP_ARGS <<< "$(replace_spaces "$RESTIC_BACKUP_ARGS")"
tags="$(echo "$RESTIC_BACKUP_TAGS" | tr "," "\n")"
tag_options=()
for tag in $tags; do
tag_options=("${tag_options[@]}" --tag "$tag")
done
backup_sources=()
for source in "${RESTIC_BACKUP_SOURCES[@]}"; do
source="$(replace_spaces_back "$source")"
backup_sources=("${backup_sources[@]}" "$source")
done
backup_args=()
for arg in "${RESTIC_BACKUP_ARGS[@]}"; do
arg="$(replace_spaces_back "$arg")"
backup_args=("${backup_args[@]}" "$arg")
done
start=$(date +%s)
echo Starting Backup at $(date +"%Y-%m-%d %H:%M:%S")
set +e
restic --repo="${RESTIC_REPOSITORY}" backup "${backup_args[@]}" "${tag_options[@]}" "${backup_sources[@]}"
rc=$?
set -e
result="success"
if [ $rc -ne 0 ]; then
if [ $rc -eq 3 ]; then
if [ "$(check_bool "${SUCCESS_ON_INCOMPLETE_BACKUP:-}")" = "true" ]; then
result="success"
else
result="incomplete"
fi
else
result="failure"
fi
fi
case "$result" in
success)
echo Backup successful;;
incomplete)
echo Backup incomplete;;
failure)
echo Backup failed;;
esac
if [ -n "${RESTIC_FORGET_ARGS}" ]; then
IFS=" " read -r -a RESTIC_FORGET_ARGS <<< "$RESTIC_FORGET_ARGS"
echo Forget about old snapshots based on RESTIC_FORGET_ARGS = "${RESTIC_FORGET_ARGS[@]}"
restic forget "${tag_options[@]}" "${RESTIC_FORGET_ARGS[@]}"
fi
end=$(date +%s)
echo Finished backup at $(date +"%Y-%m-%d %H:%M:%S") after $((end-start)) seconds
case "$result" in
success)
run_commands "${POST_COMMANDS_SUCCESS:-}";;
incomplete)
if [ -n "${POST_COMMANDS_INCOMPLETE:-}" ]; then
run_commands "${POST_COMMANDS_INCOMPLETE:-}"
else
run_commands "${POST_COMMANDS_FAILURE:-}"
fi;;
failure)
run_commands "${POST_COMMANDS_FAILURE:-}";;
esac
exit $rc