4 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
6 [[ $EUID == 0 ]] ||
exec sudo
-E "$BASH_SOURCE" "$@"
9 echo "top of script file:"
10 sed -n '1,/^[# ]*end command line/{p;b};q' "$0"
14 script_dir
=$
(dirname $
(readlink
"$BASH_SOURCE"))
16 # todo: finish figuring out fai / distro-setup
17 # initial fstab / subvol setup.
20 dry_run
=false
# mostly for testing
22 temp
=$
(getopt
-l help hcnt
: "$@") || usage
1
26 -c) conf_only
=true
; shift ;;
27 -n) dry_run
=true
; dry_run_arg
=-n; shift ;;
28 -t) IFS
=, targets
=($2); shift 2 ;;
31 *) echo "$0: Internal error!" ; exit 1 ;;
36 ##### end command line parsing ########
41 mountpoint
$root &>/dev
/null ||
return
42 cat >>/etc
/btrbk.conf
<<EOF
53 rsync
$dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
57 # note q is owned by root:1000
58 # note p is owned 1000:1000 and chmod 700
60 if awk '{print $2}' /etc
/fstab |
grep -xF /p
&>/dev
/null
; then
64 # if our mountpoints are from stale snapshots,
65 # it doesn't make sense to do a backup.
66 check-subvol-stale
${mountpoints[@]} ||
exit 1
68 if [[ ! $targets ]]; then
71 if ! timeout
-s 9 10 ssh frodo
:; then
72 targets
=($HOME_DOMAIN)
80 # todo: make bash shell prompt show something when
81 # a subvol on current host is not fresh.
82 # umount first to ensure we don't have any errors
83 # todo: do some kill fuser stuff to make umount more reliable
84 # todo: run this on a systemd timer on $primary, once per hour.
85 # todo: setup lock so that if this is already running, we exit out, so
86 # that manual runs don't interfere with cronjobs.
88 for tg
in ${targets[@]}; do
89 cat >/etc
/btrbk.conf
<<'EOF'
90 ssh_identity /root/.ssh/id_rsa
91 transaction_syslog daemon
93 # so we only run one at a time
94 lockfile /var/lock/btrbk.lock
96 # default format of short does not accomidate hourly preservation setting
97 timestamp_format long-iso
99 # only make a snapshot if things have changed
100 snapshot_create onchange
101 # much less snapshots because I have less space on the
103 #snapshot_preserve 2h 2d
104 # for now, keeping them equal for simplicity sake
105 snapshot_preserve 48h 14d 8w 24m
106 snapshot_preserve_min 6h
109 # so, total backups = ~89
110 target_preserve 48h 14d 8w 24m
111 target_preserve_min 6h
113 # if something fails and it's not obvious, try doing
114 # btrbk -l debug -v dryrun
117 remote_target
="target send-receive ssh://${tg}/mnt/root/btrbk"
119 if [[ $tg == frodo
&& $HOSTNAME == treetowl
]]; then
120 target-section
/mnt
/iroot i
122 for d
in ${mountpoints[@]}; do
123 target-section
/mnt
/root
${d##*/}
137 # if we have /p, rsync to targets without /p
138 if mountpoint
/p
>/dev
/null
; then
139 for tg
in ${targets[@]}; do
142 for x
in /p
/c
/machine_specific
/*.hosts
; do
143 if grep -qxF $tg $x; then
154 for tg
in ${targets[@]}; do
155 scp
$script_dir/{mount-latest-subvol
,check-subvol-stale
} \
156 root@tg
:/usr
/local
/bin
157 ssh root@
$tg bash
<<'EOF'
159 chmod +x /usr/local/bin/{mount-latest-subvol,check-subvol-stale}
166 # todo: move variable data we don't care about backing up
167 # to /nocow and symlink it.
170 # background on btrbk timezones. with short/long, timestamps use local time.
171 # for long, if your local time moves backwards, by moving timezones or
172 # for an hour when daylight savings changes it, you will temporarily get
173 # a more aggressive retention policy for the overlapping period, and
174 # vice versa for the opposite timezone move. The alternative is using
175 # long-iso, which puts timezone info into the timestamp, which means
176 # that instead of shifting time, you shift the start of day/week/month
177 # which is used for retention to your new local time, which means for
178 # example, if you moved forward by 8 hours, the daily/weekly/monthly
179 # retention will be 8 hours more aggressive since midnight is at a new
180 # time, unless you fake the timzeone using the TZ env variable.
181 # However, in the short term, there will be no inconsistencies.
182 # I don't see any problem with shifting when the day starts for
183 # retention, so I'm using long-iso.
185 # note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z