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"
15 dry_run
=false
# mostly for testing
17 temp
=$
(getopt
-l help,long-opt hcnt
"$@") || usage
1
21 -c) conf_only
=true
; shift ;;
22 -n) dry_run
=true
; dry_run_arg
=-n; shift ;;
23 -t) IFS
=, targets
=($2); shift 2 ;;
26 *) echo "$0: Internal error!" ; exit 1 ;;
31 ##### end command line parsing ########
33 sed="sed -r --follow-symlinks"
39 mountpoint
$root &>/dev
/null ||
return
40 cat >>/etc
/btrbk.conf
<<EOF
51 rsync
$dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
59 printf "%s %s\n" $
(date -d $
(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s
) $f
60 done |
sort -r |
head -n 1 |
awk '{print $2}'
62 last_snaps
+=($last_snap)
65 # note q is owned by root:1000
66 # note p is owned 1000:1000 and chmod 700
68 if mountpoint
/p
; then
72 if [[ ! $targets ]]; then
75 if ! timeout
-s 9 10 ssh frodo
:; then
76 targets
=($HOME_DOMAIN)
84 # umount first to ensure we don't have any errors
85 # todo: do some kill fuser stuff to make umount more reliable
86 # todo: setup sync systemd timer on $primary, once per hour.
87 # todo: setup lock so that if this is already running, we exit out, so
88 # that manual runs don't interfere with cronjobs.
89 if [[ $primary ]] && ! $dry_run; then
90 for m
in ${mountpoints[@]}; do
91 # note, this won't work for /i, due to path being /mnt/iroot
92 # todo: include /i for treetowl/frodo
93 btrfs property
set -ts /mnt
/root
$m ro true
94 ssh root@
$primary bash
<<EOF
97 [[ -e /mnt/root$m ]] || exit 0
98 btrfs sub del /mnt/root$m
103 for tg
in ${targets[@]}; do
104 cat >/etc
/btrbk.conf
<<'EOF'
105 ssh_identity /root/.ssh/id_rsa
106 transaction_syslog daemon
108 # so we only run one at a time
109 lockfile /var/lock/btrbk.lock
111 # default format of short does not accomidate hourly preservation setting
112 timestamp_format long-iso
114 # only make a snapshot if things have changed
115 snapshot_create onchange
116 # much less snapshots because I have less space on the
118 snapshot_preserve 2h 2d
120 # so, total backups = ~89
121 target_preserve 48h 14d 8w 24m
122 target_preserve_min 6h
124 # if something fails and it's not obvious, try doing
125 # btrbk -l debug -v dryrun
128 remote_target
="target send-receive ssh://${tg}/mnt/root"
130 if [[ $tg == frodo
&& $HOSTNAME == treetowl
]]; then
131 target-section
/mnt
/iroot i
133 for m
in ${mountpoints[@]}; do
134 target-section
/mnt
/root
${m##*/}
148 # if we have /p, rsync to targets without /p
149 if mountpoint
/p
; then
150 for tg
in ${targets[@]}; do
154 for x
in /p
/c
/machine_specific
/*.hosts
; do
155 if grep -qxF $tg $x; then
157 rsync-dirs
${dir##*/} $dir
165 first_root
=$
(awk '$2 == "/mnt/root" {print $1}' /etc
/mtab
)
167 # make $primary have the rw snapshot
168 if [[ $primary ]] && ! $dry_run; then
170 for m
in ${mountpoints[@]}; do
172 fstab
+=("$first_root $m btrfs noatime,subvol=$last_snap 0 0")
175 printf "%s\n" "${fstab[@]}" | cedit
/etc
/fstab
176 for d
in ${mountpoints[@]}; do
178 btrfs sub del
/mnt
/root
$d
180 ssh root@primary bash
-s "${mountpoints[*]}" "${last_snaps[*]}" <<'EOF'
184 first_root=$(awk '$2 == "/mnt/root" {print $1}' /etc/mtab)
185 for ((i=0; i < ${#mountpoints[@]}; i++)); do
188 fstab+=("$first_root $m btrfs noatime,subvol=$vol 0 0")
190 btrfs sub snapshot ${last_snaps[i]} $vol
197 # background on btrbk timezones. with short/long, timestamps use local time.
198 # for long, if your local time moves backwards, by moving timezones or
199 # for an hour when daylight savings changes it, you will temporarily get
200 # a more aggressive retention policy for the overlapping period, and
201 # vice versa for the opposite timezone move. The alternative is using
202 # long-iso, which puts timezone info into the timestamp, which means
203 # that instead of shifting time, you shift the start of day/week/month
204 # which is used for retention to your new local time, which means for
205 # example, if you moved forward by 8 hours, the daily/weekly/monthly
206 # retention will be 8 hours more aggressive since midnight is at a new
207 # time, unless you fake the timzeone using the TZ env variable.
208 # However, in the short term, there will be no inconsistencies.
209 # I don't see any problem with shifting when the day starts for
210 # retention, so I'm using long-iso.