-#!/bin/bash -l
+#!/bin/bash
set -eE -o pipefail
trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
exit $1
}
+script_dir=$(dirname $(readlink "$BASH_SOURCE"))
+
+# todo: finish figuring out fai / distro-setup
+# initial fstab / subvol setup.
+
conf_only=false
dry_run=false # mostly for testing
-temp=$(getopt -l help,long-opt hcnt "$@") || usage 1
+temp=$(getopt -l help hcnt: "$@") || usage 1
eval set -- "$temp"
while true; do
case $1 in
##### end command line parsing ########
-sed="sed -r --follow-symlinks"
-last_snaps=()
-
target-section() {
local root=$1
local subvol=$2
rsync $dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
}
-last-snap() {
- vol=${1##*/}
- cd /mnt/root
- last_snap=$(
- for f in $vol.20*; do
- printf "%s %s\n" $(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s) $f
- done | sort -r | head -n 1 | awk '{print $2}'
- )
- last_snaps+=($last_snap)
-}
# note q is owned by root:1000
# note p is owned 1000:1000 and chmod 700
mountpoints=(/q)
-if mountpoint /p; then
+if awk '{print $2}' /etc/fstab | grep -xF /p &>/dev/null; then
mountpoints+=(/p)
fi
+# if our mountpoints are from stale snapshots,
+# it doesn't make sense to do a backup.
+check-subvol-stale ${mountpoints[@]} || exit 1
+
if [[ ! $targets ]]; then
case $HOSTNAME in
tp|x2)
fi
+# todo: make bash shell prompt show something when
+# a subvol on current host is not fresh.
# umount first to ensure we don't have any errors
# todo: do some kill fuser stuff to make umount more reliable
-# todo: setup sync systemd timer on $primary, once per hour.
+# todo: run this on a systemd timer on $primary, once per hour.
# todo: setup lock so that if this is already running, we exit out, so
# that manual runs don't interfere with cronjobs.
-if [[ $primary ]] && ! $dry_run; then
- for m in ${mountpoints[@]}; do
- # note, this won't work for /i, due to path being /mnt/iroot
- # todo: include /i for treetowl/frodo
- btrfs property set -ts /mnt/root$m ro true
- ssh root@$primary bash <<EOF
-set -ex
-umount $m
-[[ -e /mnt/root$m ]] || exit 0
-btrfs sub del /mnt/root$m
-EOF
- done
-fi
for tg in ${targets[@]}; do
cat >/etc/btrbk.conf <<'EOF'
snapshot_create onchange
# much less snapshots because I have less space on the
# local filesystem.
-snapshot_preserve 2h 2d
+#snapshot_preserve 2h 2d
+# for now, keeping them equal for simplicity sake
+snapshot_preserve 48h 14d 8w 24m
+snapshot_preserve_min 6h
+snapshot_dir btrbk
# so, total backups = ~89
target_preserve 48h 14d 8w 24m
# btrbk -l debug -v dryrun
EOF
- remote_target="target send-receive ssh://${tg}/mnt/root"
+ remote_target="target send-receive ssh://${tg}/mnt/root/btrbk"
if [[ $tg == frodo && $HOSTNAME == treetowl ]]; then
target-section /mnt/iroot i
fi
- for m in ${mountpoints[@]}; do
- target-section /mnt/root ${m##*/}
+ for d in ${mountpoints[@]}; do
+ target-section /mnt/root ${d##*/}
done
done
fi
# if we have /p, rsync to targets without /p
-if mountpoint /p; then
+if mountpoint /p >/dev/null; then
for tg in ${targets[@]}; do
case $tg in
tp|li|lk)
- # todo, test this
for x in /p/c/machine_specific/*.hosts; do
if grep -qxF $tg $x; then
dir=${x%.hosts}
- rsync-dirs ${dir##*/} $dir
+ rsync-dirs $tg $dir
fi
done
;;
done
fi
-first_root=$(awk '$2 == "/mnt/root" {print $1}' /etc/mtab)
-
-# make $primary have the rw snapshot
-if [[ $primary ]] && ! $dry_run; then
- fstab=()
- for m in ${mountpoints[@]}; do
- last-snap $m
- fstab+=("$first_root $m btrfs noatime,subvol=$last_snap 0 0")
- done
-
- printf "%s\n" "${fstab[@]}" | cedit /etc/fstab
- for d in ${mountpoints[@]}; do
- mount $d
- btrfs sub del /mnt/root$d
- done
- ssh root@primary bash -s "${mountpoints[*]}" "${last_snaps[*]}" <<'EOF'
-set -xe
-mountpoints=($1)
-last_snaps=($2)
-first_root=$(awk '$2 == "/mnt/root" {print $1}' /etc/mtab)
-for ((i=0; i < ${#mountpoints[@]}; i++)); do
- m=${mountpoints[i]}
- vol=${m##*/}
- fstab+=("$first_root $m btrfs noatime,subvol=$vol 0 0")
- cd /mnt/root
- btrfs sub snapshot ${last_snaps[i]} $vol
- mount $m
-done
+if ! $dry_run; then
+ for tg in ${targets[@]}; do
+ scp $script_dir/{mount-latest-subvol,check-subvol-stale} \
+ root@tg:/usr/local/bin
+ ssh root@$tg bash <<'EOF'
+set -e
+chmod +x /usr/local/bin/{mount-latest-subvol,check-subvol-stale}
+mount-latest-subvol
EOF
+ done
fi
+# todo: move variable data we don't care about backing up
+# to /nocow and symlink it.
+
+
# background on btrbk timezones. with short/long, timestamps use local time.
# for long, if your local time moves backwards, by moving timezones or
# for an hour when daylight savings changes it, you will temporarily get
# However, in the short term, there will be no inconsistencies.
# I don't see any problem with shifting when the day starts for
# retention, so I'm using long-iso.
+
+# note to create a long-iso timestamp: date +%Y%m%dT%H%M%S%z