485c7f579a2d5b54a0ee8c438315eb5c65094d94
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 # btrbk arg: Resume only. Skips snapshot creation.
42 -r) resume_arg
=-r; shift ;;
43 -t) IFS
=, targets
=($2); shift 2 ;;
46 *) echo "$0: Internal error!" ; exit 1 ;;
51 ##### end command line parsing ########
56 m rsync
$dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
60 cat >>/etc
/btrbk.conf
<<EOF
65 cat >>/etc
/btrbk.conf
<<EOF
70 cat >>/etc
/btrbk.conf
<<EOF
71 target send-receive ssh://$tg$vol/btrbk
74 m
() { printf "%s\n" "$*"; "$@"; }
77 if ! which btrbk
&>/dev
/null
; then
78 echo "$0: error: no btrbk binary found"
81 if [[ $
- == *i
* ]]; then
82 progress_arg
=--progress
85 cat >/etc
/btrbk.conf
<<'EOF'
86 ssh_identity /root/.ssh/id_rsa
87 # Just a guess that local7 is a good facility to pick.
88 # It's a bit odd that the transaction log has to be logged to
89 # a file or syslog, while other output is sent to std out.
90 # The man does not mention a way for them to be together, but
91 # I dunno if setting a log level like warn might also output
93 transaction_syslog local7
95 # so we only run one at a time
96 lockfile /var/lock/btrbk.lock
98 # default format of short does not accomidate hourly preservation setting
99 timestamp_format long-iso
101 # only make a snapshot if things have changed
102 snapshot_create onchange
103 # much less snapshots because I have less space on the
105 #snapshot_preserve 2h 2d
106 # for now, keeping them equal for simplicity sake
107 snapshot_preserve 48h 14d 8w 24m
108 snapshot_preserve_min 6h
111 # so, total backups = ~89
112 target_preserve 48h 14d 8w 24m
113 target_preserve_min 6h
115 # if something fails and it's not obvious, try doing
116 # btrbk -l debug -v dryrun
119 # note q is owned by root:1000
120 # note p is owned 1000:1000 and chmod 700
123 if awk '{print $2}' /etc
/fstab |
grep -xF $qmnt &>/dev
/null
; then
127 # if our mountpoints are from stale snapshots,
128 # it doesn't make sense to do a backup.
129 check-subvol-stale
${mountpoints[@]} ||
exit 1
131 if [[ ! $targets ]]; then
134 if ! timeout
-s 9 10 ssh frodo
:; then
135 targets
=($HOME_DOMAIN)
143 # for i, we just do a 1 way sync from master to backup,
144 # and manually manage any changes to that.
146 for tg
in ${targets[@]}; do
147 # for an initial run, btrbk requires the dir to exist
148 ssh root@
$tg mkdir
-p /mnt
/root
/btrbk
149 if [[ $tg == frodo
&& $HOSTNAME == treetowl
]]; then
157 for m
in ${mountpoints[@]}; do
160 for tg
in ${targets[@]}; do
177 # todo: umount first to ensure we don't have any errors
178 # todo: do some kill fuser stuff to make umount more reliable
179 # todo: run this on a systemd timer on $primary, once per hour,
180 # and if primary is, change that timer over to primary, and make
181 # sure we mount the latest
190 m btrbk
-n $resume_arg run
192 # -q and just using the syslog option seemed nice,
193 # but it doesn't show when a send has a parent and when it doesn't.
194 m btrbk
$progress_arg $resume_arg run
197 # if we have /p, rsync to targets without /p
198 if mountpoint
/p
>/dev
/null
; then
199 for tg
in ${targets[@]}; do
202 for x
in /p
/c
/machine_specific
/*.hosts
; do
203 if grep -qxF $tg $x; then
214 m
$script_dir/mount-latest-remote
${targets[@]}
218 # todo: move variable data we don't care about backing up
219 # to /nocow and symlink it.
222 # background on btrbk timezones. with short/long, timestamps use local time.
223 # for long, if your local time moves backwards, by moving timezones or
224 # for an hour when daylight savings changes it, you will temporarily get
225 # a more aggressive retention policy for the overlapping period, and
226 # vice versa for the opposite timezone move. The alternative is using
227 # long-iso, which puts timezone info into the timestamp, which means
228 # that instead of shifting time, you shift the start of day/week/month
229 # which is used for retention to your new local time, which means for
230 # example, if you moved forward by 8 hours, the daily/weekly/monthly
231 # retention will be 8 hours more aggressive since midnight is at a new
232 # time, unless you fake the timzeone using the TZ env variable.
233 # However, in the short term, there will be no inconsistencies.
234 # I don't see any problem with shifting when the day starts for
235 # retention, so I'm using long-iso.
237 # note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z