fixes, logging, and better shellcheck conformance
[distro-setup] / btrbk-run
index 140f9e6053fdef509aace78d1186fee1c0abe26f..6726eb980c69ebd9cc40ad6c80da13ac42d7e39b 100644 (file)
--- a/btrbk-run
+++ b/btrbk-run
@@ -39,13 +39,14 @@ EOF
 }
 
 
+pre=btrbk-run
 
 script_name="${BASH_SOURCE[0]}"
 script_name="${script_name##*/}"
-m() { if $verbose; then printf "%s\n"  "$*"; fi;  "$@"; }
-e() { printf "%s\n"  "$*"; }
-die() { printf "%s\n" "$*" >&2; echo "exiting with status 1" >&2; exit 1; }
-mexit() { echo ": exiting with status $1"; exit $1; }
+m() { if $verbose; then printf "$pre %s\n"  "$*"; fi;  "$@"; }
+e() { printf "$pre %s\n"  "$*"; }
+die() { printf "$pre error: %s\n" "$*" >&2; echo "$pre exiting with status 1" >&2; exit 1; }
+mexit() { echo "$pre exiting with status $1"; exit $1; }
 
 # latest $MAIL_HOST
 if [[ -e /b/bash_unpublished/source-state ]]; then
@@ -89,8 +90,9 @@ targets=()
 early=false
 fast=false
 kd_spread=false
+check_installed=false
 orig_args=("$@")
-temp=$(getopt -l fast,pull-reexec,help 23cefikl:m:npqrs:t:vh "$@") || usage 1
+temp=$(getopt -l check-installed,fast,pull-reexec,help 23cefikl:m:npqrs:t:vh "$@") || usage 1
 eval set -- "$temp"
 while true; do
   case $1 in
@@ -99,6 +101,9 @@ while true; do
     -3) conf_suf=3 ;;
     # only creates the config file, does not run btrbk
     -c) conf_only=true ;;
+    --check-installed)
+      check_installed=true
+      ;;
     # quit early, just btrbk, no extra remounting etc.
     -e) early=true ;;
     # avoids some default behaviors:
@@ -146,6 +151,31 @@ done
 
 cmd_arg="$1"
 
+
+uninstalled-file-die() {
+  die "uninstalled file $1. run install-my-scripts or rerun with -f"
+}
+
+if ! $force && { $check_installed || [[ ! $source ]]; } ; then
+  install_bin_files=(
+    mount-latest-subvol
+    check-subvol-stale
+    btrbk-run
+  )
+  for f in ${install_bin_files[@]}; do
+    if ! diff -q /a/bin/ds/$f /usr/local/bin/$f; then
+      uninstalled-file-die $f
+    fi
+  done
+  if ! diff -q /a/bin/errhandle/err /usr/local/lib/err; then
+    uninstalled-file-die err
+  fi
+  if $check_installed; then
+    exit 0
+  fi
+fi
+
+
 if $kd_spread; then
   if [[ $cmd_arg && $cmd_arg != resume ]]; then
     die "dont pass -k without resume or empty run arg"
@@ -386,16 +416,21 @@ if ! command -v btrbk &>/dev/null; then
   die "error: no btrbk binary found"
 fi
 
+if ! $pull_reexec && [[ $source ]] && $pulla && ! $force ; then
+  ssh root@$source btrbk-run --check-installed || exit 1
+fi
 
 #### end pre-checks #####
 
+
+
 mkdir -p /var/log/btrbk
 # The journal doesnt go back to my oldest backups, and I've found myself
 # wanting older logs. Not going to bother expiring old logs, since it is
 # fine if they go back years.
 log_path=/var/log/btrbk/$(date +%F_%T%:::z).log
 echo copying output to $log_path
-exec &> >(ts "%F %T" | tee -a $log_path)
+exec &> >(pee cat 'ts "%F %T"|dd of='$log_path' status=none')
 
 
 if $verbose; then
@@ -413,25 +448,6 @@ fi
 echo "mountpoints: ${mountpoints[*]}"
 
 
-uninstalled-file-die() {
-  err "uninstalled file $1. run install-my-scripts or rerun with -f"
-  exit 1
-}
-if ! $force; then
-  install_bin_files=(
-    mount-latest-subvol
-    check-subvol-stale
-    btrbk-run
-  )
-  for f in ${install_bin_files[@]}; do
-    if ! diff -q /a/bin/ds/$f /usr/local/bin/$f; then
-      uninstalled-file-die $f
-    fi
-  done
-  if ! diff -q /a/bin/errhandle/err /usr/local/lib/err; then
-    uninstalled-file-die err
-  fi
-fi
 
 
 # pull_reexec stops us from getting into an infinite loop if there is some
@@ -443,6 +459,7 @@ for m in "${mountpoints[@]}"; do
     break
   fi
 done
+
 if ! $pull_reexec && [[ $source ]] && $pulla ; then
   tmpf=$(mktemp)
   m rsync -ra $source:/usr/local/bin/{mount-latest-subvol,check-subvol-stale} /usr/local/bin