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
79 early
=false
# quit early, just btrbk, no extra remounting etc.
82 temp
=$
(getopt
-l cron
,pull-reexec
,help ceil
:m
:npqs
:t
:vh
"$@") || usage
1
91 # only creates the config file, does not run btrbk
92 -c) conf_only
=true
; shift ;;
93 -e) early
=true
; shift ;;
94 -i) incremental_strict
=true
; shift ;;
95 # bytes per second, suffix k m g
96 -l) rate_limit
=$2; shift 2 ;;
97 # Comma separated mountpoints to backup. This has defaults set below.
98 -m) IFS
=, mountpoints
=($2); unset IFS
; shift 2 ;;
99 -n) dry_run
=true
; dry_run_arg
=-n; shift ;;
100 -p) progress_arg
="--progress"; shift ;;
101 --pull-reexec) pull_reexec
=true
; shift ;;
102 -q) verbose
=false
; verbose_arg
=; progress_arg
=; shift ;;
103 # source host to receive a backup from
107 if [[ $source == *:* ]]; then
108 bbksource
="[$source]"
112 # target hosts to send to. empty is valid for just doing local
113 # snapshot. we have default hosts we will populate.
114 -t) IFS
=, targets
=($2); unset IFS
; shift 2 ;;
115 -v) verbose
=true
; verbose_arg
=-v; shift ;;
118 *) die
"Internal error!" ;;
122 # only tested commands are resume and archive
126 run|resume|archive
) : ;;
127 *) die
"untested command arg" ;;
130 if (( $# > 1 )); then
131 die
: "only 1 nonoption arg is supported"
134 if [[ -v targets
&& $source ]]; then
135 # note, this doesnt need to be the case, but
136 # we would need to think about it.
137 die
"error: -t and -s are mutually exclusive"
141 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"
143 ### end options parsing
145 # remove path from earlier version of btrbk
146 rm -f /usr
/sbin
/btrbk
147 # note, this still works as intended if there is no /usr/bin/btrbk
148 if [[ /a
/opt
/btrbk
/btrbk
-nt /usr
/bin
/btrbk
]]; then
149 if [[ -e /b
/distro-functions
/src
/package-manager-abstractions
]]; then
150 .
/b
/distro-functions
/src
/package-manager-abstractions
157 # TODO: i wonder if there should be an option to send to the default
158 # targets, plus any given on the command line.
162 # set default targets
163 if [[ ! -v targets
&& ! $source ]]; then
164 if [[ $HOSTNAME != "$MAIL_HOST" ]] && $cron ; then
165 echo "MAIL_HOST=$MAIL_HOST, nothing to do"
173 targets
=(frodo.b8.nz
)
179 if ping -q -c1 -w1 hal.office.fsf.org \
180 && ip n show
192.168.0.26 |
grep .
&>/dev
/null
; then
186 if ping -q -c1 -w1 iank.vpn.office.fsf.org
&>/dev
/null
; then
187 home
=iank.vpn.office.fsf.org
201 targets
+=(kw.office.fsf.org x2.b8.nz
)
207 targets
+=(x2.b8.nz kw.b8.nz
)
208 # temporarily disabled while doing maint
209 # if ping -q -c1 -w1 bo.b8.nz &>/dev/null; then
210 # targets+=(bo.b8.nz)
212 # targets+=(bow.b8.nz)
220 die
"error: no default targets for this host, use -t"
225 if [[ -v targets
]]; then
226 echo "targets: ${targets[*]}"
229 if [[ $source ]]; then
230 echo "source: $source"
233 if [[ ${mountpoints[0]} ]]; then
234 for mp
in ${mountpoints[@]}; do
235 if [[ -e /nocow
/btrfs-stale
/$mp ]]; then
236 die
"error: $mp is stale, mount-latest-subvol first"
240 # set default mountpoints
241 if [[ ${targets[0]} == tp
]]; then
249 prospective_mps
=(/a
/q
)
250 if [[ $source ]]; then
251 source_state
="$(ssh $source cat /a/bin/bash_unpublished/source-state)"
253 source_host
="$(ssh $source cat /etc/hostname)"
254 if [[ $source_host == "$MAIL_HOST" ]]; then
255 prospective_mps
+=(/o
)
258 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
259 # HOST2 is really the mail host if it exists
260 if [[ $HOST2 && $HOST2 != "$HOSTNAME" ]]; then
261 echo "skipping /o because HOST2 is not us"
263 prospective_mps
+=(/o
)
270 for mp
in ${prospective_mps[@]}; do # default mountpoints to sync
271 if [[ -e /nocow
/btrfs-stale
/$mp ]]; then
272 e
"warning: $mp stale, not adding to default mountpoints"
275 if awk '{print $2}' /etc
/fstab |
grep -xF $mp &>/dev
/null
; then
281 echo "mountpoints: ${mountpoints[*]}"
283 ##### end command line parsing ########
285 if [[ $source ]]; then
286 if [[ $
(ssh $source ps
--no-headers -o comm 1) == systemd
]]; then
287 status
=$
(ssh $source systemctl is-active btrbk.service
) ||
: # normally returns 3
289 inactive|failed
) : ;;
291 echo "$0: error: cron btrbk is running on source. exiting out of caution"
297 # pull_reexec stops us from getting into an infinite loop if there is some
298 # kind of weird problem
300 for m
in "${mountpoints[@]}"; do
301 if [[ $m == /a
]]; then
306 if ! $pull_reexec && [[ $source ]] && $pulla ; then
308 m rsync
-ra $source:/usr
/local
/bin
/{mount-latest-subvol
,check-subvol-stale
} /usr
/local
/bin
309 m rsync
-ra $source:/usr
/local
/lib
/err
/usr
/local
/lib
310 m scp
$source:/a
/bin
/distro-setup
/btrbk-run
$tmpf
311 if ! diff -q $tmpf ${BASH_SOURCE[0]}; then
312 e
"found different version on host $source. reexecing"
313 install -T $tmpf /usr
/local
/bin
/btrbk-run
314 m
/usr
/local
/bin
/btrbk-run
--pull-reexec "${orig_args[@]}"
320 if ! which btrbk
&>/dev
/null
; then
321 die
"error: no btrbk binary found"
323 # if our mountpoints are from stale snapshots,
324 # it doesn't make sense to do a backup.
325 check-subvol-stale
${mountpoints[@]} || die
"found stale mountpoints in ${mountpoints[*]}"
327 # for an initial run, btrbk requires the dir to exist.
328 mkdir
-p /mnt
/root
/btrbk
329 local_zone
=$
(date +%z
)
331 if [[ $source ]]; then
332 if ! zone
=$
(ssh root@
$source date +%z
); then
333 die failed to
ssh to root@
$source
335 if [[ $zone != "$local_zone" ]]; then
336 die
"error: dont confuse yourself with multiple time zones. $h has different timezone than localhost"
343 min_idle_ms
=$
((1000 * 60 * 15))
344 for h
in ${targets[@]}; do
345 if remote_info
=( $
(timeout
-s 9 6 ssh root@
$h "mkdir -p /mnt/root/btrbk && date +%z && df --output=size,pcent / | tail -n1") ); then
346 zone
=${remote_info[0]}
347 root_size
=${remote_info[1]}
348 percent_used
=${remote_info[2]%%%}
350 if (( ${#remote_info[@]} != 3 )); then
351 die
"error: didnt get 3 fields in test ssh to target $h. investigate"
355 # we may be booted into a bootstrap fs or something
356 min_root_kb
=$
(( 1024 * 1024 * 200 )) # 200 gb
357 if (( root_size
< min_root_kb
)); then
361 if (( percent_used
>= 98 )); then
362 die
"error: filesystem on target $h is $percent_used % full"
365 # This is a separate ssh because xprintidle can fail and thats ok.
366 if $cron && idle_ms
=$
(timeout
-s 9 6 ssh $h DISPLAY
=:0 xprintidle
); then
367 if (( idle_ms
< min_idle_ms
)); then
369 # Ignore this host. i sometimes use a non-main machine for
370 # testing or web browsing, knowing that everything will be wiped
371 # by the next backup, but I dont want it to happen as Im using
373 e
"warning: $h: active X session in the last 15 minutes, skipping for now"
378 if [[ $zone != "$local_zone" ]]; then
379 die
"error: dont confuse yourself with multiple time zones. $h has different timezone than localhost"
385 if [[ ! ${sshable[*]} ]] || { ! $cron && [[ ${sshfail[*]} ]]; }; then
386 die
"failed to ssh to hosts: ${sshfail[*]}"
388 if [[ ${sshfail[*]} ]]; then
390 e
"error: failed to ssh to ${sshfail[*]} but continuing with other hosts"
392 targets
=(${sshable[@]})
397 cat >/etc
/btrbk.conf
<<EOF
398 ssh_identity /q/root/h
399 #ssh_identity /root/.ssh/home
401 # Just a guess that local7 is a good facility to pick.
402 # It's a bit odd that the transaction log has to be logged to
403 # a file or syslog, while other output is sent to std out.
404 # The man does not mention a way for them to be together, but
405 # I dunno if setting a log level like warn might also output
407 transaction_syslog local7
412 # so we only run one at a time
413 lockfile /var/lock/btrbk.lock
415 # default format of short does not accomidate hourly preservation setting
416 timestamp_format long-iso
418 # only make a snapshot if things have changed
419 snapshot_create onchange
420 # I could make this different from target_preserve,
421 # if one disk had less space.
422 # for now, keeping them equal.
423 snapshot_preserve 18h 14d 8w 24m
424 snapshot_preserve_min 2h
427 # so, total backups = ~58
428 target_preserve 18h 14d 8w 24m
429 target_preserve_min 2h
431 # if something fails and it's not obvious, try doing
432 # btrbk -l debug -v dryrun
434 rate_limit $rate_limit
437 if $incremental_strict; then
438 cat >>/etc
/btrbk.conf
<<EOF
444 for m
in ${mountpoints[@]}; do
446 if [[ $source ]]; then
447 cat >>/etc
/btrbk.conf
<<EOF
448 volume ssh://$bbksource$vol
450 target send-receive $vol/btrbk
453 if (( ${#targets[@]} )); then
454 cat >>/etc
/btrbk.conf
<<EOF
458 for tg
in ${targets[@]}; do
460 if [[ $tg == *:* ]]; then
463 cat >>/etc
/btrbk.conf
<<EOF
464 target send-receive ssh://$tg$vol/btrbk
471 # # to manually backup amy,
472 # # bbk -e -s amy -m root_ubuntubionic
473 # cat >>/etc/btrbk.conf <<'EOF'
474 # volume ssh://amy/mnt/root
475 # subvolume root_ubuntubionic
476 # target send-receive /mnt/root/btrbk
480 # todo: umount first to ensure we don't have any errors
481 # todo: do some kill fuser stuff to make umount more reliable
491 m btrbk
-v -n $cmd_arg
493 elif [[ $cmd_arg == archive
]]; then
494 if [[ $source ]]; then
495 m btrbk
$verbose_arg $progress_arg $cmd_arg ssh://$source$vol $vol
497 for tg
in ${targets[@]}; do
498 m btrbk
$verbose_arg $progress_arg $cmd_arg $vol ssh://$tg$vol
503 # -q and just using the syslog option seemed nice,
504 # but it doesn't show when a send has a parent and when it doesn't.
505 m btrbk
$verbose_arg $progress_arg $cmd_arg
507 # todo: tp not valid anymore.
508 # if we have it, sync to systems which don't
509 if mountpoint
$rsync_mountpoint >/dev
/null
; then
510 for tg
in ${targets[@]}; do
513 dirs=(/p
/c
/machine_specific
/tp
)
514 for x
in /p
/c
/machine_specific
/*.hosts
; do
515 if grep -qxF $tg $x; then
519 m rsync
-aSAXPH --specials --devices --delete --relative ${dirs[@]} root@
$tg:/
525 if [[ $source ]]; then
526 m mount-latest-subvol
528 m
/a
/exe
/mount-latest-remote
${targets[@]}
531 if [[ $ret == 0 ]]; then
532 for tg
in ${targets[@]}; do
534 #ssh root@$tg /a/exe/mail-backup-clean
540 # todo: move variable data we don't care about backing up
541 # to /nocow and symlink it.
544 # background on btrbk timezones. with short/long, timestamps use local time.
545 # for long, if your local time moves backwards, by moving timezones or
546 # for an hour when daylight savings changes it, you will temporarily get
547 # a more aggressive retention policy for the overlapping period, and
548 # vice versa for the opposite timezone move. The alternative is using
549 # long-iso, which puts timezone info into the timestamp, which means
550 # that instead of shifting time, you shift the start of day/week/month
551 # which is used for retention to your new local time, which means for
552 # example, if you moved forward by 8 hours, the daily/weekly/monthly
553 # retention will be 8 hours more aggressive since midnight is at a new
554 # time, unless you fake the timzeone using the TZ env variable.
555 # However, in the short term, there will be no inconsistencies.
556 # I don't see any problem with shifting when the day starts for
557 # retention, so I'm using long-iso.
559 # note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z