-
Notifications
You must be signed in to change notification settings - Fork 0
/
backUP
executable file
·506 lines (443 loc) · 14.8 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#!/usr/bin/bash
source /usr/local/lib/backUP/logging
source /usr/local/lib/backUP/args2array
CONFIGURATION_FILE='/usr/local/etc/backUP/backUP.conf'
MIN_BASH_VER='4.0'
MIN_BORG_VER='1.2.3'
MIN_COREUTIL_VER='7.0'
DATE="$(date +%m-%d-%Y-%I:%M:%S-%p)"
FILENAME="$(basename $0)"
IS_NUM_REGEX='^[1-9][0-9]*$'
REPO_STR='This is a Borg Backup repository.'
SMOL_OPTS='a:e!r:hblLcs!v!'
LONG_OPTS='dest!,repo!,pass,dupe:,conf!'
declare -a dupe_paths
usage(){
>&2 cat << EOF
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
This program is used to manage the backup 'database' defined in '$CONFIGURATION_FILE'
It is probably inefficient, buggy, and prone to explode or worse.
Usage:
$FILENAME option [<path>]
Options:
-a <path> : Add <path> to the 'database'
-b : Backup all 'database' entries with BORG backup
-c : Prune and then cleanup BORG backups
--conf <path> : Specify an alternate $FILENAME config file to use
--dupe <path> : Duplicate to <path> ( No limit to number of calls )
-e : Extract a BORG backup ( Defaults to ~/temp )
--dest <path> : Set extraction directory ( Only applies to -e flag )
-l : List all BORG backups
-L : List all paths monitored by $FILENAME
-h : Display usage information
-r <path> : Remove <path> from the 'database'
-s <name> : Search archives for <name>
--pass : Prompts for the BORG Backup passphrase
--repo <path> : Sets the BORG repo to <path>
Configuration options are loaded from '$CONFIGURATION_FILE'
A Borg repository must be setup prior to using '$FILENAME'. Use:
borg init /path/to/borg/repository
EOF
exit 1
}
parse_config() {
# don't allow multiple configuration files to pollute each other
unset __LOG_STOUT__ __LOG_LEVEL__ __LOGPATH__ BORG_PASSPHRASE
unset BORG_REPO BACKUP_DB SOME_USER EXTRACT_TO dupe_paths
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
local -A config verb
config=(
[BORG_PASSPHRASE]=""
[BORG_REPO]=""
[BACKUP_DB]=""
[SOME_USER]="$SUDO_USER"
[EXTRACT_TO]=""
)
verb=( [STOUT]=0 [CRIT]=1 [ERROR]=2 [DEBUG]=3 [TRACE]=4 )
while read line; do
# skip processing lines that begin with '#'
[[ "$line" =~ ^#.*$ ]] && continue
if echo "$line" | grep -F = &>/dev/null; then
grab_key "$line" varname
grab_value "$line" value
edumpvar varname value
case "$varname" in
DUPLICATE_REPO_PATH )
tval="$(realpath -mq "$value")"
validate_duplicate "$tval" && dupe_paths+=("$tval")
;;
BORG_REPO )
validate_repo "$value" && config["$varname"]="$value"
;;
* )
config["$varname"]="$value"
;;
esac
fi
done < "$CONFIGURATION_FILE"
if [[ "${config[LOGSTOUT]}" == "true" || "${config[LOGSTOUT]}" == "1" ]]; then
export __LOG_STOUT__=1
fi
if [[ "${config[LOGSTERR]}" == "true" || "${config[LOGSTERR]}" == "1" ]]; then
export __LOG_STERR__=1
fi
if [[ -n "${config[LOGLEVEL]}" ]]; then
if [[ "${verb[${config[LOGLEVEL]}]+1}" ]]; then
export __LOG_LEVEL__=${verb[${config[LOGLEVEL]}]}
else
ewarn "$FILENAME does not recognize configuration option LOGLEVEL = ${config[LOGLEVEL]}"
fi
fi
if [[ -n "${config[LOGPATH]}" ]]; then
export __LOGPATH__="${config[LOGPATH]}"
__open_logfile__
fi
export BORG_PASSPHRASE="${config[BORG_PASSPHRASE]}"
export BORG_REPO="${config[BORG_REPO]}"
export BACKUP_DB="${config[BACKUP_DB]}"
export SOME_USER="${config[SOME_USER]}"
if [[ -z "${config[EXTRACT_TO]}" ]]; then
export EXTRACT_TO="$(getent passwd "$SOME_USER" | cut -d: -f6)/temp"
else
export EXTRACT_TO="${config[EXTRACT_TO]}"
fi
}
append_to_backup_list() {
local argument
for argument in "$@"; do
if [[ ! -f "$argument" && ! -d "$argument" ]]; then
ecrit "Path '$argument' doesn't seem to exist. Exiting."
exit 1
fi
done
for argument in "$@"; do
# normalize the input
file="$(readlink -e "$argument")"
if ! grep -Fxq "$file" "$BACKUP_DB"; then
eprint "Appending '$file' to '$BACKUP_DB'"
echo "$file" >> "$BACKUP_DB"
else
ewarn "Entry for '$file' already exists in '$BACKUP_DB'"
fi
done
sort -o "$BACKUP_DB" "$BACKUP_DB"
}
remove_from_backup_list() {
local -i db_entry_line_num num_lines=$(wc -l "$BACKUP_DB" | awk '{print $1}')
local -A lines_passed
local argument db_entry
for argument in "$@"; do
if [[ $argument =~ $IS_NUM_REGEX && $argument -le $num_lines ]]; then
# function received a number less than or equal to the number of borg backups
db_entry=$(sed -n "$argument{p;q}" "$BACKUP_DB")
db_entry_line_num="$argument"
else
file="$(readlink -e "$argument")"
if grep -Fxq "$file" "$BACKUP_DB"; then
# function received an entry tracked by the database
db_entry="$file"
db_entry_line_num="$(grep -nFx "$file" "$BACKUP_DB" | cut -f1 -d:)"
else
# function does not recognize the argument received
ecrit "No match to '$file' found in '$BACKUP_DB'"
exit 1
fi
fi
lines_passed["$db_entry_line_num"]="$db_entry"
done
local sed_string=""
for key in "${!lines_passed[@]}"; do
eprint "Removing '${lines_passed[$key]}' from '$BACKUP_DB'"
sed_string+="${key}d;"
done
sed -i "${sed_string%;}" "$BACKUP_DB"
}
parse_parents() {
local line
while read line; do
printf '%s\n' "$line"
done < "$BACKUP_DB" | \
# The commands below (recursively?) creates a unique entry for the full *parent* directory structure
# pointing to the file/folder we want backed up,
# Stolen from: https://serverfault.com/questions/877254/tar-does-not-retain-directory-permissions
awk -F/ '{ d=$1; for (i=2; i <= NF; i++) { print d; d=d "/" $i }; print d }' | \
awk '{ if (index(old,$0 "/") != 1) { old=$0; print } }' | \
sed -e '/^[[:space:]]*$/d' -e '/\/$/d'
}
recurse_directories() {
while read line; do
if [[ -d $line ]]; then
# this recursively prints all files and subdirectories of the folder we wish to backup
find "$line" -print
fi
done < "$BACKUP_DB"
}
create_borg_backup() {
eprint "Creating new backup '$DATE' in '$BORG_REPO'"
{ parse_parents && recurse_directories; } | sort -u | \
borg create --paths-from-stdin "$BORG_REPO::$DATE"
}
list_borg_backups() {
local -i counter
borg list "$BORG_REPO" | \
while read line; do
((counter+=1))
if [[ $counter -eq 1 ]]; then
printf "\n%-8s%s\n" "#" "ARCHIVE NAME"
printf "%s%s\n" "------ ------------"
fi
archive_name=$(echo $line | awk '{print $1}')
if [[ $# -eq 0 ]]; then
printf "%-8s%s\n" "$counter" "$archive_name"
elif [[ $counter -eq $1 ]]; then
printf "%-8s%s\n" "[*$counter]" "$archive_name"
else
printf "%-8s%s\n" "$counter" "$archive_name"
fi
done
printf "\n"
}
restore_borg_backup() {
local -i num_archives=$(borg list "$BORG_REPO" | wc -l)
local borg_archive
# Don't change permissions of /
if [[ ! -d "$EXTRACT_TO" ]]; then
install -d -m 0755 -o "$SOME_USER" -g "$SOME_USER" "$EXTRACT_TO"
fi
if [[ $1 =~ $IS_NUM_REGEX && $1 -le $num_archives ]]; then
# function received a number that is less than or equal to the number of borg backups
borg_archive=$(borg list "$BORG_REPO" | sed -n "$1{p;q}" | awk '{print $1}')
else
borg_archive=$1
fi
eprint "Extracting '$borg_archive' to '$EXTRACT_TO'."
local workingdir=$(pwd)
cd "$EXTRACT_TO"
borg extract "$BORG_REPO::$borg_archive"
if [[ $? -ne 0 ]]; then
eerror "Borg Backup reports that archive '$1' does not exist in '$BORG_REPO'"
eerror "Use '$FILENAME -l' to display a list of all valid archives."
else
eprint "Success! Files extracted to $EXTRACT_TO."
fi
cd "$workingdir"
}
search() {
local a
borg list --short "$BORG_REPO" | \
while read -r a; do
printf "%s\n%s\n" "${a}" "----------------------"
borg list --short "${BORG_REPO}::$a" | grep "$1"
echo
done | \
awk 'BEGIN { empty = 0 } {
if ($0 == "") {
if (NR - empty == 3) {
start = 0
} else {
print data
}
empty = NR
data = ""
} else {
if (start == 0) {
start = NR
}
data = data "\n" $0
}
}
END { print data }'
}
borg_compact_and_prune() {
borg prune --list --keep-daily=7 --keep-weekly=3 --keep-monthly=11 --keep-yearly=1 "$BORG_REPO"
borg compact "$BORG_REPO"
}
duplicate_borg_repo() {
local primary_repo="$(realpath -mq "$BORG_REPO")/" # strip excessive trailing slashes
if validate_repo "$primary_repo"; then
if [[ ${#dupe_paths[@]} -gt 0 ]]; then
for secondary_repo in "${dupe_paths[@]}"; do
rsync -au --delete --timeout=1 "$primary_repo" "$secondary_repo" > /dev/null
if [[ $? -ne 0 ]]; then
ewarn "rsync encountered an error duplicating '$primary_repo' to '$secondary_repo'"
else
edebug "Successfully duplicated to '$secondary_repo'"
fi
done
else
ewarn "No duplication paths specified. '$primary_repo' will not be duplicated"
fi
else
ecrit "BORG repo '$primary_repo' is not valid. This shouldn't have happened"
exit 1
fi
}
print_database() {
printf "\n%-8s%-8s%s\n" "#" "TYPE" "PATH"
printf "%s%s%s\n" "------ ------ ------"
local -i counter
while read line; do
if [[ -d $line ]]; then
entry_type="Folder"
elif [[ -f $line ]]; then
entry_type="File"
fi
((counter+=1))
if [[ $# -eq 0 ]]; then
printf "%-8s%-8s%s\n" "$counter" "$entry_type" "$line"
elif [[ $counter -eq $1 ]]; then
printf "%-8s%-8s%s\n" "[*$counter]" "$entry_type" "$line"
else
printf "%-8s%-8s%s\n" "$counter" "$entry_type" "$line"
fi
done < "$BACKUP_DB"
}
get_password() {
local first_entry second_entry
read -s -p "Enter Password: " first_entry
echo
read -s -p "Re-Enter Password: " second_entry
echo
if [[ "$first_entry" == "$second_entry" ]]; then
export BORG_PASSPHRASE="$first_entry"
else
eerror "Passwords don't match. Please try again."
get_password
fi
borg info "$BORG_REPO" > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
ecrit "Your password for '$BORG_REPO' is incorrect. Exiting."
exit 1
fi
}
validate_repo() {
if ! [[ -f "${1}/README" && "$(head -n 1 "${1}/README")" == "$REPO_STR" ]]; then
# according to https://borgbackup.readthedocs.io/en/stable/internals/data-structures.html#repository
# all BORG repositories contain a "README" file with the string "This is a Borg Backup repository."
ecrit "The path, '$1' does not contain a valid BORG repository. Exiting."
exit 2
else
return 0
fi
}
validate_duplicate() {
# Duplication is performed with rsync --delete so anything in the destination will be wiped
# This script runs as root and we don't want to accidentally erase any important directories!
local duplicate_repo_path="$1"
if [[ ! -d "$duplicate_repo_path" ]]; then
# argument is not a valid directory
ewarn "Duplication path '$duplicate_repo_path' is not a valid directory and will be skipped."
return 1
elif awk '$1 !~ /^#/ && $2 ~ /^[/]/ {print $2}' /etc/fstab | grep -Fxq "$duplicate_repo_path"; then
# mountpoint command provided by util-linux - checks if duplicate_repo_path is mounted
if ! mountpoint -q "$duplicate_repo_path"; then
ewarn "'$duplicate_repo_path' isn't mounted and will be skipped."
return 1
fi
elif [[ $(ls -A "$duplicate_repo_path" | wc -l) -eq 0 ]]; then
# empty directories are OK
return 0
elif [[ -f "${duplicate_repo_path}/README" && "$(head -n 1 "${duplicate_repo_path}/README")" == "$REPO_STR" ]]; then
# according to https://borgbackup.readthedocs.io/en/stable/internals/data-structures.html#repository
# all BORG repositories contain a "README" file with the string "This is a Borg Backup repository."
return 0
else
# duplication directory is probably not a BORG repo, and we don't want to delete everything inside of it
ewarn "Duplication path '$duplicate_repo_path' is either non-empty or not a BORG repo and will be skipped."
return 1
fi
}
grab_key() {
# parameter $1 contains the config file line
declare -n gk_kref=$2 # gk_kref is a reference to the variable passed as parameter 2
gk_kref=${1#"${1%%[![:space:]]*}"} # remove leading whitespace
gk_kref=${gk_kref%%=*} # extract key before the first equal sign
gk_kref=${gk_kref%"${gk_kref##*[![:space:]]}"}
}
grab_value() {
# paraemter $1 contains the config file line
local -n gv_vref=$2 # gv_vref is a reference to the variable passed as parameter 2
gv_vref="${1#*=}" # remove everything up to and including first '=' from head of input
gv_vref="${gv_vref#"${gv_vref%%[![:space:]]*}"}" # removes leading whitespace
gv_vref="${gv_vref%"${gv_vref##*[![:space:]]}"}" # remove trailing whitespace
gv_vref="${gv_vref#"${gv_vref%%[![:space:]\"]*}"}" # remove first " and any white space between it and the next character
gv_vref="${gv_vref%"${gv_vref##*[![:space:]\"]}"}"
}
__init_messages__ "$@"
if [[ ${__ARGS_LOADED__} -ne 1 ]]; then
ecrit "$FILENAME depends on args2array."
exit 1
fi
if [[ ${__LOGS_LOADED__} -ne 1 ]]; then
ecrit "$FILENAME depends on logging."
exit 1
fi
{ printf '%s\n%s' "$MIN_BASH_VER" "$BASH_VERSION" | sort -C -V; } > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
ecrit "$FILENAME depends on coreutils v${MIN_COREUTIL_VER}+ and Bash v${MIN_BASH_VER}+. Your system is configured with"
printf '%-11s%s\n' "Coreutils:" "$(whoami --version | head -n 1 | awk '{print $NF}')"
printf '%-11s%s\n\n' "Bash:" "$BASH_VERSION"
exit 1
fi
borg -V > /dev/null 2>&1
borg_install_check=$?
{ printf '%s\n%s' "$MIN_BORG_VER" "$(borg -V | awk '{print $NF}')" | sort -C -V; } > /dev/null 2>&1
borg_version_check=$?
if [[ $borg_install_check -ne 0 || $borg_version_check -ne 0 ]]; then
ecrit "$FILENAME depends on BORG Backup v${MIN_BORG_VER}+. Your system is configured with"
printf '%-13s%s\n\n' "BORG Backup:" "$(borg -V | awk '{print $NF}')"
exit 1
fi
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
if [[ $EUID -ne 0 ]]; then
ecrit "$FILENAME requires sudo privileges."
exit 1
fi
parse_config
declare -A ARGS
args2array ARGS "$@"
for arg in "${!ARGS[@]}"; do
case "$arg" in
a ) unpack_args ADD_ARGS "${ARGS[a]}"
;;
repo )
REPO_ARG="$(readlink -e "${ARGS[repo]}")"
;;
dupe ) unpack_args dupes "${ARGS[dupe]}"
for i in "${dupes[@]}"; do
tval="$(realpath -mq "$i")"
validate_duplicate "$tval" && dupe_paths+=("$tval")
done
;;
conf )
CONF_ARG="$(realpath -mq "${ARGS[conf]}")"
;;
r ) unpack_args REM_ARGS "${ARGS[r]}"
;;
esac
done
if [[ -v ARGS[h] ]]; then
usage
exit 0
else
[[ -v ARGS[conf] ]] && CONFIGURATION_FILE="$CONF_ARG" && parse_config
[[ -v ARGS[repo] ]] && validate_repo "$REPO_ARG" && export BORG_REPO="$REPO_ARG"
[[ -v ARGS[pass] ]] && get_password
[[ -v ARGS[dest] ]] && export EXTRACT_TO="${ARGS[dest]}"
[[ -v ARGS[r] ]] && remove_from_backup_list "${REM_ARGS[@]}"
[[ -v ARGS[a] ]] && append_to_backup_list "${ADD_ARGS[@]}"
[[ -v ARGS[b] ]] && create_borg_backup
[[ -v ARGS[c] ]] && borg_compact_and_prune
[[ -v ARGS[e] ]] && restore_borg_backup "${ARGS[e]}"
[[ -v ARGS[s] ]] && search "${ARGS[s]}"
[[ -v ARGS[l] ]] && list_borg_backups
[[ -v ARGS[L] ]] && print_database
duplicate_borg_repo
__end_messages__
fi
exit 0