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