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 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
19 [[ $EUID == 0 ]] ||
exec sudo
-E "$BASH_SOURCE" "$@"
21 echo "top of script file:"
22 sed -n '1,/^[# ]*end command line/{p;b};q' "$0"
26 script_dir
=$
(dirname $
(readlink
-f "$BASH_SOURCE"))
28 # note q is owned by root:1000
29 # note p/m is owned 1000:1000 and chmod 700
31 private_mountpoints
=(/q
)
35 dry_run
=false
# mostly for testing
40 temp
=$
(getopt
-l help hcinoprt
: "$@") || usage
1
44 -c) conf_only
=true
; shift ;;
45 -i) do_i
=false
; shift ;;
46 -n) dry_run
=true
; dry_run_arg
=-n; shift ;;
47 -o) do_o
=false
; shift ;;
48 -p) progress_arg
="--progress"; shift ;;
49 # btrbk arg: Resume only. Skips snapshot creation.
50 -r) resume_arg
=-r; shift ;;
51 -t) IFS
=, targets
=($2); unset IFS
; shift 2 ;;
54 *) echo "$0: Internal error!" ; exit 1 ;;
59 private_mountpoints
+=(/o
)
61 read primary
<<<"$@" # not yet used
63 ##### end command line parsing ########
68 m rsync
$dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
72 cat >>/etc
/btrbk.conf
<<EOF
77 cat >>/etc
/btrbk.conf
<<EOF
82 cat >>/etc
/btrbk.conf
<<EOF
83 target send-receive ssh://$tg$vol/btrbk
86 m
() { printf "%s: %s\n" "${0##*/}" "$*"; "$@"; }
89 if ! which btrbk
&>/dev
/null
; then
90 echo "$0: error: no btrbk binary found"
93 cat >/etc
/btrbk.conf
<<'EOF'
94 ssh_identity /root/.ssh/id_rsa
95 # Just a guess that local7 is a good facility to pick.
96 # It's a bit odd that the transaction log has to be logged to
97 # a file or syslog, while other output is sent to std out.
98 # The man does not mention a way for them to be together, but
99 # I dunno if setting a log level like warn might also output
101 transaction_syslog local7
103 # so we only run one at a time
104 lockfile /var/lock/btrbk.lock
106 # default format of short does not accomidate hourly preservation setting
107 timestamp_format long-iso
109 # only make a snapshot if things have changed
110 snapshot_create onchange
111 # I could make this different from target_preserve,
112 # if one disk had less space.
113 # for now, keeping them equal.
114 snapshot_preserve 36h 14d 8w 24m
115 snapshot_preserve_min 4h
118 # so, total backups = ~89
119 target_preserve 36h 14d 8w 24m
120 target_preserve_min 4h
122 # if something fails and it's not obvious, try doing
123 # btrbk -l debug -v dryrun
126 for mp
in ${private_mountpoints[@]}; do # private mountpoints
127 if awk '{print $2}' /etc
/fstab |
grep -xF $mp &>/dev
/null
; then
132 # if our mountpoints are from stale snapshots,
133 # it doesn't make sense to do a backup.
134 check-subvol-stale
${mountpoints[@]} ||
exit 1
136 if [[ ! $targets ]]; then
139 if ! timeout
-s 9 10 ssh frodo
:; then
140 targets
=($HOME_DOMAIN)
145 if timeout
-s 9 10 ssh x2
:; then
153 echo "targets: ${targets[*]}"
157 # for i, we just do a 1 way sync from master to backup,
158 # and manually manage any changes to that.
160 for tg
in ${targets[@]}; do
161 # for an initial run, btrbk requires the dir to exist
162 ssh root@
$tg mkdir
-p /mnt
/root
/btrbk
163 if [[ $tg == frodo
&& $HOSTNAME == treetowl
]]; then
167 if ! $i_possible; then
174 for m
in ${mountpoints[@]}; do
177 for tg
in ${targets[@]}; do
194 # todo: umount first to ensure we don't have any errors
195 # todo: do some kill fuser stuff to make umount more reliable
196 # todo: run this on a systemd timer on $primary, once per hour,
197 # and if primary is, change that timer over to primary, and make
198 # sure we mount the latest
207 m btrbk
-n $resume_arg run
209 # -q and just using the syslog option seemed nice,
210 # but it doesn't show when a send has a parent and when it doesn't.
211 m btrbk
$progress_arg $resume_arg run
214 # if we have it, sync to systems which don't
215 if mountpoint
$rsync_mountpoint >/dev
/null
; then
216 for tg
in ${targets[@]}; do
219 for x
in /p
/c
/machine_specific
/*.hosts
; do
220 if grep -qxF $tg $x; then
231 m
$script_dir/mount-latest-remote
${targets[@]}
235 # todo: move variable data we don't care about backing up
236 # to /nocow and symlink it.
239 # background on btrbk timezones. with short/long, timestamps use local time.
240 # for long, if your local time moves backwards, by moving timezones or
241 # for an hour when daylight savings changes it, you will temporarily get
242 # a more aggressive retention policy for the overlapping period, and
243 # vice versa for the opposite timezone move. The alternative is using
244 # long-iso, which puts timezone info into the timestamp, which means
245 # that instead of shifting time, you shift the start of day/week/month
246 # which is used for retention to your new local time, which means for
247 # example, if you moved forward by 8 hours, the daily/weekly/monthly
248 # retention will be 8 hours more aggressive since midnight is at a new
249 # time, unless you fake the timzeone using the TZ env variable.
250 # However, in the short term, there will be no inconsistencies.
251 # I don't see any problem with shifting when the day starts for
252 # retention, so I'm using long-iso.
254 # note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z