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