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