a1c6963a926b445454ca1d27634a56737e2d34da
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: remove old leaf subvols, like keep up to 1 month or something.
20 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
22 [[ $EUID == 0 ]] ||
exec sudo
-E "$BASH_SOURCE" "$@"
25 echo "top of script file:"
26 sed -n '1,/^[# ]*end command line/{p;b};q' "$0"
30 script_dir
=$
(dirname $
(readlink
-f "$BASH_SOURCE"))
32 # note q is owned by root:1000
33 # note p/m is owned 1000:1000 and chmod 700
41 dry_run
=false
# mostly for testing
46 default_args_file
=/etc
/btrbk-run.conf
47 if [[ -s $default_args_file ]]; then
48 set -- $
(< $default_args_file) "$@"
51 temp
=$
(getopt
-l help cl
:m
:nprt
:vh
"$@") || usage
1
55 # only creates the config file, does not run btrbk
56 -c) conf_only
=true
; shift ;;
57 -l) rate_limit
=$2; shift 2 ;;
58 -m) IFS
=, mountpoints
=($2); unset IFS
; shift 2 ;;
59 -n) dry_run
=true
; dry_run_arg
=-n; shift ;;
60 -p) progress_arg
="--progress"; shift ;;
61 # btrbk arg: Resume only. Skips snapshot creation.
62 -r) resume_arg
=-r; shift ;;
63 # empty is valid for just doing local snapshot. we have default hosts
65 -t) IFS
=, targets
=($2); unset IFS
; shift 2 ;;
66 -v) verbose
=true
; verbose_arg
=-v; shift ;;
69 *) echo "$0: Internal error!" ; exit 1 ;;
73 if [[ -s $default_args_file ]]; then
74 echo "$0: warning: default btrbk-run options set in $default_args_file (sleeping 5 seconds):"
75 cat $default_args_file
79 echo -e "$0: options: conf_only=$conf_only\ndry_run=$dry_run\nresume_arg=$resume_arg\nrate_limit=$rate_limit\nverbose=$verbose"
82 if [[ ! -v targets
]]; then
85 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
86 targets
=($HOME_DOMAIN)
91 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
92 if timeout
-s 9 10 ssh x2
:; then
101 echo "$0: error: no default targets for this host, use -t"
107 echo "targets: ${targets[*]}"
111 if (( ${#mountpoints[@]} )); then
112 for mp
in ${mountpoints[@]}; do
113 if [[ -e /nocow
/btrfs-stale
/$mp ]]; then
114 echo "$0: warning: $mp stale. Sleeping for 3 seconds in case you want to cancel."
118 else # set default mountpoints
124 prospective_mps
=(/a
/q
)
125 if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
126 prospective_mps
+=(/o
)
130 case ${targets[0]} in
136 for mp
in ${prospective_mps[@]}; do # default mountpoints to sync
137 if [[ -e /nocow
/btrfs-stale
/$mp ]]; then
138 echo "$0: warning: $mp stale, not adding to default mountpoints"
141 if awk '{print $2}' /etc
/fstab |
grep -xF $mp &>/dev
/null
; then
147 echo "mountpoints: ${mountpoints[*]}"
149 ##### end command line parsing ########
154 m rsync
$dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
158 cat >>/etc
/btrbk.conf
<<EOF
163 cat >>/etc
/btrbk.conf
<<EOF
168 cat >>/etc
/btrbk.conf
<<EOF
169 target send-receive ssh://$tg$vol/btrbk
172 m
() { printf "%s: %s\n" "${0##*/}" "$*"; "$@"; }
175 if ! which btrbk
&>/dev
/null
; then
176 echo "$0: error: no btrbk binary found"
179 cat >/etc
/btrbk.conf
<<EOF
180 ssh_identity /root/.ssh/home
181 # Just a guess that local7 is a good facility to pick.
182 # It's a bit odd that the transaction log has to be logged to
183 # a file or syslog, while other output is sent to std out.
184 # The man does not mention a way for them to be together, but
185 # I dunno if setting a log level like warn might also output
187 transaction_syslog local7
189 # so we only run one at a time
190 lockfile /var/lock/btrbk.lock
192 # default format of short does not accomidate hourly preservation setting
193 timestamp_format long-iso
195 # only make a snapshot if things have changed
196 snapshot_create onchange
197 # I could make this different from target_preserve,
198 # if one disk had less space.
199 # for now, keeping them equal.
200 snapshot_preserve 36h 14d 8w 24m
201 snapshot_preserve_min 4h
204 # so, total backups = ~89
205 target_preserve 36h 14d 8w 24m
206 target_preserve_min 4h
208 # if something fails and it's not obvious, try doing
209 # btrbk -l debug -v dryrun
211 rate_limit $rate_limit
215 # if our mountpoints are from stale snapshots,
216 # it doesn't make sense to do a backup.
217 check-subvol-stale
${mountpoints[@]} ||
exit 1
219 for tg
in ${targets[@]}; do
220 # for an initial run, btrbk requires the dir to exist
221 ssh root@
$tg mkdir
-p /mnt
/root
/btrbk
225 for m
in ${mountpoints[@]}; do
226 # for /i, some special cases. there is just one static target and direction.
227 if [[ $m == /i
]]; then
240 for tg
in ${targets[@]}; do
247 # todo: umount first to ensure we don't have any errors
248 # todo: do some kill fuser stuff to make umount more reliable
256 m btrbk
-v -n $resume_arg run
258 # -q and just using the syslog option seemed nice,
259 # but it doesn't show when a send has a parent and when it doesn't.
260 m btrbk
$verbose_arg $progress_arg $resume_arg run
263 # if we have it, sync to systems which don't
264 if mountpoint
$rsync_mountpoint >/dev
/null
; then
265 for tg
in ${targets[@]}; do
268 for x
in /p
/c
/machine_specific
/*.hosts
; do
269 if grep -qxF $tg $x; then
280 m
$script_dir/mount-latest-remote
${targets[@]}
284 # todo: move variable data we don't care about backing up
285 # to /nocow and symlink it.
288 # background on btrbk timezones. with short/long, timestamps use local time.
289 # for long, if your local time moves backwards, by moving timezones or
290 # for an hour when daylight savings changes it, you will temporarily get
291 # a more aggressive retention policy for the overlapping period, and
292 # vice versa for the opposite timezone move. The alternative is using
293 # long-iso, which puts timezone info into the timestamp, which means
294 # that instead of shifting time, you shift the start of day/week/month
295 # which is used for retention to your new local time, which means for
296 # example, if you moved forward by 8 hours, the daily/weekly/monthly
297 # retention will be 8 hours more aggressive since midnight is at a new
298 # time, unless you fake the timzeone using the TZ env variable.
299 # However, in the short term, there will be no inconsistencies.
300 # I don't see any problem with shifting when the day starts for
301 # retention, so I'm using long-iso.
303 # note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z