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