2 # Copyright (C) 2016 Ian Kelling
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
23 [[ $EUID == 0 ]] ||
exec sudo
-E "${BASH_SOURCE[0]}" "$@"
25 source /usr
/local
/lib
/err
30 usually -t TARGET_HOST or -s SOURCE_HOST
32 Note, at source location, intentionally not executable, run and read
36 echo "top of script file:"
37 sed -n '1,/^[# ]*end command line/{p;b};q' "$0"
44 m
() { if $verbose; then printf "$pre%s\n" "$*"; fi; "$@"; }
45 e
() { printf "$pre%s\n" "$*"; }
46 die
() { printf "$pre%s\n" "$*" >&2; echo "exiting with status 1" >&2; exit 1; }
47 mexit
() { echo "$pre: exiting with status $1"; exit $1; }
50 if [[ -e /b
/bash_unpublished
/source-state
]]; then
51 source /b
/bash_unpublished
/source-state
54 # note q is owned by root:1000
63 dry_run
=false
# mostly for testing
65 verbose
=true
; verbose_arg
=-v
66 progress_arg
="--progress"
67 incremental_strict
=false
70 default_args_file
=/etc
/btrbk-run.conf
71 if [[ -s $default_args_file ]]; then
72 set -- $
(< $default_args_file) "$@"
73 # i havent used this feature yet, so warn about it
74 echo "$0: warning: default btrbk-run options set in $default_args_file (sleeping 5 seconds):"
75 cat $default_args_file
83 temp
=$
(getopt
-l cron
,pull-reexec
,help ceil
:m
:npqrs
:t
:vh
"$@") || usage
1
87 # some behaviors specific to running under cron:
88 # - skip hosts where xprintidle haven't been idle recently
89 # - if we can't ssh to 1 or more hosts, still do the rest
90 # - if we aren't MAIL_HOST and no -m or -s, just exit
96 # only creates the config file, does not run btrbk
97 -c) conf_only
=true
; shift ;;
98 # quit early, just btrbk, no extra remounting etc.
99 -e) early
=true
; shift ;;
100 -i) incremental_strict
=true
; shift ;;
101 # bytes per second, suffix k m g
102 -l) rate_limit
=$2; shift 2 ;;
103 # Comma separated mountpoints to backup. This has defaults set below.
104 -m) IFS
=, mountpoints
=($2); unset IFS
; shift 2 ;;
105 -n) dry_run
=true
; dry_run_arg
=-n; shift ;;
107 -p) progress_arg
="--progress"; shift ;;
108 # internal option for rerunning under newer SOURCE_HOST version.
109 --pull-reexec) pull_reexec
=true
; shift ;;
111 -q) verbose
=false
; verbose_arg
=; progress_arg
=; shift ;;
112 # source host to receive a backup from
116 if [[ $source == *:* ]]; then
117 bbksource
="[$source]"
121 # target hosts to send to. empty is valid for just doing local
122 # snapshot. we have default hosts we will populate.
123 -t) IFS
=, targets
=($2); unset IFS
; shift 2 ;;
125 -v) verbose
=true
; verbose_arg
=-v; shift ;;
128 *) die
"Internal error!" ;;
132 # only tested commands are resume and archive
136 std_preserve
="18h 14d 8w 24m"
140 run|resume|archive
) : ;;
144 std_preserve
="999h 999d 999w 999m"
145 q_preserve
="$std_preserve"
148 *) die
"untested command arg" ;;
151 if (( $# > 1 )); then
152 die
: "only 1 nonoption arg is supported"
155 if [[ -v targets
&& $source ]]; then
156 # note, this doesnt need to be the case, but
157 # we would need to think about it.
158 die
"error: -t and -s are mutually exclusive"
162 printf "options: conf_only=%s\ndry_run=%s\nrate_limit=%s\nverbose=%s\ncmd_arg=%s" "$conf_only" "$dry_run" "$rate_limit" "$verbose" "$cmd_arg"
164 ### end options parsing
168 # remove path from earlier version of btrbk
169 rm -f /usr
/sbin
/btrbk
170 # note, this still works as intended if there is no /usr/bin/btrbk
171 if [[ /a
/opt
/btrbk
/btrbk
-nt /usr
/bin
/btrbk
]]; then
172 if [[ -e /b
/distro-functions
/src
/package-manager-abstractions
]]; then
173 .
/b
/distro-functions
/src
/package-manager-abstractions
180 # TODO: i wonder if there should be an option to send to the default
181 # targets, plus any given on the command line.
185 # set default targets
186 if [[ ! -v targets
&& ! $source ]]; then
187 if [[ $HOSTNAME != "$MAIL_HOST" ]] && $cron ; then
188 echo "MAIL_HOST=$MAIL_HOST, nothing to do"
196 targets
=(frodo.b8.nz
)
202 if ping -q -c1 -w1 hal.office.fsf.org \
203 && ip n show
192.168.0.26 |
grep .
&>/dev
/null
; then
209 if ping -q -c1 -w1 iank.vpn.office.fsf.org
&>/dev
/null
; then
210 home
=iank.vpn.office.fsf.org
219 targets
+=($home x2.office.fsf.org
)
224 targets
+=(x2.office.fsf.org x2.b8.nz
)
226 targets
+=(x2wg.b8.nz
)
230 targets
+=(x2wg.b8.nz x3.b8.nz
)
231 if ping -q -c1 -w1 sy.b8.nz
&>/dev
/null
; then
242 die
"error: no default targets for this host, use -t"
247 if [[ -v targets
]]; then
248 echo "targets: ${targets[*]}"
251 if [[ $source ]]; then
252 echo "source: $source"
255 if [[ ${mountpoints[0]} ]]; then
256 for mp
in ${mountpoints[@]}; do
257 if [[ -e /nocow
/btrfs-stale
/$mp ]]; then
258 die
"error: $mp is stale, mount-latest-subvol first"
262 # set default mountpoints
263 if [[ ${targets[0]} == tp
]]; then
272 if [[ $source ]]; then
273 source_state
="$(ssh $source cat /a/bin/bash_unpublished/source-state)"
275 source_host
="$(ssh $source cat /etc/hostname)"
276 if [[ $source_host == "$MAIL_HOST" ]]; then
277 prospective_mps
+=(/o
)
280 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
281 # HOST2 is really the mail host if it exists
282 if [[ $HOST2 && $HOST2 != "$HOSTNAME" ]]; then
283 echo "skipping /o because HOST2 is not us"
285 prospective_mps
+=(/o
)
289 # note: put q last just in case its specific retention options were to
290 # affect other config sections. I havent tested if that is the case.
291 prospective_mps
+=(/a
/ar /qr
/q
)
295 for mp
in ${prospective_mps[@]}; do # default mountpoints to sync
296 if [[ -e /nocow
/btrfs-stale
/$mp ]]; then
297 e
"warning: $mp stale, not adding to default mountpoints"
300 if awk '{print $2}' /etc
/fstab |
grep -xF $mp &>/dev
/null
; then
306 echo "mountpoints: ${mountpoints[*]}"
308 ##### end command line parsing ########
310 if [[ $source ]]; then
311 if [[ $
(ssh $source ps
--no-headers -o comm 1) == systemd
]]; then
312 status
=$
(ssh $source systemctl is-active btrbk.service
) ||
: # normally returns 3
314 inactive|failed
) : ;;
316 echo "$0: error: cron btrbk is running on source. exiting out of caution"
322 # pull_reexec stops us from getting into an infinite loop if there is some
323 # kind of weird problem
325 for m
in "${mountpoints[@]}"; do
326 if [[ $m == /a
]]; then
331 if ! $pull_reexec && [[ $source ]] && $pulla ; then
333 m rsync
-ra $source:/usr
/local
/bin
/{mount-latest-subvol
,check-subvol-stale
} /usr
/local
/bin
334 m rsync
-ra $source:/usr
/local
/lib
/err
/usr
/local
/lib
335 m scp
$source:/a
/bin
/distro-setup
/btrbk-run
$tmpf
336 if ! diff -q $tmpf ${BASH_SOURCE[0]}; then
337 e
"found different version on host $source. reexecing"
338 install -T $tmpf /usr
/local
/bin
/btrbk-run
339 m
/usr
/local
/bin
/btrbk-run
--pull-reexec "${orig_args[@]}"
345 if ! command -v btrbk
&>/dev
/null
; then
346 die
"error: no btrbk binary found"
348 # if our mountpoints are from stale snapshots,
349 # it doesn't make sense to do a backup.
350 check-subvol-stale
${mountpoints[@]} || die
"found stale mountpoints in ${mountpoints[*]}"
352 # for an initial run, btrbk requires the dir to exist.
353 mkdir
-p /mnt
/{root
,o
}/btrbk
354 local_zone
=$
(date +%z
)
356 if [[ $source ]]; then
357 if ! zone
=$
(ssh root@
$source date +%z
); then
358 die failed to
ssh to root@
$source
360 if [[ $zone != "$local_zone" ]]; then
361 die
"error: dont confuse yourself with multiple time zones. $h has different timezone than localhost"
368 min_idle_ms
=$
((1000 * 60 * 15))
369 for h
in ${targets[@]}; do
370 if remote_info
=( $
(timeout
-s 9 6 ssh root@
$h "mkdir -p /mnt/root/btrbk /mnt/o/btrbk && date +%z && df --output=size,pcent / | tail -n1") ); then
371 zone
=${remote_info[0]}
372 root_size
=${remote_info[1]}
373 percent_used
=${remote_info[2]%%%}
375 if (( ${#remote_info[@]} != 3 )); then
376 die
"error: didnt get 3 fields in test ssh to target $h. investigate"
380 # we may be booted into a bootstrap fs or something
381 min_root_kb
=$
(( 1024 * 1024 * 200 )) # 200 gb
382 if (( root_size
< min_root_kb
)); then
386 if (( percent_used
>= 98 )); then
387 die
"error: filesystem on target $h is $percent_used % full"
390 # This is a separate ssh because xprintidle can fail and thats ok.
391 if $cron && idle_ms
=$
(timeout
-s 9 6 ssh $h DISPLAY
=:0 xprintidle
); then
392 if (( idle_ms
< min_idle_ms
)); then
394 # Ignore this host. i sometimes use a non-main machine for
395 # testing or web browsing, knowing that everything will be wiped
396 # by the next backup, but I dont want it to happen as Im using
398 e
"warning: $h: active X session in the last 15 minutes, skipping for now"
403 if [[ $zone != "$local_zone" ]]; then
404 die
"error: dont confuse yourself with multiple time zones. $h has different timezone than localhost"
410 if [[ ! ${sshable[*]} ]] || { ! $cron && [[ ${sshfail[*]} ]]; }; then
411 die
"failed to ssh to hosts: ${sshfail[*]}"
413 if [[ ${sshfail[*]} ]]; then
415 e
"error: failed to ssh to ${sshfail[*]} but continuing with other hosts"
417 targets
=(${sshable[@]})
422 cat >/etc
/btrbk.conf
<<EOF
423 ssh_identity /q/root/h
424 #ssh_identity /root/.ssh/home
426 # Just a guess that local7 is a good facility to pick.
427 # It's a bit odd that the transaction log has to be logged to
428 # a file or syslog, while other output is sent to std out.
429 # The man does not mention a way for them to be together, but
430 # I dunno if setting a log level like warn might also output
432 transaction_syslog local7
437 # so we only run one at a time
438 lockfile /var/lock/btrbk.lock
440 # default format of short does not accomidate hourly preservation setting
441 timestamp_format long-iso
443 # only make a snapshot if things have changed
444 snapshot_create onchange
445 # I could make this different from target_preserve,
446 # if one disk had less space.
447 # for now, keeping them equal.
448 snapshot_preserve $std_preserve
449 snapshot_preserve_min 2h
451 # so, total backups = ~58
452 target_preserve $std_preserve
453 target_preserve_min 2h
455 # if something fails and it's not obvious, try doing
456 # btrbk -l debug -v dryrun
458 rate_limit $rate_limit
461 if $incremental_strict; then
462 cat >>/etc
/btrbk.conf
<<EOF
467 for m
in ${mountpoints[@]}; do
480 if [[ $source ]]; then
481 cat >>/etc
/btrbk.conf
<<EOF
482 volume ssh://$bbksource$vol
484 target send-receive $vol/btrbk
487 if (( ${#targets[@]} )); then
488 cat >>/etc
/btrbk.conf
<<EOF
494 # q has sensitive data i dont want to backup for so long
495 cat >>/etc
/btrbk.conf
<<EOF
496 snapshot_preserve $std_preserve
497 snapshot_preserve_min 2h
499 target_preserve $std_preserve
500 target_preserve_min 2h
504 for tg
in ${targets[@]}; do
506 if [[ $tg == *:* ]]; then
509 cat >>/etc
/btrbk.conf
<<EOF
510 target send-receive ssh://$tg$vol/btrbk
517 # # to manually backup amy,
518 # # bbk -e -s amy -m root_ubuntubionic
519 # cat >>/etc/btrbk.conf <<'EOF'
520 # volume ssh://amy/mnt/root
521 # subvolume root_ubuntubionic
522 # target send-receive /mnt/root/btrbk
526 # todo: umount first to ensure we don't have any errors
527 # todo: do some kill fuser stuff to make umount more reliable
537 m btrbk
-v -n $cmd_arg
539 elif [[ $cmd_arg == archive
]]; then
540 if [[ $source ]]; then
541 for vol
in ${!vols[@]}; do
542 m btrbk
$verbose_arg $progress_arg $cmd_arg ssh://$source$vol $vol
545 for tg
in ${targets[@]}; do
546 for vol
in ${!vols[@]}; do
547 m btrbk
$verbose_arg $progress_arg $cmd_arg $vol ssh://$tg$vol
553 # -q and just using the syslog option seemed nice,
554 # but it doesn't show when a send has a parent and when it doesn't.
555 m btrbk
$preserve_arg $verbose_arg $progress_arg $cmd_arg
557 # todo: tp not valid anymore.
558 # if we have it, sync to systems which don't
559 if mountpoint
$rsync_mountpoint >/dev
/null
; then
560 for tg
in ${targets[@]}; do
563 dirs=(/p
/c
/machine_specific
/tp
)
564 for x
in /p
/c
/machine_specific
/*.hosts
; do
565 if grep -qxF $tg $x; then
569 m rsync
-aSAXPH --specials --devices --delete --relative ${dirs[@]} root@
$tg:/
575 if [[ $source ]]; then
576 m mount-latest-subvol
578 m
/a
/exe
/mount-latest-remote
${targets[@]}
581 if [[ $ret == 0 ]]; then
582 for tg
in ${targets[@]}; do
584 #ssh root@$tg /a/exe/mail-backup-clean
590 # todo: move variable data we don't care about backing up
591 # to /nocow and symlink it.
594 # background on btrbk timezones. with short/long, timestamps use local time.
595 # for long, if your local time moves backwards, by moving timezones or
596 # for an hour when daylight savings changes it, you will temporarily get
597 # a more aggressive retention policy for the overlapping period, and
598 # vice versa for the opposite timezone move. The alternative is using
599 # long-iso, which puts timezone info into the timestamp, which means
600 # that instead of shifting time, you shift the start of day/week/month
601 # which is used for retention to your new local time, which means for
602 # example, if you moved forward by 8 hours, the daily/weekly/monthly
603 # retention will be 8 hours more aggressive since midnight is at a new
604 # time, unless you fake the timzeone using the TZ env variable.
605 # However, in the short term, there will be no inconsistencies.
606 # I don't see any problem with shifting when the day starts for
607 # retention, so I'm using long-iso.
609 # note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z