various changes around data volumes
[distro-setup] / btrbk-run
index 4f4739d954e39d33c423ea59d06e174244893316..8cae3a79cb744f85edbd662fc192e7f6afdcaa85 100755 (executable)
--- a/btrbk-run
+++ b/btrbk-run
@@ -1,25 +1,31 @@
-#!/bin/bash -l
+#!/bin/bash
 
 set -eE -o pipefail
 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
 }
 
+script_dir=$(dirname $(readlink -f "$BASH_SOURCE"))
+
+# todo: finish figuring out fai / distro-setup
+# initial fstab / subvol setup.
+
 conf_only=false
 dry_run=false # mostly for testing
+resume_arg=
 
-temp=$(getopt -l help,long-opt hcnt "$@") || usage 1
+temp=$(getopt -l help hcnrt: "$@") || 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 ;;
+        -r) resume_arg=-r; shift ;;
         -t) IFS=, targets=($2); shift 2 ;;
         -h|--help) usage ;;
         --) shift; break ;;
@@ -30,80 +36,36 @@ 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:/"
 }
 
-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)
+vol-conf() {
+    cat >>/etc/btrbk.conf <<EOF
+volume $vol
+EOF
+}
+sub-conf() {
+    cat >>/etc/btrbk.conf <<EOF
+subvolume $sub
+EOF
+}
+tg-conf() {
+    cat >>/etc/btrbk.conf <<EOF
+target send-receive ssh://$tg$vol/btrbk
+EOF
 }
 
-# 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
+if ! which btrbk &>/dev/null; then
+    echo "$0: error: no btrbk binary found"
 fi
 
-for tg in ${targets[@]}; do
-    cat >/etc/btrbk.conf <<'EOF'
+cat >/etc/btrbk.conf <<'EOF'
 ssh_identity /root/.ssh/id_rsa
-transaction_syslog daemon
 
 # so we only run one at a time
 lockfile                   /var/lock/btrbk.lock
@@ -115,7 +77,11 @@ timestamp_format long-iso
 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
@@ -125,36 +91,92 @@ target_preserve_min 6h
 # btrbk -l debug -v dryrun
 EOF
 
-    remote_target="target send-receive ssh://${tg}/mnt/root"
+# note q is owned by root:1000
+# note p is owned 1000:1000 and chmod 700
+mountpoints=(/q)
+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)
+            if ! timeout -s 9 10 ssh frodo :; then
+                targets=($HOME_DOMAIN)
+            fi
+            ;;
+    esac
+    targets=(frodo)
+fi
+
+
+# for i, we just do a 1 way sync from master to backup,
+# and manually manage any changes to that.
+do_i=false
+for tg in ${targets[@]}; do
+    # for an initial run, btrbk requires the dir to exist
+    ssh root@$tg mkdir -p /mnt/root/btrbk
     if [[ $tg == frodo && $HOSTNAME == treetowl ]]; then
-        target-section /mnt/iroot i
+        do_i=true
     fi
-    for m in ${mountpoints[@]}; do
-        target-section /mnt/root ${m##*/}
+done
+
+
+vol=/mnt/root
+vol-conf
+for m in ${mountpoints[@]}; do
+    sub=${m##*/}
+    sub-conf
+    for tg in ${targets[@]}; do
+        tg-conf
     done
 done
 
+if $do_i; then
+    vol=/mnt/iroot
+    vol-conf
+    sub=i
+    sub-conf
+    tg=frodo
+    vol=/mnt/root
+    tg-conf
+fi
+
+
+
+# todo: umount first to ensure we don't have any errors
+# todo: do some kill fuser stuff to make umount more reliable
+# todo: run this on a systemd timer on $primary, once per hour,
+# and if primary is, change that timer over to primary, and make
+# sure we mount the latest
+
+
+
 if $conf_only; then
     exit
 fi
 
 if $dry_run; then
-    btrbk -n run
+    btrbk -n $resume_arg run
 else
-    btrbk -q run
+    # -q and just using the syslog option seemed nice,
+    # but it doesn't show when a send has a parent and when it doesn't.
+    btrbk $resume_arg run
 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
                 ;;
@@ -162,36 +184,13 @@ if mountpoint /p; then
     done
 fi
 
-first_root=$(awk '$2 == "/mnt/root" {print $1}' /etc/mtab)
+if ! $dry_run; then
+    $script_dir/mount-latest-remote ${targets[@]}
+fi
 
-# 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
+# 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.
@@ -208,3 +207,5 @@ fi
 # 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