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 # todo: finish figuring out fai / distro-setup
29 # initial fstab / subvol setup.
32 dry_run
=false
# mostly for testing
35 temp
=$
(getopt
-l help hcnprt
: "$@") || usage
1
39 -c) conf_only
=true
; shift ;;
40 -n) dry_run
=true
; dry_run_arg
=-n; shift ;;
41 -p) progress_arg
="--progress"; shift ;;
42 # btrbk arg: Resume only. Skips snapshot creation.
43 -r) resume_arg
=-r; shift ;;
44 -t) IFS
=, targets
=($2); unset IFS
; shift 2 ;;
47 *) echo "$0: Internal error!" ; exit 1 ;;
52 ##### end command line parsing ########
57 m rsync
$dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
61 cat >>/etc
/btrbk.conf
<<EOF
66 cat >>/etc
/btrbk.conf
<<EOF
71 cat >>/etc
/btrbk.conf
<<EOF
72 target send-receive ssh://$tg$vol/btrbk
75 m
() { printf "%s: %s\n" "${0##*/}" "$*"; "$@"; }
78 if ! which btrbk
&>/dev
/null
; then
79 echo "$0: error: no btrbk binary found"
82 cat >/etc
/btrbk.conf
<<'EOF'
83 ssh_identity /root/.ssh/id_rsa
84 # Just a guess that local7 is a good facility to pick.
85 # It's a bit odd that the transaction log has to be logged to
86 # a file or syslog, while other output is sent to std out.
87 # The man does not mention a way for them to be together, but
88 # I dunno if setting a log level like warn might also output
90 transaction_syslog local7
92 # so we only run one at a time
93 lockfile /var/lock/btrbk.lock
95 # default format of short does not accomidate hourly preservation setting
96 timestamp_format long-iso
98 # only make a snapshot if things have changed
99 snapshot_create onchange
100 # much less snapshots because I have less space on the
102 #snapshot_preserve 2h 2d
103 # for now, keeping them equal for simplicity sake
104 snapshot_preserve 48h 14d 8w 24m
105 snapshot_preserve_min 6h
108 # so, total backups = ~89
109 target_preserve 48h 14d 8w 24m
110 target_preserve_min 6h
112 # if something fails and it's not obvious, try doing
113 # btrbk -l debug -v dryrun
116 # note q is owned by root:1000
117 # note p is owned 1000:1000 and chmod 700
120 if awk '{print $2}' /etc
/fstab |
grep -xF $qmnt &>/dev
/null
; then
124 # if our mountpoints are from stale snapshots,
125 # it doesn't make sense to do a backup.
126 check-subvol-stale
${mountpoints[@]} ||
exit 1
128 if [[ ! $targets ]]; then
131 if ! timeout
-s 9 10 ssh frodo
:; then
132 targets
=($HOME_DOMAIN)
137 if timeout
-s 9 10 ssh x2
:; then
145 echo "targets: ${targets[*]}"
149 # for i, we just do a 1 way sync from master to backup,
150 # and manually manage any changes to that.
152 for tg
in ${targets[@]}; do
153 # for an initial run, btrbk requires the dir to exist
154 ssh root@
$tg mkdir
-p /mnt
/root
/btrbk
155 if [[ $tg == frodo
&& $HOSTNAME == treetowl
]]; then
163 for m
in ${mountpoints[@]}; do
166 for tg
in ${targets[@]}; do
183 # todo: umount first to ensure we don't have any errors
184 # todo: do some kill fuser stuff to make umount more reliable
185 # todo: run this on a systemd timer on $primary, once per hour,
186 # and if primary is, change that timer over to primary, and make
187 # sure we mount the latest
196 m btrbk
-n $resume_arg run
198 # -q and just using the syslog option seemed nice,
199 # but it doesn't show when a send has a parent and when it doesn't.
200 m btrbk
$progress_arg $resume_arg run
203 # if we have /p, rsync to targets without /p
204 if mountpoint
/p
>/dev
/null
; then
205 for tg
in ${targets[@]}; do
208 for x
in /p
/c
/machine_specific
/*.hosts
; do
209 if grep -qxF $tg $x; then
220 m
$script_dir/mount-latest-remote
${targets[@]}
224 # todo: move variable data we don't care about backing up
225 # to /nocow and symlink it.
228 # background on btrbk timezones. with short/long, timestamps use local time.
229 # for long, if your local time moves backwards, by moving timezones or
230 # for an hour when daylight savings changes it, you will temporarily get
231 # a more aggressive retention policy for the overlapping period, and
232 # vice versa for the opposite timezone move. The alternative is using
233 # long-iso, which puts timezone info into the timestamp, which means
234 # that instead of shifting time, you shift the start of day/week/month
235 # which is used for retention to your new local time, which means for
236 # example, if you moved forward by 8 hours, the daily/weekly/monthly
237 # retention will be 8 hours more aggressive since midnight is at a new
238 # time, unless you fake the timzeone using the TZ env variable.
239 # However, in the short term, there will be no inconsistencies.
240 # I don't see any problem with shifting when the day starts for
241 # retention, so I'm using long-iso.
243 # note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z