-
Notifications
You must be signed in to change notification settings - Fork 0
/
restic-dataset-backup.sh
executable file
·328 lines (295 loc) · 7.61 KB
/
restic-dataset-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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/bin/bash
# Creates backups of ZFS datasets with restic
# Uses healthchecks.io for signaling
###############################################################################
# Define your variables here
###############################################################################
# Please use the config file!
###############################################################################
# End of user defined variables
###############################################################################
# Version
VERSION="v0.12"
# Path
PATH="/usr/sbin:/usr/bin:/sbin:/bin"
# /usr/local/bin
if [ -d /usr/local/bin ]; then PATH="/usr/local/bin:$PATH"; fi
# /usr/local/sbin
if [ -d /usr/local/sbin ]; then PATH="/usr/local/sbin:$PATH"; fi
# OmniOS Community Edition
if [ -d /opt/ooce/bin ]; then PATH="/opt/ooce/bin:$PATH"; fi
# pkgsrc (https://pkgsrc.smartos.org/)
if [ -d /opt/local/bin ]; then PATH="/opt/local/bin:$PATH"; fi
export PATH
# echo to stderr
echoerr() { printf "%s\n" "$*" >&2; }
fnUsage() {
echoerr "USAGE: $0 [-c <config-file>]" 1>&2;
echoerr " -c <config-file>: path to config file" 1>&2;
echoerr " -i run 'restic init' on repo" 1>&2;
echoerr " -u run 'restic unlock' on repo" 1>&2;
exit 1;
}
# command line arguments
# https://wiki.bash-hackers.org/howto/getopts_tutorial
#echo $*
CONFIGFILE=""
INIT=""
UNLOCK=""
while getopts "hiuc:" OPT; do
case $OPT in
c)
CONFIGFILE="$OPTARG"
;;
h)
fnUsage
;;
i)
INIT="YES"
;;
u)
UNLOCK="YES"
;;
*)
fnUsage
;;
esac
done
# mandatory arguments
if [ ! "$CONFIGFILE" ]; then
fnUsage;
fi
# Absolute path to this script, e.g. /home/user/bin/foo.sh
SCRIPT=$(readlink -f "$0")
# Name of the script
SCRIPTNAME=$(basename "$SCRIPT")
# Hostname
HOSTNAME=$(hostname)
# runtime measurement
declare -i iSTARTTIME=0
declare -i iENDTIME=0
declare -i iRUNTIME=0
declare -i iRUNTIMEH=0
declare -i iRUNTIMEM=0
declare -i iRUNTIMES=0
iSTARTTIME=$(date +%s)
# load config file
DATASET="none"
RESTIC_REPOSITORY="none"
RESTIC_PASSWORD="none"
RESTIC_ARGS=""
AWS_ACCESS_KEY_ID="none"
AWS_SECRET_ACCESS_KEY="none"
HC_URL=""
KEEP_WITHIN=""
KEEP_MONTHLY=""
RCLONE_PROGRAM=""
PATH_APPEND=""
if [ -f "$CONFIGFILE" ]; then
if [ "$(uname)" = "FreeBSD" ]; then
if [ "$(stat -f '%u' "$CONFIGFILE")" != "0" ]; then
echoerr "WARNING: $CONFIGFILE should be owned by root because it contains sensible data!"
fi
if [ "$(stat -f '%Lp' "$CONFIGFILE")" != "600" ]; then
echoerr "WARNING: $CONFIGFILE should have 600 permissions because it contains sensible data!"
fi
else
if [ "$(stat -c '%u' "$CONFIGFILE")" != "0" ]; then
echoerr "WARNING: $CONFIGFILE should be owned by root because it contains sensible data!"
fi
if [ "$(stat -c '%a' "$CONFIGFILE")" != "600" ]; then
echoerr "WARNING: $CONFIGFILE should have 600 permissions because it contains sensible data!"
fi
fi
source "$CONFIGFILE"
if [ -n "$PATH_APPEND" ]; then
PATH="$PATH:$PATH_APPEND"
export PATH
fi
else
echoerr "$CONFIGFILE not found!"
exit 1
fi
fnYesNo() {
REPLY="x"
while [ "${REPLY}" != "" ]; do
printf " (y/n) "
read -r REPLY
REPLY=$(echo $REPLY | head -c1 | tr [:upper:] [:lower:])
if [ "$REPLY" = "y" ]; then
echo "YES"
REPLY=""
return 0
elif [ "$REPLY" = "n" ]; then
echo "NO"
REPLY=""
return 1
else
echo "Only y/n is allowed as answer"
REPLY="x"
fi
done
}
fnCleanup () {
if [ -e "$DATASET_MOUNTPOINT"/.zfs/snapshot/backup-source/ ]; then
zfs destroy "$DATASET"@backup-source
fi
if [ -e "$LOCKDIR" ]; then
rm -rf "$LOCKDIR"
fi
}
fnSendStart () {
if [ ! "$1" ]; then
local MESSAGE="Empty message."
else
local MESSAGE="$1"
fi
echo "$MESSAGE"
# Ping Healthchecks.io
if [ -n "$HC_URL" ]; then
echo -n "Connecting to healthchecks.io: "
curl -fsS --retry 3 --data-raw "$SCRIPTNAME $VERSION: $MESSAGE" "$HC_URL/start"
echo ""
fi
}
fnSendSuccess () {
if [ ! "$1" ]; then
local MESSAGE="Empty message."
else
local MESSAGE="$1"
fi
echo "$MESSAGE"
# Ping Healthchecks.io
if [ -n "$HC_URL" ]; then
echo -n "Connecting to healthchecks.io: "
curl -fsS --retry 3 --data-raw "$SCRIPTNAME $VERSION: $MESSAGE" "$HC_URL"
echo ""
fi
}
fnSendError () {
if [ ! "$1" ]; then
local MESSAGE="Undefined error!"
else
local MESSAGE="$1"
fi
echoerr "$MESSAGE"
# Ping Healthchecks.io
if [ -n "$HC_URL" ]; then
echo -n "Connecting to healthchecks.io: "
curl -fsS --retry 3 --data-raw "$SCRIPTNAME $VERSION: $MESSAGE" "$HC_URL/fail"
echo ""
fi
}
fnSendStart "Starting backup: ${HOSTNAME}, ${DATASET}"
# root check
if [ "$(id -u)" -ne 0 ]; then fnSendError "Please run as root!"; exit 1; fi
# Repository type, get the part before first ":"
RESTIC_REPOSITORY_TYPE="${RESTIC_REPOSITORY%%:*}"
# check some vars
if [ "$RESTIC_REPOSITORY_TYPE" == "s3" ]; then
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
fnSendError "S3 variables not set!"
exit 1
fi
elif [ "$RESTIC_REPOSITORY_TYPE" == "rclone" ]; then
if [ -z "$RCLONE_PROGRAM" ]; then
fnSendError "rclone program not set!"
exit 1
fi
RESTIC_ARGS="-o rclone.program=$RCLONE_PROGRAM"
fi
# check if backup is already running
LOCKSUBDIR=$(echo "$RESTIC_REPOSITORY" | md5sum | head -c32)
LOCKPARENTDIR="/tmp/restic-dataset-backup"
if [ ! -e "$LOCKPARENTDIR" ]; then
mkdir "$LOCKPARENTDIR"
fi
LOCKDIR="$LOCKPARENTDIR/$LOCKSUBDIR"
if mkdir "$LOCKDIR"; then
echo "Locking succeeded."
else
fnSendError "Lock failed!"
exit 1
fi
# export some vars
export RESTIC_REPOSITORY
export RESTIC_PASSWORD
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
# init restic repo?
if [ "$INIT" = "YES" ]; then
printf "Do you really want to run 'restic init'?"
if fnYesNo; then
if ! restic "$RESTIC_ARGS" init; then
fnSendError "Restic init failed!"
fnCleanup
exit 1
else
fnSendSuccess "Restic init done!"
fnCleanup
exit 0
fi
else
fnSendSuccess "Restic init aborted!"
fnCleanup
exit 0
fi
fi
# unlock restic repo?
if [ "$UNLOCK" = "YES" ]; then
printf "Do you really want to run 'restic unlock' to remove stale locks?"
if fnYesNo; then
if ! restic "$RESTIC_ARGS" unlock; then
fnSendError "Restic unlock failed!"
fnCleanup
exit 1
else
fnSendSuccess "Restic unlock done!"
fnCleanup
exit 0
fi
else
fnSendSuccess "Restic unlock aborted!"
fnCleanup
exit 0
fi
fi
# create snapshot of ZFS dataset
if ! zfs snap "$DATASET"@backup-source; then fnSendError "Creating snapshot $DATASET@backup-source failed!"; fnCleanup; exit 1; fi
# Mount point of ZFS dataset
DATASET_MOUNTPOINT=$(zfs get -H -o value mountpoint "$DATASET")
# restic backup
if ! nice -n19 restic "$RESTIC_ARGS" --verbose backup --tag "dataset:$DATASET" --tag "mountpoint:$DATASET_MOUNTPOINT" "${DATASET_MOUNTPOINT}/.zfs/snapshot/backup-source/"; then
fnSendError "Restic backup failed!"
fnCleanup
exit 1
fi
# delete snapshot of ZFS dataset
if ! zfs destroy "$DATASET"@backup-source; then
sync; sleep 2; fnSendError "Destroying snapshot $DATASET@backup-source failed!"
fnCleanup
exit 1
fi
# restic forget
if [ -n "$KEEP_WITHIN" ] && [ -n "$KEEP_MONTHLY" ]; then
if ! nice -n19 restic "$RESTIC_ARGS" forget --keep-within "$KEEP_WITHIN" --keep-monthly "$KEEP_MONTHLY" --prune; then
sync; sleep 2; fnSendError "Restic forget failed!"
fnCleanup
exit 1
fi
fi
# unset some vars
unset RESTIC_REPOSITORY
unset RESTIC_PASSWORD
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
# remove lock dir
rm -rf "$LOCKDIR"
# runtime measurement
iENDTIME=$(date +%s)
iRUNTIME=$iENDTIME-$iSTARTTIME
iRUNTIMEH=${iRUNTIME}/3600
iRUNTIMEM=(${iRUNTIME}%3600)/60
iRUNTIMES=${iRUNTIME}%60
# send success mesage
fnSendSuccess "OK! Run time: $(printf "%02d:%02d:%02d" $iRUNTIMEH $iRUNTIMEM $iRUNTIMES)."