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
23 temp
=$
(getopt
-l help hcnrt
: "$@") || usage
1
27 -c) conf_only
=true
; shift ;;
28 -n) dry_run
=true
; dry_run_arg
=-n; shift ;;
29 -r) resume_arg
=-r; shift ;;
30 -t) IFS
=, targets
=($2); shift 2 ;;
33 *) echo "$0: Internal error!" ; exit 1 ;;
38 ##### end command line parsing ########
43 mountpoint
$root &>/dev
/null ||
return
44 cat >>/etc
/btrbk.conf
<<EOF
55 rsync
$dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
59 # note q is owned by root:1000
60 # note p is owned 1000:1000 and chmod 700
62 if awk '{print $2}' /etc
/fstab |
grep -xF /p
&>/dev
/null
; then
66 # if our mountpoints are from stale snapshots,
67 # it doesn't make sense to do a backup.
68 check-subvol-stale
${mountpoints[@]} ||
exit 1
70 if [[ ! $targets ]]; then
73 if ! timeout
-s 9 10 ssh frodo
:; then
74 targets
=($HOME_DOMAIN)
82 # todo: make bash shell prompt show something when
83 # a subvol on current host is not fresh.
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: run this on a systemd timer on $primary, once per hour,
87 # and if primary is, change that timer over to primary, and make
88 # sure we mount the latest
89 # todo: setup lock so that if this is already running, we exit out, so
90 # that manual runs don't interfere with cronjobs.
92 for tg
in ${targets[@]}; do
93 cat >/etc
/btrbk.conf
<<'EOF'
94 ssh_identity /root/.ssh/id_rsa
95 transaction_syslog daemon
97 # so we only run one at a time
98 lockfile /var/lock/btrbk.lock
100 # default format of short does not accomidate hourly preservation setting
101 timestamp_format long-iso
103 # only make a snapshot if things have changed
104 snapshot_create onchange
105 # much less snapshots because I have less space on the
107 #snapshot_preserve 2h 2d
108 # for now, keeping them equal for simplicity sake
109 snapshot_preserve 48h 14d 8w 24m
110 snapshot_preserve_min 6h
113 # so, total backups = ~89
114 target_preserve 48h 14d 8w 24m
115 target_preserve_min 6h
117 # if something fails and it's not obvious, try doing
118 # btrbk -l debug -v dryrun
121 remote_target
="target send-receive ssh://${tg}/mnt/root/btrbk"
123 if [[ $tg == frodo
&& $HOSTNAME == treetowl
]]; then
124 target-section
/mnt
/iroot i
126 for d
in ${mountpoints[@]}; do
127 target-section
/mnt
/root
${d##*/}
136 btrbk
-n $resume_arg run
138 btrbk
-q $resume_arg run
141 # if we have /p, rsync to targets without /p
142 if mountpoint
/p
>/dev
/null
; then
143 for tg
in ${targets[@]}; do
146 for x
in /p
/c
/machine_specific
/*.hosts
; do
147 if grep -qxF $tg $x; then
158 for tg
in ${targets[@]}; do
159 scp
$script_dir/{mount-latest-subvol
,check-subvol-stale
} \
160 root@
$tg:/usr
/local
/bin
161 ssh root@
$tg bash
<<'EOF'
163 chmod +x /usr/local/bin/{mount-latest-subvol,check-subvol-stale}
170 # todo: move variable data we don't care about backing up
171 # to /nocow and symlink it.
174 # background on btrbk timezones. with short/long, timestamps use local time.
175 # for long, if your local time moves backwards, by moving timezones or
176 # for an hour when daylight savings changes it, you will temporarily get
177 # a more aggressive retention policy for the overlapping period, and
178 # vice versa for the opposite timezone move. The alternative is using
179 # long-iso, which puts timezone info into the timestamp, which means
180 # that instead of shifting time, you shift the start of day/week/month
181 # which is used for retention to your new local time, which means for
182 # example, if you moved forward by 8 hours, the daily/weekly/monthly
183 # retention will be 8 hours more aggressive since midnight is at a new
184 # time, unless you fake the timzeone using the TZ env variable.
185 # However, in the short term, there will be no inconsistencies.
186 # I don't see any problem with shifting when the day starts for
187 # retention, so I'm using long-iso.
189 # note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z