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 [[ $EUID == 0 ]] ||
exec sudo
-E "${BASH_SOURCE[0]}" "$@"
22 source /usr
/local
/lib
/err
27 usually -t TARGET_HOST or -s SOURCE_HOST
29 Note, at source location, intentionally not executable, run and read
33 echo "top of script file:"
34 sed -n '1,/^[# ]*end command line/{p;b};q' "$0"
41 m rsync
$dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
46 m
() { if $verbose; then printf "$pre%s\n" "$*"; fi; "$@"; }
47 e
() { printf "$pre%s\n" "$*"; }
48 die
() { printf "$pre%s\n" "$*" >&2; exit 1; }
51 if [[ -e /b
/bash_unpublished
/source-state
]]; then
52 source /b
/bash_unpublished
/source-state
55 # note q is owned by root:1000
63 dry_run
=false
# mostly for testing
65 verbose
=true
; verbose_arg
=-v
66 progress_arg
="--progress"
69 default_args_file
=/etc
/btrbk-run.conf
70 if [[ -s $default_args_file ]]; then
71 set -- $
(< $default_args_file) "$@"
72 # i havent used this feature yet, so warn about it
73 echo "$0: warning: default btrbk-run options set in $default_args_file (sleeping 5 seconds):"
74 cat $default_args_file
80 temp
=$
(getopt
-l cron
,pull-reexec
,help cl
:m
:npqs
:t
:vh
"$@") || usage
1
89 # only creates the config file, does not run btrbk
90 -c) conf_only
=true
; shift ;;
91 # bytes per second, suffix k m g
92 -l) rate_limit
=$2; shift 2 ;;
93 # Comma separated mountpoints to backup. This has defaults set below.
94 -m) IFS
=, mountpoints
=($2); unset IFS
; shift 2 ;;
95 -n) dry_run
=true
; dry_run_arg
=-n; shift ;;
96 -p) progress_arg
="--progress"; shift ;;
97 --pull-reexec) pull_reexec
=true
; shift ;;
98 -q) verbose
=false
; verbose_arg
=; progress_arg
=; shift ;;
99 # source host to receive a backup from
100 -s) source=$2; shift 2 ;;
101 # target hosts to send to. empty is valid for just doing local
102 # snapshot. we have default hosts we will populate.
103 -t) IFS
=, targets
=($2); unset IFS
; shift 2 ;;
104 -v) verbose
=true
; verbose_arg
=-v; shift ;;
107 *) die
"Internal error!" ;;
111 # only tested commands are resume and archive
115 run|resume|archive
) : ;;
116 *) die
"untested command arg" ;;
119 if (( $# > 1 )); then
120 die
: "only 1 nonoption arg is supported"
123 if [[ -v targets
&& $source ]]; then
124 die
"error: -t and -s are mutually exclusive"
128 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"
130 ### end options parsing
132 # TODO: i wonder if there should be an option to send to the default
133 # targets, plus any given on the command line.
135 # set default targets
136 if [[ ! -v targets
&& ! $source ]]; then
137 if [[ $HOSTNAME != "$MAIL_HOST" ]] && $cron ; then
138 echo "MAIL_HOST=$MAIL_HOST, nothing to do"
143 if ping -q -c1 -w1 iank.vpn.office.fsf.org
&>/dev
/null
; then
144 home
=iank.vpn.office.fsf.org
156 # kd disabled temporarily while its hot and i plan to work on it.
158 targets
=(frodo x3.b8.nz
)
159 # might not be connected to the vpn
160 if timeout
-s 9 10 ssh kw
:; then
166 # might not be connected to the vpn
167 if timeout
-s 9 10 ssh kw
:; then
172 die
"error: no default targets for this host, use -t"
177 if [[ -v targets
]]; then
178 echo "targets: ${targets[*]}"
181 if [[ $source ]]; then
182 echo "source: $source"
186 if [[ $mountpoints ]]; then
187 for mp
in ${mountpoints[@]}; do # default mountpoints to sync
188 if [[ -e /nocow
/btrfs-stale
/$mp ]]; then
189 die
"error: $mp is stale, mount-latest-subvol first"
193 # set default mountpoints
195 # no remote backups atm. note, if we do enable this, configuration below will need some changes.
197 # prospective_mps=(/i)
200 prospective_mps
=(/a
/q
)
201 if [[ $source ]]; then
202 source_state
="$(ssh $source cat /a/bin/bash_unpublished/source-state)"
204 source_host
="$(ssh $source cat /etc/hostname)"
205 if [[ $source_host == "$MAIL_HOST" ]]; then
206 prospective_mps
+=(/o
)
209 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
210 prospective_mps
+=(/o
)
215 for mp
in ${prospective_mps[@]}; do # default mountpoints to sync
216 if [[ -e /nocow
/btrfs-stale
/$mp ]]; then
217 e
"warning: $mp stale, not adding to default mountpoints"
220 if awk '{print $2}' /etc
/fstab |
grep -xF $mp &>/dev
/null
; then
226 echo "mountpoints: ${mountpoints[*]}"
228 ##### end command line parsing ########
230 if [[ $source ]]; then
231 if [[ $
(ssh $source systemctl is-active btrbk.service
) != inactive
]]; then
232 echo "$0: error: cron btrbk is running on source. exiting out of caution"
237 # pull_reexec stops us from getting into an infinite loop if there is some
238 # kind of weird problem
240 for m
in "${mountpoints[@]}"; do
241 if [[ $m == /a
]]; then
246 if ! $pull_reexec && [[ $source ]] && $pulla ; then
248 scp
$source:/a
/bin
/distro-setup
/btrbk-run
$tmpf
249 if ! diff -q $tmpf $BASH_SOURCE; then
250 e
"found newer version on host $source. reexecing"
251 install -T $tmpf /usr
/local
/bin
/btrbk-run
252 m
/usr
/local
/bin
/btrbk-run
--pull-reexec "${orig_args[@]}"
261 if ! which btrbk
&>/dev
/null
; then
262 die
"error: no btrbk binary found"
264 # if our mountpoints are from stale snapshots,
265 # it doesn't make sense to do a backup.
266 check-subvol-stale
${mountpoints[@]} || die
"found stale mountpoints in ${mountpoints[*]}"
268 # for an initial run, btrbk requires the dir to exist.
269 mkdir
-p /mnt
/root
/btrbk
270 local_zone
=$
(date +%z
)
272 if [[ $source ]]; then
273 if ! zone
=$
(ssh root@
$source date +%z
); then
274 die failed to
ssh to root@
$source
276 if [[ $zone != $local_zone ]]; then
277 die
"error: dont confuse yourself with multiple time zones. $h has different timezone than localhost"
284 min_idle_ms
=$
((1000 * 60 * 15))
285 for h
in ${targets[@]}; do
286 if zone
=$
(ssh root@
$h "mkdir -p /mnt/root/btrbk && date +%z"); then
287 # This is a separate ssh because xprintidle can fail and thats ok.
288 if $cron && idle_ms
=$
(ssh $h DISPLAY
=:0 xprintidle
); then
289 if (( idle_ms
< min_idle_ms
)); then
291 # Ignore this host. i sometimes use a non-main machine for
292 # testing or web browsing, knowing that everything will be wiped
293 # by the next backup, but I dont want it to happen as Im using
295 e
"warning: $h: active X session in the last 15 minutes, skipping for now"
300 if [[ $zone != $local_zone ]]; then
301 die
"error: dont confuse yourself with multiple time zones. $h has different timezone than localhost"
307 if [[ ! ${sshable[@]} ]] || { ! $cron && [[ ${sshfail[@]} ]]; }; then
308 die
"failed to ssh to hosts: ${sshfail[*]}"
310 if [[ ${sshfail[@]} ]]; then
312 e
"error: failed to ssh to ${sshfail[*]} but continuing with other hosts"
314 targets
=(${sshable[@]})
319 cat >/etc
/btrbk.conf
<<EOF
320 ssh_identity /root/.ssh/home
321 # Just a guess that local7 is a good facility to pick.
322 # It's a bit odd that the transaction log has to be logged to
323 # a file or syslog, while other output is sent to std out.
324 # The man does not mention a way for them to be together, but
325 # I dunno if setting a log level like warn might also output
327 transaction_syslog local7
329 # note, i had this because man said 20% speedup, but ran into
330 # this issue, https://github.com/digint/btrbk/issues/275
333 # so we only run one at a time
334 lockfile /var/lock/btrbk.lock
336 # default format of short does not accomidate hourly preservation setting
337 timestamp_format long-iso
339 # only make a snapshot if things have changed
340 snapshot_create onchange
341 # I could make this different from target_preserve,
342 # if one disk had less space.
343 # for now, keeping them equal.
344 snapshot_preserve 36h 14d 8w 24m
345 snapshot_preserve_min 4h
348 # so, total backups = ~89
349 target_preserve 36h 14d 8w 24m
350 target_preserve_min 4h
352 # if something fails and it's not obvious, try doing
353 # btrbk -l debug -v dryrun
355 rate_limit $rate_limit
363 for m
in ${mountpoints[@]}; do
365 if [[ $source ]]; then
366 cat >>/etc
/btrbk.conf
<<EOF
367 volume ssh://$source$vol
369 target send-receive $vol/btrbk
372 cat >>/etc
/btrbk.conf
<<EOF
376 for tg
in ${targets[@]}; do
377 cat >>/etc
/btrbk.conf
<<EOF
378 target send-receive ssh://$tg$vol/btrbk
385 # todo: umount first to ensure we don't have any errors
386 # todo: do some kill fuser stuff to make umount more reliable
396 m btrbk
-v -n $cmd_arg
398 elif [[ $cmd_arg == archive
]]; then
399 if [[ $source ]]; then
400 m btrbk
$verbose_arg $progress_arg $cmd_arg ssh://$source$vol $vol
402 for tg
in ${targets[@]}; do
403 m btrbk
$verbose_arg $progress_arg $cmd_arg $vol ssh://$tg$vol
408 # -q and just using the syslog option seemed nice,
409 # but it doesn't show when a send has a parent and when it doesn't.
410 m btrbk
$verbose_arg $progress_arg $cmd_arg
412 # if we have it, sync to systems which don't
413 if mountpoint
$rsync_mountpoint >/dev
/null
; then
414 for tg
in ${targets[@]}; do
417 for x
in /p
/c
/machine_specific
/*.hosts
; do
418 if grep -qxF $tg $x; then
428 if [[ $source ]]; then
429 m mount-latest-subvol
$verbose_arg
431 m
/a
/exe
/mount-latest-remote
${targets[@]}
436 # todo: move variable data we don't care about backing up
437 # to /nocow and symlink it.
440 # background on btrbk timezones. with short/long, timestamps use local time.
441 # for long, if your local time moves backwards, by moving timezones or
442 # for an hour when daylight savings changes it, you will temporarily get
443 # a more aggressive retention policy for the overlapping period, and
444 # vice versa for the opposite timezone move. The alternative is using
445 # long-iso, which puts timezone info into the timestamp, which means
446 # that instead of shifting time, you shift the start of day/week/month
447 # which is used for retention to your new local time, which means for
448 # example, if you moved forward by 8 hours, the daily/weekly/monthly
449 # retention will be 8 hours more aggressive since midnight is at a new
450 # time, unless you fake the timzeone using the TZ env variable.
451 # However, in the short term, there will be no inconsistencies.
452 # I don't see any problem with shifting when the day starts for
453 # retention, so I'm using long-iso.
455 # note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z