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 immediately, the received subvolume doesn't get a Received UUID: field, and we won't mount it. Need to figure out a solution that will fix this.
20 local -i argc_index
=0 frame i start
=${1:-0} max_indent
=8 indent
23 if [[ $
(shopt -p extdebug
) == *-s* ]]; then
26 for ((frame
=0; frame
< ${#FUNCNAME[@]}-1; frame
++)); do
27 argc
=${BASH_ARGC[frame]}
29 ((frame
< start
)) && continue
30 if (( ${#BASH_SOURCE[@]} > 1 )); then
31 source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:"
33 indent
=$
((frame-start
+ 1))
34 indent
=$
((indent
< max_indent ? indent
: max_indent
))
35 printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}"
37 for ((i
=argc_index-1
; i
>= argc_index-argc
; i--
)); do
38 printf " %s" "${BASH_ARGV[i]}"
46 set -E; shopt -s extdebug
51 echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: \`$BASH_COMMAND' returned $err"
53 set -e # err trap does not work within an error trap
54 "${_errcatch_cleanup[@]:-:}" # note :-: is to be compatible with set -u
55 echo "$0: exiting with code $err"
64 [[ $EUID == 0 ]] ||
exec sudo
-E "$BASH_SOURCE" "$@"
70 usually -t TARGET_HOST or -s SOURCE_HOST
72 Note, at source location, intentionally not executable, run and read
76 echo "top of script file:"
77 sed -n '1,/^[# ]*end command line/{p;b};q' "$0"
84 m rsync
$dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
88 m
() { if $verbose; then printf "$pre %s\n" "$*"; fi; "$@"; }
89 die
() { printf "$pre %s\n" "$*" >&2; exit 1; }
92 if [[ -e /b
/bash_unpublished
/source-state
]]; then
93 source /b
/bash_unpublished
/source-state
96 # note q is owned by root:1000
104 dry_run
=false
# mostly for testing
106 verbose
=true
; verbose_arg
=-v
107 progress_arg
="--progress"
110 default_args_file
=/etc
/btrbk-run.conf
111 if [[ -s $default_args_file ]]; then
112 set -- $
(< $default_args_file) "$@"
113 # i havent used this feature yet, so warn about it
114 echo "$0: warning: default btrbk-run options set in $default_args_file (sleeping 5 seconds):"
115 cat $default_args_file
122 temp
=$
(getopt
-l cron
,pull-reexec
,help cl
:m
:npqs
:t
:vh
"$@") || usage
1
131 # only creates the config file, does not run btrbk
132 -c) conf_only
=true
; shift ;;
133 # bytes per second, suffix k m g
134 -l) rate_limit
=$2; shift 2 ;;
135 # Comma separated mountpoints to backup. This has defaults set below.
136 -m) IFS
=, mountpoints
=($2); unset IFS
; shift 2 ;;
137 -n) dry_run
=true
; dry_run_arg
=-n; shift ;;
138 -p) progress_arg
="--progress"; shift ;;
139 --pull-reexec) pull_reexec
=true
; shift ;;
140 -q) verbose
=false
; verbose_arg
=; progress_arg
=; shift ;;
141 # source host to receive a backup from
142 -s) source=$2; shift 2 ;;
143 # target hosts to send to. empty is valid for just doing local
144 # snapshot. we have default hosts we will populate.
145 -t) IFS
=, targets
=($2); unset IFS
; shift 2 ;;
146 -v) verbose
=true
; verbose_arg
=-v; shift ;;
149 *) die
"Internal error!" ;;
153 # only tested commands are resume and archive
157 run|resume|archive
) : ;;
158 *) die
"untested command arg" ;;
161 if (( $# > 1 )); then
162 die
: "only 1 nonoption arg is supported"
165 if [[ -v targets
&& $source ]]; then
166 die
"error: -t and -s are mutually exclusive"
170 printf "$pre options: conf_only=%s\ndry_run=%s\nrate_limit=%s\nverbose=%s\ncmd_arg=%s" "$conf_only" "$dry_run" "$rate_limit" "$verbose" "$cmd_arg"
172 ### end options parsing
174 # TODO: i wonder if there should be an option to send to the default
175 # targets, plus any given on the command line.
177 # set default targets
178 if [[ ! -v targets
&& ! $source ]]; then
179 if [[ $HOSTNAME != "$MAIL_HOST" ]] && $cron ; then
180 echo "MAIL_HOST=$MAIL_HOST, nothing to do"
185 if ping -q -c1 -w1 iank.vpn.office.fsf.org
&>/dev
/null
; then
186 home
=iank.vpn.office.fsf.org
199 # might not be connected to the vpn
200 if timeout
-s 9 10 ssh kw
:; then
206 # might not be connected to the vpn
207 if timeout
-s 9 10 ssh kw
:; then
212 die
"error: no default targets for this host, use -t"
217 if [[ -v targets
]]; then
218 echo "targets: ${targets[*]}"
221 if [[ $source ]]; then
222 echo "source: $source"
226 if [[ $mountpoints ]]; then
227 for mp
in ${mountpoints[@]}; do # default mountpoints to sync
228 if [[ -e /nocow
/btrfs-stale
/$mp ]]; then
229 die
"error: $mp is stale, mount-latest-subvol first"
233 # set default mountpoints
235 # no remote backups atm. note, if we do enable this, configuration below will need some changes.
237 # prospective_mps=(/i)
240 prospective_mps
=(/a
/q
)
241 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
242 prospective_mps
+=(/o
)
246 for mp
in ${prospective_mps[@]}; do # default mountpoints to sync
247 if [[ -e /nocow
/btrfs-stale
/$mp ]]; then
248 echo "$pre warning: $mp stale, not adding to default mountpoints"
251 if awk '{print $2}' /etc
/fstab |
grep -xF $mp &>/dev
/null
; then
257 echo "mountpoints: ${mountpoints[*]}"
259 ##### end command line parsing ########
261 if [[ $source ]]; then
262 if [[ $
(ssh $source systemctl is-active btrbk.service
) != inactive
]]; then
263 echo "$0: error: cron btrbk is running on source. exiting out of caution"
268 # pull_reexec stops us from getting into an infinite loop if there is some
269 # kind of weird problem
271 for m
in "${mountpoints[@]}"; do
272 if [[ $m == /a
]]; then
277 if ! $pull_reexec && [[ $source ]] && $pulla ; then
279 scp
$source:/a
/bin
/distro-setup
/btrbk-run
$tmpf
280 if ! diff -q $tmpf $BASH_SOURCE; then
281 echo "$pre found newer version on host $source. reexecing"
282 install -T $tmpf /usr
/local
/bin
/btrbk-run
283 m
/usr
/local
/bin
/btrbk-run
--pull-reexec "${orig_args[@]}"
292 if ! which btrbk
&>/dev
/null
; then
293 die
"error: no btrbk binary found"
295 # if our mountpoints are from stale snapshots,
296 # it doesn't make sense to do a backup.
297 check-subvol-stale
${mountpoints[@]} || die
"found stale mountpoints in ${mountpoints[*]}"
299 # for an initial run, btrbk requires the dir to exist.
300 mkdir
-p /mnt
/root
/btrbk
301 local_zone
=$
(date +%z
)
303 if [[ $source ]]; then
304 if ! zone
=$
(ssh root@
$source date +%z
); then
305 die failed to
ssh to root@
$source
307 if [[ $zone != $local_zone ]]; then
308 die
"error: dont confuse yourself with multiple time zones. $h has different timezone than localhost"
315 min_idle_ms
=$
((1000 * 60 * 15))
316 for h
in ${targets[@]}; do
317 if zone
=$
(ssh root@
$h "mkdir -p /mnt/root/btrbk && date +%z"); then
318 if $cron && DISPLAY
=:0 xprintidle
; then
319 # This is a separate ssh because xprintidle can fail and thats ok.
320 # Ignore this host. i sometimes use a non-main machine for testing or web browsing, knowing that
321 # everything will be wiped by the next backup, but I dont want it to happen as Im using
326 if [[ $zone != $local_zone ]]; then
327 die
"error: dont confuse yourself with multiple time zones. $h has different timezone than localhost"
333 if [[ ! $sshable ]] ||
{ ! $cron && [[ $sshfail ]]; }; then
334 die
"failed to ssh to hosts: ${sshfail[*]}"
336 if [[ $sshfail ]]; then
338 echo "$pre error: failed to ssh to ${sshfail[*]} but continuing with other hosts"
340 targets
=(${sshable[@]})
345 cat >/etc
/btrbk.conf
<<EOF
346 ssh_identity /root/.ssh/home
347 # Just a guess that local7 is a good facility to pick.
348 # It's a bit odd that the transaction log has to be logged to
349 # a file or syslog, while other output is sent to std out.
350 # The man does not mention a way for them to be together, but
351 # I dunno if setting a log level like warn might also output
353 transaction_syslog local7
355 # note, i had this because man said 20% speedup, but ran into
356 # this issue, https://github.com/digint/btrbk/issues/275
359 # so we only run one at a time
360 lockfile /var/lock/btrbk.lock
362 # default format of short does not accomidate hourly preservation setting
363 timestamp_format long-iso
365 # only make a snapshot if things have changed
366 snapshot_create onchange
367 # I could make this different from target_preserve,
368 # if one disk had less space.
369 # for now, keeping them equal.
370 snapshot_preserve 36h 14d 8w 24m
371 snapshot_preserve_min 4h
374 # so, total backups = ~89
375 target_preserve 36h 14d 8w 24m
376 target_preserve_min 4h
378 # if something fails and it's not obvious, try doing
379 # btrbk -l debug -v dryrun
381 rate_limit $rate_limit
389 for m
in ${mountpoints[@]}; do
391 if [[ $source ]]; then
392 cat >>/etc
/btrbk.conf
<<EOF
393 volume ssh://$source$vol
395 target send-receive $vol/btrbk
398 cat >>/etc
/btrbk.conf
<<EOF
402 for tg
in ${targets[@]}; do
403 cat >>/etc
/btrbk.conf
<<EOF
404 target send-receive ssh://$tg$vol/btrbk
411 # todo: umount first to ensure we don't have any errors
412 # todo: do some kill fuser stuff to make umount more reliable
422 m btrbk
-v -n $cmd_arg
424 elif [[ $cmd_arg == archive
]]; then
425 if [[ $source ]]; then
426 m btrbk
$verbose_arg $progress_arg $cmd_arg ssh://$source$vol $vol
428 for tg
in ${targets[@]}; do
429 m btrbk
$verbose_arg $progress_arg $cmd_arg $vol ssh://$tg$vol
434 # -q and just using the syslog option seemed nice,
435 # but it doesn't show when a send has a parent and when it doesn't.
436 m btrbk
$verbose_arg $progress_arg $cmd_arg
438 # if we have it, sync to systems which don't
439 if mountpoint
$rsync_mountpoint >/dev
/null
; then
440 for tg
in ${targets[@]}; do
443 for x
in /p
/c
/machine_specific
/*.hosts
; do
444 if grep -qxF $tg $x; then
454 if [[ $source ]]; then
455 m mount-latest-subvol
$verbose_arg
457 m
/a
/exe
/mount-latest-remote
${targets[@]}
462 # todo: move variable data we don't care about backing up
463 # to /nocow and symlink it.
466 # background on btrbk timezones. with short/long, timestamps use local time.
467 # for long, if your local time moves backwards, by moving timezones or
468 # for an hour when daylight savings changes it, you will temporarily get
469 # a more aggressive retention policy for the overlapping period, and
470 # vice versa for the opposite timezone move. The alternative is using
471 # long-iso, which puts timezone info into the timestamp, which means
472 # that instead of shifting time, you shift the start of day/week/month
473 # which is used for retention to your new local time, which means for
474 # example, if you moved forward by 8 hours, the daily/weekly/monthly
475 # retention will be 8 hours more aggressive since midnight is at a new
476 # time, unless you fake the timzeone using the TZ env variable.
477 # However, in the short term, there will be no inconsistencies.
478 # I don't see any problem with shifting when the day starts for
479 # retention, so I'm using long-iso.
481 # note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z