This repository has been archived by the owner on Jan 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbr.host
executable file
·692 lines (552 loc) · 19.1 KB
/
pbr.host
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#!/usr/bin/env bash
set -e
set -o pipefail
umask o=rwx,go=
show_help() {
echo "# pbr.sh — provisioning & backup & restore cycle for small hosts"
echo
echo "> pbr.host — host machine pbr.sh"
echo
echo "Usage: $0 {backup|restore|help}"
echo
echo "Examples:"
echo
echo " pbr.host backup - do backup"
echo " pbr.host restore - do restore"
echo " pbr.host help - display help message"
echo
}
# Regexp for removing unnecessary lines from logs (for notifications)
backup_log_start_pattern="using parent|start scan"
# Variables
restore_folder="./restore"
log_file_path="./pbr_log.txt"
log_separator="--------------------"
mysql_credentials_config_name="backup.cnf"
mysql_backup_file_name="docker_mysql_dump.sql"
# All working files used in sciprt should be listed in this array,
# so we can clean them up after script error / exit
working_files=()
is_mysql_credentials_uploaded_to_container=0
# Echo error string to stderr
# Exit if not error message provided
echo_err() {
[ -z "$1" ] && {
echo 'Error message not provided via $1' >&2
exit 1
}
printf "$1\n" >&2
}
# TODO: ability to see help here (before any checks)
# Check for required host toolset
check_host() {
required_host_programms=(restic curl docker)
for prog in $required_host_programms; do
if ! [ -n "$(command -v $prog)" ]; then
echo_err "$prog not installed or missing on host machine"
exit 1
fi
done
}
load_config() {
if [ -n "$1" ]; then
if [ -f "$1" ]; then
secrets_file="$1"
elif [ -d "$1" ]; then
if [ -f "$1/settings.conf" ]; then
secrets_file="$1/settings.conf"
else
echo_err "Can't find settings.conf file in secrets directory"
exit 1
fi
else
echo_err "Can't access $1"
exit 1
fi
else
secrets_file=${SECRETS_FILE:-~/.config/.secrets/settings.conf}
fi
source $secrets_file || {
echo_err "Can't load secrets file"
exit 1
}
}
setup_variables() {
if [ -z "$restoration_mode" ]; then
restoration_mode=${RESTORATION_MODE:-false}
fi
}
# Remove all created working files on script exit
finalize() {
# Remove files from host
for file in "${working_files[@]}"; do
# echo "removing working file $file"
rm -rf "${file}"
done
working_files=()
# Remove files from containers
if [[ $is_mysql_credentials_uploaded_to_container == 1 ]]; then
mysql_id=$(get_mysql_container_id 2> /dev/null)
if [ -n "$mysql_id" ]; then
remove_mysql_credentials_from_container $mysql_id
fi
fi
}
trap finalize EXIT ERR
# $1 -> repository name
init_backup_repository() {
# if repo not exists
if ! restic --password-file=<(echo $backup_storage_password) --repo="$1" cat config > /dev/null; then
# then init new one
restic --password-file=<(echo $backup_storage_password) --repo="$1" init || {
echo_err "Failed initializing restic repository $1"
exit 1
}
fi
}
# $1 -> bold title message
# $2 -> log message
# No return 1 or abort here, silently fail
notify_to_telegram() {
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$telegram_bot_token" ] || [ -z "$telegram_chat_id" ]; then
echo_err "Skipping telegram notifications (reason: not configured)"
return 0
fi
text="<b>$1</b>\n$2"
message_json="{
\"chat_id\": $telegram_chat_id,
\"text\": \"$text\",
\"parse_mode\": \"html\",
\"disable_notification\": true
}"
echo "$text"
curl --silent --show-error --fail -X POST \
-H 'Content-Type: application/json' \
-d "$message_json" https://api.telegram.org/bot${telegram_bot_token}/sendMessage > /dev/null || \
echo_err "sending to telegram failed"
return 0
}
# Convert "a,b" string to list
# "a, b,c" => a b c
# "a ,b" => a b
#
# "a a,b" => return 1, we don't support names with spaces like "my volume"
split_by_comma() {
[ -z "$1" ] && {
echo_err 'String for splitting not provided via $1'
return 1
}
local volumes=()
local OLD_IFS="$IFS";
local IFS=','
for v in $1; do
# remove space before and after volume name
volume_name=$(echo "$v" | sed 's/\(^ \| $\)//g') || {
echo_err "volume name filtering failed"
return 1
}
# if still found any space then abort script
if [[ "$volume_name" =~ " " ]]; then
echo_err "Names with spaces are forbidden"
return 1
fi
volumes+=("$volume_name")
done
local IFS="$OLD_IFS"
echo ${volumes[@]}
return 0
}
# Make directories argument list
# convert "a,b" to
# /a /b
#
# if --with-path-glob-ending provided in $2 then convert to:
# /a/* /b/*
volumes_string_to_mounted_dir_list() {
local volumes_list=()
local dir_list=()
volumes_list=$(split_by_comma "$1") || return 1
for v in $volumes_list; do
if [ "$2" = "--with-path-glob-ending" ]; then
dir_list+=("/$v/*")
else
dir_list+=("/$v")
fi
done
echo ${dir_list[@]}
return 0
}
folders_string_to_mounted_dir_list() {
local volumes_list=()
local dir_list=()
volumes_list=$(split_by_comma "$1") || return 1
dir_list=()
for v in $volumes_list; do
dir_list+=("/host$v")
done
echo ${dir_list[@]}
return 0
}
# Make docker volumes mount argument list
# convert from "a,b" to
# -v a:/a/ -v b:/b/
# if --readonly provided in $2 then convert to:
# -v a:/a/:ro -v b:/b/:ro
volumes_string_to_docker_volumes_arg() {
local volumes_list=()
volumes_list=$(split_by_comma "$1") || return 1
args_list=()
for v in $volumes_list; do
args_list+=("-v")
if [ "$2" = "--readonly" ]; then
args_list+=("$v:/$v/:ro")
else
args_list+=("$v:/$v/")
fi
done
echo ${args_list[@]}
return 0
}
# Make host folders mount argument list
# convert from "/home/user/folder" to
# -v /home/user/folder:/host/home/user/folder/
folders_string_to_docker_volumes_arg() {
local volumes_list=()
volumes_list=$(split_by_comma "$1") || return 1
args_list=()
for v in $volumes_list; do
args_list+=("-v")
args_list+=("$v:/host$v:ro")
done
echo ${args_list[@]}
return 0
}
# Remove old restore folder is exists
# Create new one and return absolute path via stdin
prepare_restore_folder() {
[ -d $restore_folder ] && rm -r $restore_folder # ensure no restore folder present
mkdir $restore_folder # create new one
restore_folder_abs_path=$(realpath $restore_folder) # construct absolute path
echo $restore_folder_abs_path # echo absolute path to stdout
return 0
}
get_mysql_container_id() {
mysql_id=$(docker ps --filter "ancestor=mysql" -q) || {
echo_err "Can't fetch mysql docker container id"
return 1
}
[ -z "$mysql_id" ] && {
echo_err "Mysql container not running"
return 1
}
echo $mysql_id
}
# Create config file suitable for mysql
# Copy this file to container
#
# $1 -> mysql container id
# $2 -> if --restore provided, then restore config will be used (for mysql binary)
#
# NOTICE: config file should be deleted after with remove_mysql_credentials_from_container
copy_mysql_credentials_to_container() {
[ -z "$1" ] && { echo_err 'mysql container id not provided via $1' && exit 1; }
# Determintate type of process, is it restore or backup?
if [ "$2" = "--restore" ]; then binary_name="mysql"; else binary_name="mysqldump"; fi
# Consturct mysql configuration file with credentials
echo -e "[${binary_name}]\nuser=$docker_mysql_user\npassword=$docker_mysql_password" > \
./${mysql_credentials_config_name}
working_files+=("./${mysql_credentials_config_name}")
is_mysql_credentials_uploaded_to_container=1
# Copy configuration file to container
docker cp ./${mysql_credentials_config_name} ${1}:/${mysql_credentials_config_name} || {
echo_err "Docker copy failed"
return 1
}
}
# $1 -> mysql container id
remove_mysql_credentials_from_container() {
docker exec "$1" sh -e -c "rm -f /${mysql_credentials_config_name}" || {
echo_err "Can't remove config file from container"
return 1
}
}
# $1 -> volumes list to check
check_docker_volumes_presence() {
[ -z "$1" ] && { echo_err "Volumes list not provided in $1" && return 1; }
local volumes_list
volumes_list="$(docker volume list -q)" || {
echo_err "Can't fetch docker volumes list"
return 1
}
for v in "pechorindev"; do
if ! [[ "$volumes_list" =~ "$v" ]]; then
echo_err "Docker volume not created on host machine"
return 1
fi
done
return 0
}
do_data_backup() {
if [ -z "$data_rclone_repo" ]; then
echo "Skipping data backup, data_rclone_repo not configured"
return 0
fi
if [ -z "$docker_backup_volumes" ] && [ -z "$host_backup_folders" ]; then
echo "Skipping data backup, docker_backup_volumes nor host_backup_folders not set"
return 0
fi
init_backup_repository "$data_rclone_repo" || return 1
local volumes_list=()
local volumes_arg_list=()
local mounted_dir_list=()
if [ -n "$docker_backup_volumes" ]; then
local tmp_list=()
tmp_list=$(split_by_comma "$docker_backup_volumes") || return 1
volumes_list+=(${tmp_list[@]})
check_docker_volumes_presence "$tmp_list" || return 1
tmp_list=$(volumes_string_to_docker_volumes_arg "$docker_backup_volumes" --readonly) || return 1
volumes_arg_list+=(${tmp_list[@]})
tmp_list=$(volumes_string_to_mounted_dir_list "$docker_backup_volumes") || return 1
mounted_dir_list+=(${tmp_list[@]})
fi
if [ -n "$host_backup_folders" ]; then
local tmp_list=()
tmp_list=$(split_by_comma "$host_backup_folders") || return 1
volumes_list+=(${tmp_list[@]})
tmp_list=$(folders_string_to_docker_volumes_arg "$host_backup_folders") || return 1
volumes_arg_list+=(${tmp_list[@]})
tmp_list=$(folders_string_to_mounted_dir_list "$host_backup_folders") || return 1
mounted_dir_list+=(${tmp_list[@]})
fi
# if only host folders backup needed then do not create container for backups
# this is mainly for perfomance on osx hosts backups
if [ -n "$host_backup_folders" ] && [ -z "$docker_backup_volumes" ]; then
echo "running host-only folders backup"
local host_folders
host_folders=$(split_by_comma "$host_backup_folders") || return 1
echo "backup args -> $restic_backup_args"
backup_log=$(restic -v --password-file=<(echo $backup_storage_password) $restic_backup_args \
--host=$backup_name \
--repo=$data_rclone_repo backup ${host_folders[@]})
else
echo "running docker backup"
# otherwise:
# Bakcup command will run in container:
backup_command="restic -v --password-file=<(echo \$backup_storage_password) $restic_backup_args \
--host=\$backup_name \
--repo=\$data_rclone_repo backup ${mounted_dir_list[@]}"
# Run backup inside container
# - mount backup volumes
# - mount rclone config
# - mount backup settings
# - run backup inside alpine container, but use bash instead of ash internally
# because we need bash process substituion <() for safety providing password to processes
backup_log=$(docker run --rm --name "volumes_backup" \
${volumes_arg_list[@]} \
-v "$(realpath ~/.config/rclone/rclone.conf):/root/.config/rclone/rclone.conf:ro" \
-v "$(realpath "$secrets_file"):/settings.conf:ro" \
frolvlad/alpine-bash bash -e -c "
apk update && apk add rclone restic
source /settings.conf
$backup_command
") || {
echo_err "docker volumes backup failed: $backup_log"
return 1
}
fi
# Log to stdout
printf "$backup_log\n"
# remove unnecessary data from telegram message and notify
filtered_log=$(printf "$backup_log" | sed -n -E -e "/$backup_log_start_pattern/,\$p")
notify_to_telegram "$backup_name volumes backup" "$filtered_log"
return 0
}
do_data_restore() {
if [ -z "$data_rclone_repo" ]; then
echo "Skipping data restore, data_rclone_repo not configured"
return 0
fi
local volumes_list
if [ -n "$docker_backup_volumes" ]; then
volumes_list=$(split_by_comma "$docker_backup_volumes") || return 1
check_docker_volumes_presence "$volumes_list" || return 1
volumes_arg_list=$(volumes_string_to_docker_volumes_arg $docker_backup_volumes) || return 1
mounted_dir_list=$(volumes_string_to_mounted_dir_list $docker_backup_volumes) || return 1
mounted_dir_list_with_glob=$(volumes_string_to_mounted_dir_list $docker_backup_volumes --with-path-glob-ending) || return 1
fi
restore_folder_path=$(prepare_restore_folder)
working_files+=($restore_folder_path)
# Make this configurable?
restic --password-file=<(echo $backup_storage_password) -v --repo="$data_rclone_repo" check || return 1
# Do restore on host folder
restic --password-file=<(echo $backup_storage_password) -v --repo="$data_rclone_repo" restore \
latest --target="$restore_folder_path" || return 1
restored_size=($(du -sh $restore_folder_path))
# Get snapshot id
restic_snapshot=$(restic --password-file=<(echo $backup_storage_password) -v --repo="$data_rclone_repo" snapshots -c --latest 1) || return 1
snapshot_id=$(echo "$restic_snapshot" | grep -E -e '^[a-z0-9]{8}' | cut -f 1 -d ' ') || {
echo_err "can't fetch snapshot id"
return 1
}
if [ "$restoration_mode" = 'true' ]; then
if [ -n "$docker_backup_volumes" ]; then
# Mount restore folder and docker volumes in container, and then do restoration
restore_log=$(docker run --rm --name "restore" \
${volumes_arg_list[@]} \
-v "$restore_folder_path:/restore/" \
frolvlad:alpine-bash bash -e -c "
apk add bash
rm -rf ${mounted_dir_list_with_glob[@]}
for v in ${volumes_list[@]}; do
cp -r /restore/\${v}/* /\${v}/
done
") || {
echo_err "docker volumes restore failed: $restore_log"
return 1
}
# Log to stdout
printf "$restore_log\n"
fi
if [ -n "$host_backup_folders" ]; then
local host_restore_to="$HOME/pbr.restore/host-folders-restore-$snapshot_id"
echo "Restoration host folders is not implemented, please do it by yourself"
echo "Processing regular restoring to $host_restore_to"
folders_list=$(split_by_comma "$host_backup_folders") || return 1
for folder in ${folder_list[@]}; do
mkdir -p "$host_restore_to"
mv -v "$restore_folder_path/$folder" "$host_restore_to/$folder"
done
fi
else
mkdir -p $HOME/pbr.restore/
mv "$restore_folder_path" "$HOME/pbr.restore/data-restore-$snapshot_id"
fi
notify_to_telegram "$backup_name $snapshot_id data restore finished" "total size: ${restored_size[0]}"
return 0
}
do_db_backup() {
if [ -z "$db_rclone_repo" ]; then
echo "Skipping db backup"
return 0
fi
init_backup_repository "$db_rclone_repo" || return 1
dumps=()
# Mysql docker backup
if [ -n "$docker_mysql_db_names" ]; then
mysql_id=$(get_mysql_container_id) || return 1
copy_mysql_credentials_to_container $mysql_id || return 1
# Run backup
# also ensure what config file removed fron container
mysql_databases=$(split_by_comma $docker_mysql_db_names) || return 1
docker exec $mysql_id sh -e -c "
mysqldump --defaults-extra-file=/${mysql_credentials_config_name} \
--databases $mysql_databases " > ./${mysql_backup_file_name} || {
echo_err "mysql dump failed"
return 1
}
dumps+=($mysql_backup_file_name)
working_files+=("./$mysql_backup_file_name")
fi
# Postgresql docker backup
if [ -n "$host_postgresql_db_names" ]; then
pg_databases=$(split_by_comma $host_postgresql_db_names) || return 1
# TODO: move to prerequirements part?
if [ -z "$(command -v pg_dump)" ]; then
echo_err "pg_dump not installed or missing on host machine"
return 1
fi
for db in $pg_databases; do
dump_path="./host_postgresql_${db}_dump.sql"
pg_dump --clean $db > $dump_path
dumps+=(host_postgresql_${db}_dump.sql)
working_files+=($dump_path)
done
fi
# if no backups was processed then return
[ 0 -eq ${#dumps[@]} ] && return 0;
# otherwise do backup
backup_log=$(restic -v --password-file=<(echo $backup_storage_password) \
--host=$backup_name \
--repo=$db_rclone_repo backup ${dumps[@]}) || { echo_err "db backup failed"; return 1; }
# Log to stdout
printf "$backup_log\n"
# remove unnecessary data from telegram message and notify
filtered_log=$(printf "$backup_log" | sed -n -E -e "/$backup_log_start_pattern/,\$p")
notify_to_telegram "$backup_name DB backup" "$filtered_log"
return 0
}
do_db_restore() {
if [ -z "$db_rclone_repo" ]; then
echo "Skipping db restore"
return 0
fi
restore_folder_path=$(prepare_restore_folder)
working_files+=($restore_folder_path)
# Make this configurable?
restic --password-file=<(echo $backup_storage_password) -v --repo="$db_rclone_repo" check || return 1
# Do restore on host folder
restic --password-file=<(echo $backup_storage_password) -v --repo="$db_rclone_repo" restore latest \
--target="$restore_folder_path" || return 1
restored_size=($(du -sh $restore_folder_path))
if [ "$restoration_mode" = "true" ]; then
if [ -n "$docker_mysql_db_names" ]; then
mysql_id=$(get_mysql_container_id) || return 1
copy_mysql_credentials_to_container $mysql_id --restore || return 1
# Run restore
# also ensure what config file removed fron container
docker exec -i $mysql_id sh -c "mysql --defaults-extra-file=/${mysql_credentials_config_name}" \
< ${restore_folder_path}/${mysql_backup_file_name} || {
echo_err "mysql restore failed"
return 1
}
fi
if [ -n "$host_postgresql_db_names" ]; then
pg_databases=$(split_by_comma $host_postgresql_db_names) || return 1
for db in $pg_databases; do
psql $db < ${restore_folder_path}/host_postgresql_${db}_dump.sql
done
fi
else
local host_restore_to="$HOME/pbr.restore/db-restore-$snapshot_id"
mkdir -p $host_restore_to
mv $restore_folder_path/* $host_restore_to/
fi
notify_to_telegram "Restoring $backup_name db backup" "total size: ${restored_size[0]}"
}
# Execution part
if [ "$1" = "backup" ]; then
current_action="backup"
elif [ "$1" = "restore" ]; then
current_action="restore"
elif [ "$1" = "help" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
show_help
exit 1
else
# TODO: add usage
echo_err "action not provided: pbr.host backup\n"
show_help
exit 1
fi
run() {
echo -e "\n$log_separator"
echo "Starting $current_action at $(date)"
if [ "$current_action" = "restore" ]; then
echo "Restoration mode enabled: $restoration_mode"
do_data_restore || return 1
do_db_restore || return 1
elif [ "$current_action" = "backup" ]; then
do_data_backup || return 1
do_db_backup || return 1
fi
echo -e "$current_action completed at $(date)"
}
echo "Running pbr.sh (on host machine)"
check_host
load_config "$2"
setup_variables
echo "at $ssh_connection_string"
run $1 &>> $log_file_path || {
notify_to_telegram "$current_action script failed" \
"inspect errors on host ~/$log_file_path"
}
echo "pbr.sh finished, log can be found in $log_file_path"