fixes, logging, and better shellcheck conformance
[distro-setup] / btrbk-run
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16
17 # todo: if we cancel in the middle of a btrfs send, then run again
18 # immediately, the received subvolume doesn't get a Received UUID:
19 # field, and we won't mount it. Need to figure out a solution that will
20 # fix this.
21
22
23 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
24
25 source /usr/local/lib/err
26
27 usage() {
28 cat <<'EOF'
29 btrbk-run [OPTIONS] [run|resume|archive]
30 usually -t TARGET_HOST or -s SOURCE_HOST
31
32 Note, at source location, intentionally not executable, run and read
33 install-my-scripts.
34
35 EOF
36 echo "top of script file:"
37 sed -n '1,/^[# ]*end command line/{p;b};q' "$0"
38 exit $1
39 }
40
41
42 pre=btrbk-run
43
44 script_name="${BASH_SOURCE[0]}"
45 script_name="${script_name##*/}"
46 m() { if $verbose; then printf "$pre %s\n" "$*"; fi; "$@"; }
47 e() { printf "$pre %s\n" "$*"; }
48 die() { printf "$pre error: %s\n" "$*" >&2; echo "$pre exiting with status 1" >&2; exit 1; }
49 mexit() { echo "$pre exiting with status $1"; exit $1; }
50
51 # latest $MAIL_HOST
52 if [[ -e /b/bash_unpublished/source-state ]]; then
53 source /b/bash_unpublished/source-state
54 fi
55
56 # note q is owned by root:1000
57
58 mountpoints=()
59
60 rsync_mountpoint=/q
61
62 ret=0
63 # default options
64 conf_only=false
65 dry_run=false # mostly for testing
66 rate_limit=no
67 verbose=true; verbose_arg=-v
68 force=false
69 if [[ $INVOCATION_ID ]]; then
70 # INVOCATION_ID means running as a systemd service. we cant show progress in this case,
71 # but if we pass the arg, it will insert mbuffer into the command.
72 progress_arg=
73 else
74 progress_arg="--progress"
75 fi
76 incremental_strict=false
77 pull_reexec=false
78
79 default_args_file=/etc/btrbk-run.conf
80 if [[ -s $default_args_file ]]; then
81 # shellcheck disable=SC2046 # we want word splitting
82 set -- $(< $default_args_file) "$@"
83 # i havent used this feature yet, so warn about it
84 echo "$0: warning: default btrbk-run options set in $default_args_file (sleeping 5 seconds):"
85 cat $default_args_file
86 sleep 5
87 fi
88
89 targets=()
90 early=false
91 fast=false
92 kd_spread=false
93 check_installed=false
94 orig_args=("$@")
95 temp=$(getopt -l check-installed,fast,pull-reexec,help 23cefikl:m:npqrs:t:vh "$@") || usage 1
96 eval set -- "$temp"
97 while true; do
98 case $1 in
99 # for the rare case we want to run multiple instances at the same time
100 -2) conf_suf=2 ;;
101 -3) conf_suf=3 ;;
102 # only creates the config file, does not run btrbk
103 -c) conf_only=true ;;
104 --check-installed)
105 check_installed=true
106 ;;
107 # quit early, just btrbk, no extra remounting etc.
108 -e) early=true ;;
109 # avoids some default behaviors:
110 # - no skipping hosts where xprintidle haven't been idle recently
111 # - exit if we can't ssh to 1 or more hosts
112 # - still set default hosts despite MAIL_HOST status
113 -f) force=true ;;
114 # skip various checks. when we run twice in a row for
115 # switch mail-host, no need to repeat the same checks again.
116 --fast) fast=true ;;
117 -i) incremental_strict=true ;;
118 # note this implies resume and -p
119 -k) kd_spread=true ;;
120 # bytes per second, suffix k m g
121 -l) rate_limit=$2; shift ;;
122 # Comma separated mountpoints to backup. This has defaults set below.
123 -m) IFS=, mountpoints=($2); unset IFS; shift ;;
124 -n) dry_run=true ;;
125 # preserve existing snapshots and backups
126 -p) preserve_arg=-p ;;
127 # internal option for rerunning under newer SOURCE_HOST version.
128 --pull-reexec) pull_reexec=true;;
129 # quiet
130 -q) verbose=false; verbose_arg=; progress_arg= ;;
131 # source host to receive a backup from
132 -s)
133 source=$2
134 bbksource=$source
135 if [[ $source == *:* ]]; then
136 bbksource="[$source]"
137 fi
138 shift
139 ;;
140 # target hosts to send to. empty is valid for just doing local
141 # snapshot. we have default hosts we will populate.
142 -t) IFS=, targets=($2); unset IFS; shift ;;
143 # verbose.
144 -v) verbose=true; verbose_arg=-v ;;
145 -h|--help) usage ;;
146 --) shift; break ;;
147 *) die "Internal error!" ;;
148 esac
149 shift
150 done
151
152 cmd_arg="$1"
153
154
155 uninstalled-file-die() {
156 die "uninstalled file $1. run install-my-scripts or rerun with -f"
157 }
158
159 if ! $force && { $check_installed || [[ ! $source ]]; } ; then
160 install_bin_files=(
161 mount-latest-subvol
162 check-subvol-stale
163 btrbk-run
164 )
165 for f in ${install_bin_files[@]}; do
166 if ! diff -q /a/bin/ds/$f /usr/local/bin/$f; then
167 uninstalled-file-die $f
168 fi
169 done
170 if ! diff -q /a/bin/errhandle/err /usr/local/lib/err; then
171 uninstalled-file-die err
172 fi
173 if $check_installed; then
174 exit 0
175 fi
176 fi
177
178
179 if $kd_spread; then
180 if [[ $cmd_arg && $cmd_arg != resume ]]; then
181 die "dont pass -k without resume or empty run arg"
182 fi
183 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
184 die "something went wrong, -k not meant to be run on MAIL_HOST"
185 fi
186 if [[ $HOSTNAME != kd ]]; then
187 die "something went wrong, -k only meant to run on kd"
188 fi
189 cmd_arg=resume
190 preserve_arg=-p
191 h=sy
192 add-wireless-target-h
193 fi
194
195 if [[ ! $cmd_arg ]]; then
196 cmd_arg=run
197 fi
198
199
200 std_preserve="36h 14d 8w 24m"
201 q_preserve="18h 14d 8w"
202
203 case $cmd_arg in
204 run|resume) : ;;
205
206 # This works better than the normal archive command. We have to
207 # specify the mount points, but that is what we are used to doing and
208 # we prefer it. Another difference is that archive works recursively
209 # and we don't care about that. Sometimes we may still want to run
210 # btrbk archive, but it doesn't even use the config file, so just
211 # run it directly, eg:
212 # time s btrbk -v archive /mnt/r7/amy/boot/btrbk ssh://bo/mnt/boot2/btrbk
213 archive)
214 cmd_arg=resume
215 std_preserve="999h 999d 999w 999m"
216 q_preserve="$std_preserve"
217 preserve_arg=-p
218 ;;
219 *) die "untested command arg" ;;
220 esac
221
222 if (( $# > 1 )); then
223 die: "only 1 nonoption arg is supported"
224 fi
225
226 if [[ -v targets && $source ]]; then
227 # note, this doesnt need to be the case, but
228 # we would need to think about it.
229 die "error: -t and -s are mutually exclusive"
230 fi
231
232 ### end options parsing
233
234 # remove path from earlier version of btrbk
235 rm -f /usr/sbin/btrbk
236 # note, this still works as intended if there is no /usr/bin/btrbk
237 if [[ /a/opt/btrbk/btrbk -nt /usr/bin/btrbk ]]; then
238 if [[ -e /b/distro-functions/src/package-manager-abstractions ]]; then
239 . /b/distro-functions/src/package-manager-abstractions
240 pi asciidoctor
241 fi
242 cd /a/opt/btrbk
243 m make install
244 fi
245
246 # TODO: i wonder if there should be an option to send to the default
247 # targets, plus any given on the command line.
248
249
250 at_work=false
251 at_home=false
252
253
254 set-location() {
255 case $HOSTNAME in
256 kw)
257 at_work=true
258 ;;
259 kd|frodo)
260 at_home=true
261 ;;
262 x2|x3|sy)
263 if [[ $(dig +short @10.2.0.1 -x 10.2.0.2 2>&1 ||:) == kd.b8.nz. ]] \
264 && ip n show 10.2.0.1 | grep . &>/dev/null; then
265 at_home=true
266 elif ping -q -c1 -w1 hal.office.fsf.org &>/dev/null \
267 && ip n show 192.168.0.26 | grep . &>/dev/null; then
268 at_work=true
269 fi
270 ;;
271 esac
272 }
273
274 exit-if-no-default-targets() {
275 if ! $force && [[ $HOSTNAME != "$MAIL_HOST" ]]; then
276 echo "MAIL_HOST=$MAIL_HOST, nothing to do"
277 mexit 0
278 fi
279 case $HOSTNAME in
280 kw|kd|frodo|x2|x3|sy) : ;;
281 *)
282 die "error: no default targets for this host, use -t"
283 ;;
284 esac
285 }
286
287 add-x3-target() {
288 # main work machine
289 if ping -q -c1 -w1 x3.office.fsf.org &>/dev/null; then
290 targets+=(x3.office.fsf.org)
291 elif ping -q -c1 -w1 $h.b8.nz &>/dev/null; then
292 # in case we took it home
293 targets+=(x3.b8.nz)
294 else
295 targets+=(x3wg.b8.nz)
296 fi
297 }
298
299 add-wireless-target-h() {
300 if ping -q -c1 -w1 $h.b8.nz &>/dev/null; then
301 targets+=($h.b8.nz)
302 elif ping -q -c1 -w1 ${h}w.b8.nz &>/dev/null; then
303 targets+=(${h}w.b8.nz)
304 fi
305 }
306
307
308 # set default targets
309 if [[ ! -v targets && ! $source ]]; then
310 exit-if-no-default-targets
311 set-location
312 if $at_home; then
313 if ! $kd_spread && [[ $HOSTNAME != x3 ]]; then
314 add-x3-target
315 fi
316 if [[ $HOSTNAME != kd ]]; then
317 targets+=(kd.b8.nz)
318 fi
319 wireless_home_hosts=(
320 x2
321 sy
322 )
323 for h in ${wireless_home_hosts[@]}; do
324 if [[ $HOSTNAME != "$h" ]]; then
325 add-wireless-target-h
326 fi
327 done
328 elif $at_work; then
329 targets+=(i.b8.nz)
330 for h in x2 x3 kw; do
331 if [[ $HOSTNAME == "$h" ]]; then
332 continue
333 fi
334 if ping -q -c1 -w1 $h.office.fsf.org &>/dev/null; then
335 targets+=($h.office.fsf.org)
336 fi
337 done
338 else
339 targets+=(i.b8.nz)
340 fi
341 fi
342
343 if [[ ${mountpoints[0]} ]]; then
344 for mp in ${mountpoints[@]}; do
345 if [[ -e /nocow/btrfs-stale/$mp ]]; then
346 die "error: $mp is stale, mount-latest-subvol first"
347 fi
348 done
349 else
350 # set default mountpoints
351 if [[ ${targets[0]} == tp ]]; then
352 prospective_mps=(/a)
353 else
354 case $HOSTNAME in
355 *)
356 prospective_mps=()
357 if [[ $source ]]; then
358 source_state="$(ssh $source cat /a/bin/bash_unpublished/source-state)"
359 eval "$source_state"
360 source_host="$(ssh $source cat /etc/hostname)"
361 if [[ $source_host == "$MAIL_HOST" ]]; then
362 prospective_mps+=(/o)
363 fi
364 if [[ $source_host == "$HOST2" ]]; then
365 prospective_mps+=(/a /ar /qr /qd /q)
366 fi
367 else
368 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
369 prospective_mps+=(/o)
370 fi
371 if [[ $HOSTNAME == "$HOST2" ]]; then
372 prospective_mps+=(/a /ar /qr /qd /q)
373 fi
374 if $kd_spread; then
375 prospective_mps=(/a /ar /o /qr /qd /q)
376 fi
377 fi
378 # note: put q last just in case its specific retention options were to
379 # affect other config sections. I havent tested if that is the case.
380 ;;
381 esac
382 fi
383 for mp in ${prospective_mps[@]}; do # default mountpoints to sync
384 if [[ -e /nocow/btrfs-stale/$mp ]]; then
385 e "warning: $mp stale, not adding to default mountpoints"
386 continue
387 fi
388 if awk '{print $2}' /etc/fstab | grep -xF $mp &>/dev/null; then
389 mountpoints+=($mp)
390 fi
391 done
392 fi
393
394 if (( ! ${#mountpoints[@]} )); then
395 die didnt get mountpoint arg and had no defaults
396 fi
397
398 ##### end command line parsing ########
399
400 #### begin pre-checks #####
401
402 # todo: this has a timing problem, since btrbk.timer could activate the service after this check.
403 if ! $fast && [[ $source ]]; then
404 if [[ $(ssh $source ps --no-headers -o comm 1) == systemd ]]; then
405 status=$(ssh $source systemctl is-active btrbk.service) || : # normally returns 3
406 case $status in
407 inactive|failed) : ;;
408 *)
409 echo "$0: error: btrbk is running on source. exiting out of caution"
410 mexit 1
411 esac
412 fi
413 fi
414
415 if ! command -v btrbk &>/dev/null; then
416 die "error: no btrbk binary found"
417 fi
418
419 if ! $pull_reexec && [[ $source ]] && $pulla && ! $force ; then
420 ssh root@$source btrbk-run --check-installed || exit 1
421 fi
422
423 #### end pre-checks #####
424
425
426
427 mkdir -p /var/log/btrbk
428 # The journal doesnt go back to my oldest backups, and I've found myself
429 # wanting older logs. Not going to bother expiring old logs, since it is
430 # fine if they go back years.
431 log_path=/var/log/btrbk/$(date +%F_%T%:::z).log
432 echo copying output to $log_path
433 exec &> >(pee cat 'ts "%F %T"|dd of='$log_path' status=none')
434
435
436 if $verbose; then
437 printf " options: conf_only=%s\ndry_run=%s\nrate_limit=%s\nverbose=%s\ncmd_arg=%s" "$conf_only" "$dry_run" "$rate_limit" "$verbose" "$cmd_arg"
438 fi
439
440 if [[ -v targets ]]; then
441 echo "targets: ${targets[*]}"
442 fi
443
444 if [[ $source ]]; then
445 echo "source: $source"
446 fi
447
448 echo "mountpoints: ${mountpoints[*]}"
449
450
451
452
453 # pull_reexec stops us from getting into an infinite loop if there is some
454 # kind of weird problem
455 pulla=false
456 for m in "${mountpoints[@]}"; do
457 if [[ $m == /a ]]; then
458 pulla=true
459 break
460 fi
461 done
462
463 if ! $pull_reexec && [[ $source ]] && $pulla ; then
464 tmpf=$(mktemp)
465 m rsync -ra $source:/usr/local/bin/{mount-latest-subvol,check-subvol-stale} /usr/local/bin
466 m rsync -ra $source:/usr/local/lib/err /usr/local/lib
467 m scp $source:/a/bin/distro-setup/btrbk-run $tmpf
468 if ! diff -q $tmpf ${BASH_SOURCE[0]}; then
469 e "found different version on host $source. reexecing"
470 install -T $tmpf /usr/local/bin/btrbk-run
471 m /usr/local/bin/btrbk-run --pull-reexec "${orig_args[@]}"
472 mexit 0
473 fi
474 fi
475
476
477 if ! $fast; then
478 # if our mountpoints are from stale snapshots,
479 # it doesn't make sense to do a backup.
480 m check-subvol-stale ${mountpoints[@]} || die "found stale mountpoints in ${mountpoints[*]}"
481
482 # for an initial run, btrbk requires the dir to exist.
483 mkdir -p /mnt/{root,o}/btrbk
484 fi
485 local_zone=$(date +%z)
486
487 if [[ $source ]]; then
488 if $fast; then
489 zone=$local_zone
490 else
491 if ! zone=$(ssh root@$source date +%z); then
492 if $conf_only; then
493 echo "$0: warning: failed to ssh to root@$source"
494 else
495 die failed to ssh to root@$source
496 fi
497 fi
498 if [[ $zone != "$local_zone" ]]; then
499 die "error: dont confuse yourself with multiple time zones. $h has different timezone than localhost"
500 fi
501 fi
502 else
503
504 sshable=()
505 sshfail=()
506 for h in ${targets[@]}; do
507 if $fast || $conf_only; then
508 # Use some typical values in this case
509 root_size=$(( 1024 * 1024 * 2000 )) #2tb
510 percent_used=10
511 zone=$(date +%z)
512 elif remote_str=$(timeout -s 9 6 ssh root@$h "mkdir -p /mnt/root/btrbk /mnt/o/btrbk && date +%z && df --output=size,pcent / | tail -n1"); then
513 mapfile -t tmp_array <<<"$remote_str"
514 zone="${tmp_array[0]}"
515 IFS=" " read -r root_size percent_used <<<"${tmp_array[1]}"
516 percent_used=${percent_used%%%}
517
518 if (( ${#tmp_array[@]} != 2 )); then
519 die "error: didnt get 2 lines in test ssh to target $h. investigate"
520 fi
521 case $percent_used in
522 [0-9]|[1-9][0-9]) : ;;
523 *)
524 die "error: didnt get percent disk use in test ssh to target $h. investigate"
525 ;;
526 esac
527 else
528 sshfail+=($h)
529 continue
530 fi
531
532 # we may be booted into a bootstrap fs or something
533 min_root_kb=$(( 1024 * 1024 * 200 )) # 200 gb
534 if (( root_size < min_root_kb )); then
535 continue
536 fi
537
538 if (( percent_used >= 98 )); then
539 die "error: filesystem on target $h is $percent_used % full"
540 fi
541
542 # on sy, xprintidle is resetting every 12 seconds even when not
543 # idle, i dunno why, instead we are checking if the screen is locked,
544 # which is good enough.
545 #
546 # This is a separate ssh because the command can fail and thatis ok.
547 if ! $force; then
548 locked=false
549 if lock_info=$(timeout -s 9 6 ssh $h DISPLAY=:0 xscreensaver-command -time); then
550 if [[ $lock_info != *non-blanked* ]]; then
551 locked=true
552 fi
553 else
554 locked=true
555 fi
556 if ! $locked; then
557 # Ignore this host. i sometimes use a non-main machine for
558 # testing or web browsing, knowing that everything will be wiped
559 # by the next backup, but I dont want it to happen as Im using
560 # it from cronjob.
561 e "warning: $h: seems to be actively in use, skipping for now"
562 continue
563 fi
564 fi
565 sshable+=($h)
566 if [[ $zone != "$local_zone" ]]; then
567 die "error: dont confuse yourself with multiple time zones. $h has different timezone than localhost"
568 fi
569 done
570 if [[ ! ${sshable[*]} ]] || { $force && [[ ${sshfail[*]} ]]; }; then
571 die "failed to ssh to hosts: ${sshfail[*]}"
572 else
573 if [[ ${sshfail[*]} ]]; then
574 ret=1
575 e "error: failed to ssh to ${sshfail[*]} but continuing with other hosts"
576 fi
577 targets=(${sshable[@]})
578 fi
579 fi
580
581
582 cat >/etc/btrbk$conf_suf.conf <<EOF
583 ssh_identity /q/root/h
584 #ssh_identity /root/.ssh/home
585
586 # Just a guess that local7 is a good facility to pick.
587 # It's a bit odd that the transaction log has to be logged to
588 # a file or syslog, while other output is sent to std out.
589 # The man does not mention a way for them to be together, but
590 # I dunno if setting a log level like warn might also output
591 # transaction info.
592 transaction_syslog local7
593
594 # trying this out
595 #stream_compress zstd
596
597 # so we only run one at a time
598 lockfile /var/lock/btrbk$conf_suf.lock
599
600 # default format of short does not accomidate hourly preservation setting
601 timestamp_format long-iso
602
603 # only make a snapshot if things have changed
604 snapshot_create onchange
605 # I could make this different from target_preserve,
606 # if one disk had less space.
607 # for now, keeping them equal.
608 snapshot_preserve $std_preserve
609 snapshot_preserve_min 6h
610 snapshot_dir btrbk
611 # so, total backups = ~58
612 target_preserve $std_preserve
613 target_preserve_min 6h
614
615 # i tried this when investigating: clone no source subvolume found error
616 #incremental_prefs sro:1 srn:1 sao san:1 aro:1 arn:1
617
618 # if something fails and it's not obvious, try doing
619 # btrbk -l debug -v dryrun
620
621 rate_limit $rate_limit
622 EOF
623
624 if $incremental_strict; then
625 cat >>/etc/btrbk$conf_suf.conf <<EOF
626 incremental strict
627 EOF
628 fi
629
630 qconf() {
631 case $sub in
632 q)
633 # q has sensitive data i dont want to backup for so long
634 cat >>/etc/btrbk$conf_suf.conf <<EOF
635 snapshot_preserve $q_preserve
636 snapshot_preserve_min 2h
637 snapshot_dir btrbk
638 target_preserve $q_preserve
639 target_preserve_min 2h
640 EOF
641 ;;
642 esac
643
644 }
645
646 # make /q be last
647 mp_count=${#mountpoints[@]}
648 for (( i=0; i < mp_count - 1 ; i++ )); do
649 if [[ ${mountpoints[i]} == /q ]]; then
650 unset "mountpoints[i]"
651 mountpoints+=(/q)
652 fi
653 done
654
655 for m in ${mountpoints[@]}; do
656 case $m in
657 /o)
658 vol=/mnt/o
659 ;;
660 *)
661 vol=/mnt/root
662 ;;
663 esac
664
665 sub=${m#/}
666 if [[ $source ]]; then
667 cat >>/etc/btrbk$conf_suf.conf <<EOF
668 volume ssh://$bbksource$vol
669 subvolume $sub
670 EOF
671 qconf
672 cat >>/etc/btrbk$conf_suf.conf <<EOF
673 target send-receive $vol/btrbk
674 EOF
675 fi
676 if (( ${#targets[@]} )); then
677 cat >>/etc/btrbk$conf_suf.conf <<EOF
678 volume $vol
679 subvolume $sub
680 EOF
681 qconf
682 for tg in ${targets[@]}; do
683 # handle ipv6
684 if [[ $tg == *:* ]]; then
685 tg="[$tg]"
686 fi
687 cat >>/etc/btrbk$conf_suf.conf <<EOF
688 target send-receive ssh://$tg$vol/btrbk
689 EOF
690 done
691 fi
692 done
693
694 # todo: umount first to ensure we don't have any errors
695 # todo: do some kill fuser stuff to make umount more reliable
696
697
698 if $conf_only; then
699 mexit 0
700 fi
701
702
703
704 if $dry_run; then
705 m btrbk -c /etc/btrbk$conf_suf.conf -v -n $cmd_arg
706 mexit 0
707 fi
708 # -q and just using the syslog option seemed nice,
709 # but it doesn't show when a send has a parent and when it doesn't.
710 m btrbk -c /etc/btrbk$conf_suf.conf $preserve_arg $verbose_arg $progress_arg $cmd_arg
711
712 if $early; then
713 exit 0
714 fi
715
716 # todo: tp not valid anymore.
717 # if we have it, sync to systems which don't
718 if mountpoint $rsync_mountpoint >/dev/null; then
719 for tg in ${targets[@]}; do
720 case $tg in
721 tp)
722 dirs=(/p/c/machine_specific/tp)
723 for x in /p/c/machine_specific/*.hosts; do
724 if grep -qxF $tg $x; then
725 dirs+=(${x%.hosts})
726 fi
727 done
728 m rsync -aSAXPH --specials --devices --delete --relative ${dirs[@]} root@$tg:/
729 ;;
730 esac
731 done
732 fi
733
734 subvols=()
735 for mp in "${mountpoints[@]}"; do
736 subvols+=("${mp##*/}")
737 done
738 if [[ $source ]]; then
739 m mount-latest-subvol "${subvols[@]}"
740 else
741 for tg in ${targets[@]}; do
742 m /a/exe/mount-latest-remote "$tg" "${subvols[@]}" || ret=$?
743 done
744 fi
745
746 if [[ $ret == 0 ]]; then
747 for tg in ${targets[@]}; do
748 h=$(ssh $tg hostname)
749 if [[ $h == kd && $HOSTNAME == x3 && $HOSTNAME == "$MAIL_HOST" ]]; then
750 ssh root$tg systemctl --no-block start btrbk-spread
751 fi
752 rsync -a -f"- */" -f"+ *" /var/log/btrbk/ root@$tg:/var/log/btrbk/$tg
753 ssh root@$tg /usr/local/bin/mail-backup-clean
754 done
755 if [[ $source ]]; then
756 rsync -a -f"- */" -f"+ *" $source:/var/log/btrbk/ /var/log/btrbk/$source
757 fi
758 fi
759
760 mexit $ret
761
762 # todo: move variable data we don't care about backing up
763 # to /nocow and symlink it.
764
765
766 # background on btrbk timezones. with short/long, timestamps use local time.
767 # for long, if your local time moves backwards, by moving timezones or
768 # for an hour when daylight savings changes it, you will temporarily get
769 # a more aggressive retention policy for the overlapping period, and
770 # vice versa for the opposite timezone move. The alternative is using
771 # long-iso, which puts timezone info into the timestamp, which means
772 # that instead of shifting time, you shift the start of day/week/month
773 # which is used for retention to your new local time, which means for
774 # example, if you moved forward by 8 hours, the daily/weekly/monthly
775 # retention will be 8 hours more aggressive since midnight is at a new
776 # time, unless you fake the timzeone using the TZ env variable.
777 # However, in the short term, there will be no inconsistencies.
778 # I don't see any problem with shifting when the day starts for
779 # retention, so I'm using long-iso.
780
781 # note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z