wip using btrfs send for sync
[distro-setup] / btrbk-run
index 9ca4eb3333eaffbaa8b176a71d73d240c5530d6b..4f4739d954e39d33c423ea59d06e174244893316 100755 (executable)
--- a/btrbk-run
+++ b/btrbk-run
@@ -5,32 +5,103 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 
 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
 
+usage() {
+    echo "top of script file:"
+    sed -n '1,/^[# ]*end command line/{p;b};q' "$0"
+    exit $1
+}
+
 conf_only=false
-dry_run=false
-# mostly for testing
-case $1 in
-    -c) conf_only=true ;;
-    -n) dry_run=true ;;
-    ?*) echo "$0: error: unsupported arg"; exit 1 ;;
-esac
-
-# background on 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
-# a more aggressive retention policy for the overlapping period, and
-# vice versa for the opposite timezone move. The alternative is using
-# long-iso, which puts timezone info into the timestamp, which means
-# that instead of shifting time, you shift the start of day/week/month
-# which is used for retention to your new local time, which means for
-# example, if you moved forward by 8 hours, the daily/weekly/monthly
-# retention will be 8 hours more aggressive since midnight is at a new
-# time, unless you fake the timzeone using the TZ env variable.
-# 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.
+dry_run=false # mostly for testing
+
+temp=$(getopt -l help,long-opt hcnt "$@") || usage 1
+eval set -- "$temp"
+while true; do
+    case $1 in
+        -c) conf_only=true; shift ;;
+        -n) dry_run=true; dry_run_arg=-n; shift ;;
+        -t) IFS=, targets=($2); shift 2 ;;
+        -h|--help) usage ;;
+        --) shift; break ;;
+        *) echo "$0: Internal error!" ; exit 1 ;;
+    esac
+done
+read primary <<<"$@"
+
+##### end command line parsing ########
+
+sed="sed -r --follow-symlinks"
+last_snaps=()
+
+target-section() {
+    local root=$1
+    local subvol=$2
+    mountpoint $root &>/dev/null || return
+    cat >>/etc/btrbk.conf <<EOF
+volume $root
+subvolume $subvol
+$remote_target
+
+EOF
+}
+
+rsync-dirs() {
+    local host=$1
+    local path=$2
+    rsync $dry_run_arg -ahi --relative --delete "$path" "root@$host:/"
+}
 
-target_host=frodo
-cat >/etc/btrbk.conf <<'EOF'
+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
+    mountpoints+=(/p)
+fi
+
+if [[ ! $targets ]]; then
+    case $HOSTNAME in
+        tp|x2)
+            if ! timeout -s 9 10 ssh frodo :; then
+                targets=($HOME_DOMAIN)
+            fi
+            ;;
+    esac
+    targets=(frodo)
+fi
+
+
+# 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: 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'
 ssh_identity /root/.ssh/id_rsa
 transaction_syslog daemon
 
@@ -54,36 +125,15 @@ target_preserve_min 6h
 # btrbk -l debug -v dryrun
 EOF
 
+    remote_target="target send-receive ssh://${tg}/mnt/root"
 
-case $HOSTNAME in
-    tp|x2)
-        if ! timeout -s 9 10 ssh frodo :; then
-            target_host=$HOME_DOMAIN
-            cat >>/etc/btrbk.conf <<EOF
-ssh_port 2222
-EOF
-        fi
-        ;;
-esac
-
-if [[ $HOSTNAME != frodo ]]; then
-    remote_target="target send-receive ssh://${target_host}/mnt/root/${HOSTNAME}-btrbk"
-fi
-
-target-section() {
-    root=$1
-    subvol=$2
-    mountpoint $root &>/dev/null || return
-    cat >>/etc/btrbk.conf <<EOF
-volume $root
-subvolume $subvol
-$remote_target
-
-EOF
-}
-
-target-section /mnt/iroot i
-target-section /mnt/root q
+    if [[ $tg == frodo && $HOSTNAME == treetowl ]]; then
+        target-section /mnt/iroot i
+    fi
+    for m in ${mountpoints[@]}; do
+        target-section /mnt/root ${m##*/}
+    done
+done
 
 if $conf_only; then
     exit
@@ -94,3 +144,67 @@ if $dry_run; then
 else
     btrbk -q run
 fi
+
+# if we have /p, rsync to targets without /p
+if mountpoint /p; 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
+                    fi
+                done
+                ;;
+        esac
+    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
+EOF
+fi
+
+
+# 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
+# a more aggressive retention policy for the overlapping period, and
+# vice versa for the opposite timezone move. The alternative is using
+# long-iso, which puts timezone info into the timestamp, which means
+# that instead of shifting time, you shift the start of day/week/month
+# which is used for retention to your new local time, which means for
+# example, if you moved forward by 8 hours, the daily/weekly/monthly
+# retention will be 8 hours more aggressive since midnight is at a new
+# time, unless you fake the timzeone using the TZ env variable.
+# 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.