various fixes
[distro-setup] / btrbk-run
index 7a1d946c88f064eafae23ae27bccd53511114aad..eb8a7b5db7e9c89ea051095f55399de541d7e31d 100755 (executable)
--- a/btrbk-run
+++ b/btrbk-run
@@ -17,6 +17,7 @@ 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"
@@ -27,33 +28,32 @@ script_dir=$(dirname $(readlink -f "$BASH_SOURCE"))
 
 # note q is owned by root:1000
 # note p/m is owned 1000:1000 and chmod 700
-mountpoints=(/a)
-private_mountpoints=(/q)
+
+
+targets=()
+mountpoints=()
+
 rsync_mountpoint=/q
 
 conf_only=false
 dry_run=false # mostly for testing
 resume_arg=
-do_i=true
-if [[ $HOSTNAME == $MAIL_HOST ]]; then
-    do_o=true
-else
-    do_o=false
-fi
 
 default_args_file=/etc/btrbk-run.conf
-if [[ -r $default_args_file ]]; then
+if [[ -s $default_args_file ]]; then
+    echo "$0: warning: options file default options set:"
+    cat $default_args_file
+    sleep 5
     set -- $(< $default_args_file) "$@"
 fi
 
-temp=$(getopt -l help hcinoprt: "$@") || usage 1
+temp=$(getopt -l help hcm:nprt: "$@") || usage 1
 eval set -- "$temp"
 while true; do
     case $1 in
         -c) conf_only=true; shift ;;
-        -i) do_i=false; shift ;;
+        -m) IFS=, mountpoints=($2); unset IFS; shift 2 ;;
         -n) dry_run=true; dry_run_arg=-n; shift ;;
-        -o) do_o=false; shift ;;
         -p) progress_arg="--progress"; shift ;;
         # btrbk arg: Resume only. Skips snapshot creation.
         -r) resume_arg=-r; shift ;;
@@ -64,10 +64,54 @@ while true; do
     esac
 done
 
-if $do_o; then
-    private_mountpoints+=(/o)
+echo "$0: options: conf_only=$conf_only, dry_run=$dry_run, resume_arg=$resume_arg"
+
+# set default targets
+if ! (( ${#targets[@]} )); then
+    case $HOSTNAME in
+        x2)
+            if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
+                targets=($HOME_DOMAIN)
+            fi
+            ;;
+        treetowl)
+            targets=(frodo)
+            if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
+                if timeout -s 9 10 ssh x2 :; then
+                    targets+=(x2)
+                fi
+            fi
+            ;;
+        *)
+            echo "$0: error: no default targets for this host, use -t"
+            exit 1
+            ;;
+    esac
+fi
+
+echo "targets: ${targets[*]}"
+
+
+
+# set default mountpoints
+if ! (( ${#mountpoints[@]} )); then
+    prospective_mps=(/a /q)
+    if [[ $HOSTNAME == "$MAIL_HOST" ]]; then
+        prospective_mps+=(/o)
+    fi
+    for tg in ${targets[@]}; do
+        if [[ $tg == frodo && $HOSTNAME == treetowl ]]; then
+            prospective_mps+=(/i)
+        fi
+    done
+    for mp in ${prospective_mps[@]}; do # default mountpoints to sync
+        if awk '{print $2}' /etc/fstab | grep -xF $mp &>/dev/null; then
+            mountpoints+=($mp)
+        fi
+    done
 fi
-read primary <<<"$@" # not yet used
+
+echo "mountpoints: ${mountpoints[*]}"
 
 ##### end command line parsing ########
 
@@ -132,82 +176,41 @@ target_preserve_min 4h
 # btrbk -l debug -v dryrun
 EOF
 
-for mp in ${private_mountpoints[@]}; do # private mountpoints
-    if awk '{print $2}' /etc/fstab | grep -xF $mp &>/dev/null; then
-        mountpoints+=($mp)
-    fi
-done
 
 # 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
-            ;;
-        treetowl)
-            targets=(frodo)
-            if timeout -s 9 10 ssh x2 :; then
-                targets+=(x2)
-            fi
-            ;;
-        *)
-            targets=(frodo)
-            ;;
-    esac
-fi
-
-
-echo "targets: ${targets[*]}"
-
-
-# for i, we just do a 1 way sync from master to backup,
-# and manually manage any changes to that.
-i_possible=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
-        i_possible=true
-    fi
 done
-if ! $i_possible; then
-    do_i=false
-fi
 
 
-vol=/mnt/root
-vol-conf
 for m in ${mountpoints[@]}; do
-    sub=${m##*/}
-    sub-conf
-    for tg in ${targets[@]}; do
+    # for /i, some special cases. there is just one static target and direction.
+    if [[ $m == /i ]]; then
+        vol=/mnt/iroot
+        vol-conf
+        sub=i
+        sub-conf
+        tg=frodo
+        vol=/mnt/root
         tg-conf
-    done
+    else
+        vol=/mnt/root
+        vol-conf
+        sub=${m##*/}
+        sub-conf
+        for tg in ${targets[@]}; do
+            tg-conf
+        done
+    fi
 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