59631fdd75efbad9f54733a08b22975e5b5712c8
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 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
22 [[ $EUID == 0 ]] ||
exec sudo
-E "$BASH_SOURCE" "$@"
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"
39 if [[ -e /b
/bash_unpublished
/source-semi-priv
]]; then
40 source /b
/bash_unpublished
/source-semi-priv
43 # note q is owned by root:1000
51 dry_run
=false
# mostly for testing
53 verbose
=true
; verbose_arg
=-v
54 progress_arg
="--progress"
57 default_args_file
=/etc
/btrbk-run.conf
58 if [[ -s $default_args_file ]]; then
59 set -- $
(< $default_args_file) "$@"
60 echo "$0: warning: default btrbk-run options set in $default_args_file (sleeping 5 seconds):"
61 cat $default_args_file
66 temp
=$
(getopt
-l pull-reexec
,help cl
:m
:nps
:t
:vh
"$@") || usage
1
70 # only creates the config file, does not run btrbk
71 -c) conf_only
=true
; shift ;;
72 # bytes per second, suffix k m g
73 -l) rate_limit
=$2; shift 2 ;;
74 # Comma separated mountpoints to backup. This has defaults set below.
75 -m) IFS
=, mountpoints
=($2); unset IFS
; shift 2 ;;
76 -n) dry_run
=true
; dry_run_arg
=-n; shift ;;
77 -p) progress_arg
="--progress"; shift ;;
78 --pull-reexec) pull_reexec
=true
; shift ;;
79 -q) verbose
=false
; verbose_arg
=; progress_arg
=; shift ;;
80 # source host to receive a backup from
81 -s) source=$2; shift 2 ;;
82 # target hosts to send to. empty is valid for just doing local
83 # snapshot. we have default hosts we will populate.
84 -t) IFS
=, targets
=($2); unset IFS
; shift 2 ;;
85 -v) verbose
=true
; verbose_arg
=-v; shift ;;
88 *) echo "$0: Internal error!" ; exit 1 ;;
92 # usefull commands are resume and archive
95 if [[ -v targets
&& $source ]]; then
96 echo "$0: error: -t and -s are mutually exclusive" >&2
100 # pull_reexec stops us from getting into an infinite loop if there is some
101 # kind of weird problem
102 if ! $pull_reexec && [[ $source ]]; then
104 scp
$source:/a
/bin
/distro-setup
/btrbk-run
$tmpf
105 if diff -q $tmpf $BASH_SOURCE; then
106 echo "$0: found newer version on host $source. reexecing"
107 install -T $tmpf /usr
/local
/bin
/btrbk-run
108 m
/usr
/local
/bin
/btrbk-run
--pull-reexec "${orig_args[@]}"
114 echo -e "$0: options: conf_only=$conf_only\ndry_run=$dry_run\nrate_limit=$rate_limit\nverbose=$verbose\ncmd_arg=$cmd_arg"
115 ### end options parsing
118 # set default targets
119 if [[ ! -v targets
&& ! $source ]]; then
122 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
123 targets
=($HOME_DOMAIN)
128 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
129 if timeout
-s 9 10 ssh x2
:; then
138 echo "$0: error: no default targets for this host, use -t"
144 if [[ -v targets
]]; then
145 echo "targets: ${targets[*]}"
148 if [[ $source ]]; then
149 echo "source: $source"
153 if [[ $mountpoints ]]; then
154 for mp
in ${mountpoints[@]}; do # default mountpoints to sync
155 if [[ -e /nocow
/btrfs-stale
/$mp ]]; then
156 echo "$0: error: $mp is stale, mount-latest-subvol first"
161 # set default mountpoints
163 # no remote backups atm. note, if we do enable this, configuration below will need some changes.
165 # prospective_mps=(/i)
168 prospective_mps
=(/a
/q
)
169 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
170 prospective_mps
+=(/o
)
174 for mp
in ${prospective_mps[@]}; do # default mountpoints to sync
175 if [[ -e /nocow
/btrfs-stale
/$mp ]]; then
176 echo "$0: warning: $mp stale, not adding to default mountpoints"
179 if awk '{print $2}' /etc
/fstab |
grep -xF $mp &>/dev
/null
; then
185 echo "mountpoints: ${mountpoints[*]}"
187 ##### end command line parsing ########
192 m rsync
$dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
196 m
() { printf "%s: %s\n" "${0##*/}" "$*"; "$@"; }
199 if ! which btrbk
&>/dev
/null
; then
200 echo "$0: error: no btrbk binary found"
203 # if our mountpoints are from stale snapshots,
204 # it doesn't make sense to do a backup.
205 check-subvol-stale
${mountpoints[@]} ||
exit 1
208 cat >/etc
/btrbk.conf
<<EOF
209 ssh_identity /root/.ssh/home
210 # Just a guess that local7 is a good facility to pick.
211 # It's a bit odd that the transaction log has to be logged to
212 # a file or syslog, while other output is sent to std out.
213 # The man does not mention a way for them to be together, but
214 # I dunno if setting a log level like warn might also output
216 transaction_syslog local7
218 # note, i had this because man said 20% speedup, but ran into
219 # this issue, https://github.com/digint/btrbk/issues/275
222 # so we only run one at a time
223 lockfile /var/lock/btrbk.lock
225 # default format of short does not accomidate hourly preservation setting
226 timestamp_format long-iso
228 # only make a snapshot if things have changed
229 snapshot_create onchange
230 # I could make this different from target_preserve,
231 # if one disk had less space.
232 # for now, keeping them equal.
233 snapshot_preserve 36h 14d 8w 24m
234 snapshot_preserve_min 4h
237 # so, total backups = ~89
238 target_preserve 36h 14d 8w 24m
239 target_preserve_min 4h
241 # if something fails and it's not obvious, try doing
242 # btrbk -l debug -v dryrun
244 rate_limit $rate_limit
249 # for an initial run, btrbk requires the dir to exist.
250 mkdir
-p /mnt
/root
/btrbk
251 local_zone
=$
(date +%z
)
252 for h
in ${targets[@]} $source; do
253 zone
=$
(ssh root@
$h "mkdir -p /mnt/root/btrbk; date +%z")
254 if [[ $zone != $local_zone ]]; then
255 echo "error: error. dont confuse yourself with multiple time zones. $h has different timezone than localhost" >&2
263 for m
in ${mountpoints[@]}; do
265 if [[ $source ]]; then
266 cat >>/etc
/btrbk.conf
<<EOF
267 volume ssh://$source$vol
269 target send-receive $vol/btrbk
272 cat >>/etc
/btrbk.conf
<<EOF
276 for tg
in ${targets[@]}; do
277 cat >>/etc
/btrbk.conf
<<EOF
278 target send-receive ssh://$tg$vol/btrbk
285 # todo: umount first to ensure we don't have any errors
286 # todo: do some kill fuser stuff to make umount more reliable
296 m btrbk
-v -n $cmd_arg
298 elif [[ $cmd_arg == archive
]]; then
299 if [[ $source ]]; then
300 m btrbk
$verbose_arg $progress_arg $cmd_arg ssh://$source$vol $vol
302 for tg
in ${targets[@]}; do
303 m btrbk
$verbose_arg $progress_arg $cmd_arg $vol ssh://$tg$vol
308 # -q and just using the syslog option seemed nice,
309 # but it doesn't show when a send has a parent and when it doesn't.
310 m btrbk
$verbose_arg $progress_arg $cmd_arg
312 # if we have it, sync to systems which don't
313 if mountpoint
$rsync_mountpoint >/dev
/null
; then
314 for tg
in ${targets[@]}; do
317 for x
in /p
/c
/machine_specific
/*.hosts
; do
318 if grep -qxF $tg $x; then
328 /a
/bin
/distro-setup
/install-my-scripts
329 if [[ $source ]]; then
330 m mount-latest-subvol
332 m
/a
/exe
/mount-latest-remote
${targets[@]}
335 # todo: move variable data we don't care about backing up
336 # to /nocow and symlink it.
339 # background on btrbk timezones. with short/long, timestamps use local time.
340 # for long, if your local time moves backwards, by moving timezones or
341 # for an hour when daylight savings changes it, you will temporarily get
342 # a more aggressive retention policy for the overlapping period, and
343 # vice versa for the opposite timezone move. The alternative is using
344 # long-iso, which puts timezone info into the timestamp, which means
345 # that instead of shifting time, you shift the start of day/week/month
346 # which is used for retention to your new local time, which means for
347 # example, if you moved forward by 8 hours, the daily/weekly/monthly
348 # retention will be 8 hours more aggressive since midnight is at a new
349 # time, unless you fake the timzeone using the TZ env variable.
350 # However, in the short term, there will be no inconsistencies.
351 # I don't see any problem with shifting when the day starts for
352 # retention, so I'm using long-iso.
354 # note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z