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 hcnrt
: "$@") || usage
1
39 -c) conf_only
=true
; shift ;;
40 -n) dry_run
=true
; dry_run_arg
=-n; shift ;;
41 -r) resume_arg
=-r; shift ;;
42 -t) IFS
=, targets
=($2); shift 2 ;;
45 *) echo "$0: Internal error!" ; exit 1 ;;
50 ##### end command line parsing ########
55 rsync
$dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
59 cat >>/etc
/btrbk.conf
<<EOF
64 cat >>/etc
/btrbk.conf
<<EOF
69 cat >>/etc
/btrbk.conf
<<EOF
70 target send-receive ssh://$tg$vol/btrbk
76 if ! which btrbk
&>/dev
/null
; then
77 echo "$0: error: no btrbk binary found"
80 cat >/etc
/btrbk.conf
<<'EOF'
81 ssh_identity /root/.ssh/id_rsa
83 # so we only run one at a time
84 lockfile /var/lock/btrbk.lock
86 # default format of short does not accomidate hourly preservation setting
87 timestamp_format long-iso
89 # only make a snapshot if things have changed
90 snapshot_create onchange
91 # much less snapshots because I have less space on the
93 #snapshot_preserve 2h 2d
94 # for now, keeping them equal for simplicity sake
95 snapshot_preserve 48h 14d 8w 24m
96 snapshot_preserve_min 6h
99 # so, total backups = ~89
100 target_preserve 48h 14d 8w 24m
101 target_preserve_min 6h
103 # if something fails and it's not obvious, try doing
104 # btrbk -l debug -v dryrun
107 # note q is owned by root:1000
108 # note p is owned 1000:1000 and chmod 700
110 if awk '{print $2}' /etc
/fstab |
grep -xF /p
&>/dev
/null
; then
114 # if our mountpoints are from stale snapshots,
115 # it doesn't make sense to do a backup.
116 check-subvol-stale
${mountpoints[@]} ||
exit 1
118 if [[ ! $targets ]]; then
121 if ! timeout
-s 9 10 ssh frodo
:; then
122 targets
=($HOME_DOMAIN)
130 # for i, we just do a 1 way sync from master to backup,
131 # and manually manage any changes to that.
133 for tg
in ${targets[@]}; do
134 # for an initial run, btrbk requires the dir to exist
135 ssh root@
$tg mkdir
-p /mnt
/root
/btrbk
136 if [[ $tg == frodo
&& $HOSTNAME == treetowl
]]; then
144 for m
in ${mountpoints[@]}; do
147 for tg
in ${targets[@]}; do
164 # todo: umount first to ensure we don't have any errors
165 # todo: do some kill fuser stuff to make umount more reliable
166 # todo: run this on a systemd timer on $primary, once per hour,
167 # and if primary is, change that timer over to primary, and make
168 # sure we mount the latest
177 btrbk
-n $resume_arg run
179 # -q and just using the syslog option seemed nice,
180 # but it doesn't show when a send has a parent and when it doesn't.
181 btrbk
$resume_arg run
184 # if we have /p, rsync to targets without /p
185 if mountpoint
/p
>/dev
/null
; then
186 for tg
in ${targets[@]}; do
189 for x
in /p
/c
/machine_specific
/*.hosts
; do
190 if grep -qxF $tg $x; then
201 $script_dir/mount-latest-remote
${targets[@]}
205 # todo: move variable data we don't care about backing up
206 # to /nocow and symlink it.
209 # background on btrbk timezones. with short/long, timestamps use local time.
210 # for long, if your local time moves backwards, by moving timezones or
211 # for an hour when daylight savings changes it, you will temporarily get
212 # a more aggressive retention policy for the overlapping period, and
213 # vice versa for the opposite timezone move. The alternative is using
214 # long-iso, which puts timezone info into the timestamp, which means
215 # that instead of shifting time, you shift the start of day/week/month
216 # which is used for retention to your new local time, which means for
217 # example, if you moved forward by 8 hours, the daily/weekly/monthly
218 # retention will be 8 hours more aggressive since midnight is at a new
219 # time, unless you fake the timzeone using the TZ env variable.
220 # However, in the short term, there will be no inconsistencies.
221 # I don't see any problem with shifting when the day starts for
222 # retention, so I'm using long-iso.
224 # note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z