-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathbackup
More file actions
executable file
·137 lines (113 loc) · 3.05 KB
/
backup
File metadata and controls
executable file
·137 lines (113 loc) · 3.05 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/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"
}
# Check if another backup is running
if [ -f /run/lock/backup.lock ]; then
echo Backup already running, skipping backup at $(date +"%Y-%m-%d %H:%M:%S")
run_commands "${SKIP_COMMANDS:-}"
exit 1
fi
touch /run/lock/backup.lock
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
repo_arg="--repo=${RESTIC_REPOSITORY}"
if [ -n "${RESTIC_REPOSITORY_FILE:-}" ]; then
echo Using restic repository file at $RESTIC_REPOSITORY_FILE
repo_arg="--repository-file=${RESTIC_REPOSITORY_FILE}"
unset RESTIC_REPOSITORY
fi
start=$(date +%s)
echo Starting Backup at $(date +"%Y-%m-%d %H:%M:%S")
set +e
restic "${repo_arg}" 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[@]}"
set +e
restic forget "${tag_options[@]}" "${RESTIC_FORGET_ARGS[@]}"
forget_rc=$?
set -e
if [ $forget_rc -ne 0 ]; then
echo "Forget failed"
fi
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
rm -f /run/lock/backup.lock
exit $rc