From b37f31cdee02eab5a7a9bb083688c0a90a973e31 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Wed, 26 Jul 2023 14:40:06 -0400 Subject: [PATCH] fixes, logging, and better shellcheck conformance --- brc | 11 ++++++-- brc2 | 3 -- btrbk-run | 67 ++++++++++++++++++++++++++++----------------- check-subvol-stale | 4 ++- mail-setup | 2 +- mount-latest-subvol | 10 +++---- primary-setup | 5 ++-- switch-mail-host | 58 ++++++++++++++++++++++++++------------- 8 files changed, 102 insertions(+), 58 deletions(-) diff --git a/brc b/brc index 7dfaefc..971cb02 100644 --- a/brc +++ b/brc @@ -666,10 +666,17 @@ khcopy() { # ya, hacky hardcoded hostnames in 2023. we could do better hssh-update() { + local -a failed_hosts for host in kd x3.office.fsf.org syw; do e $host - scp /b/fai/fai/config/files/usr/local/bin/hssh/IANK root@$host:/usr/local/bin/hssh + if ! scp /b/fai/fai/config/files/usr/local/bin/hssh/IANK root@$host:/usr/local/bin/hssh; then + failed_hosts+=($host) + fi done + if (( ${#failed_hosts[@]} >= 1 )); then + echo failed_hosts=${failed_hosts[*]} + return 1 + fi } a() { @@ -1664,7 +1671,7 @@ ksu() { # history search unique histrm() { history -n HISTTIMEFORMAT= history | awk -v IGNORECASE=1 '{ a=$1; sub(/^ *[^ ]+ */, "") }; /'"$*"'/' - read -p "press anything but contrl-c to delete" + read -r -p "press anything but contrl-c to delete" for entry in $(HISTTIMEFORMAT= history | awk -v IGNORECASE=1 '{ a=$1; sub(/^ *[^ ]+ */, "") }; /'"$*"'/ { print a }' | tac); do history -d $entry done diff --git a/brc2 b/brc2 index d51add8..025008b 100644 --- a/brc2 +++ b/brc2 @@ -1385,8 +1385,6 @@ bbk() { # btrbk wrapper return 1 ;; esac - # run latest - install-my-scripts # todo: consider changing this to srun and having the args come # from a file like /etc/default/btrbk, like is done in exim s jdo btrbk-run "$@" @@ -1593,7 +1591,6 @@ sm() { # switch mail host if [[ ! $tmp ]]; then s ssh-add /root/.ssh/home fi - install-my-scripts s jdo switch-mail-host "$@" return $ret } diff --git a/btrbk-run b/btrbk-run index 140f9e6..6726eb9 100644 --- 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 diff --git a/check-subvol-stale b/check-subvol-stale index cb49379..fbd31d6 100644 --- a/check-subvol-stale +++ b/check-subvol-stale @@ -77,9 +77,11 @@ stale-file() { fi } +pre="check-subvol-stale:${SSH_CLIENT:+ $HOSTNAME:}" + d() { if $verbose; then - printf "%s\n" "$*" + printf "$pre %s\n" "$*" fi } diff --git a/mail-setup b/mail-setup index 9dd5259..dd87fba 100755 --- a/mail-setup +++ b/mail-setup @@ -303,7 +303,7 @@ fi # * functions & constants -pre="${0##*/}:" +pre="${0##*/}:${SSH_CLIENT:+ $HOSTNAME:}" m() { printf "$pre %s\n" "$*"; "$@"; } e() { printf "$pre %s\n" "$*"; } err() { printf "$pre %s\n" "$*" >&2; exit 1; } diff --git a/mount-latest-subvol b/mount-latest-subvol index 3844e72..cddb714 100644 --- a/mount-latest-subvol +++ b/mount-latest-subvol @@ -38,7 +38,7 @@ EOF exit $1 } - +pre="mount-latest-subvol:${SSH_CLIENT:+ $HOSTNAME:}" tu() { while read -r line; do @@ -48,17 +48,17 @@ tu() { } d() { if $verbose; then - printf "%s\n" "$*" + printf "$pre %s\n" "$*" fi } m() { if $verbose; then - printf "%s\n" "$*" + printf "$pre %s\n" "$*" fi "$@" } x() { - printf "%s\n" "$*" + printf "$pre %s\n" "$*" "$@" } @@ -477,7 +477,7 @@ for dir in /mnt/r7/amy/{root/root,boot/boot}_ubuntubionic /mnt/{root2/root,boot2 rm -f /nocow/btrfs-stale/$vol done -if (( $ret )); then +if (( ret >= 1 )); then echo "$0: exit status $ret. see error above" fi exit $ret diff --git a/primary-setup b/primary-setup index 8683dcf..e7d70f2 100755 --- a/primary-setup +++ b/primary-setup @@ -7,13 +7,14 @@ source /usr/local/lib/err script_name="${BASH_SOURCE[0]}" script_name="${script_name##*/}" -pre="${SSH_CLIENT:+$HOSTNAME} $script_name:" +pre="$script_name:${0##*/}:${SSH_CLIENT:+ $HOSTNAME:}" m() { printf "$pre %s\n" "$*"; "$@"; } e() { printf "$pre %s\n" "$*"; } err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $0: $*" >&2; } # service maybe serm() { - local service=${@: -1} + local service + service="${*: -1}" if [[ ! -s /etc/systemd/system/$service ]]; then return 0 fi diff --git a/switch-mail-host b/switch-mail-host index d0117af..f7d926c 100644 --- a/switch-mail-host +++ b/switch-mail-host @@ -26,7 +26,7 @@ on the internet. Email can wait. Note: Uses GNU getopt options parsing style EOF - exit $1 + exit 0 } script_name="${BASH_SOURCE[0]}" @@ -45,10 +45,11 @@ err-cleanup() { fi } -pre="${SSH_CLIENT:+$HOSTNAME} $script_name:" +pre="$script_name:" m() { printf "$pre %s\n" "$*"; "$@"; } e() { printf "$pre %s\n" "$*"; } err() { echo "$pre ERROR: $*" >&2; } +die() { printf "%s\n" "$*" >&2; echo "exiting with status 1" >&2; exit 1; } if [[ $EUID != 0 ]]; then err "requires running as root" @@ -61,15 +62,25 @@ fi mail_only=false host2_only=false force=false +force_arg= pull_reexec=false mp_args="-m /o,/a,/ar,/q,/qd,/qr" +check_installed=false orig_args=("$@") -temp=$(getopt -l force,pull-reexec,help aioh "$@") || usage 1 +if ! temp=$(getopt -l check-installed,force,pull-reexec,help afioh "$@"); then + err "args invalid. args=$*" +fi eval set -- "$temp" while true; do case $1 in -a) snapshot_arg=resume ;; - --force) force=true ;; + --force|-f) + force=true + force_arg=-f + ;; + --check-installed) + check_installed=true + ;; -i) incremental_arg="-i" ;; # internal option for rerunning under newer old_host when doing pull --pull-reexec) pull_reexec=true;; @@ -83,7 +94,9 @@ while true; do done -(( $# == 2 )) || usage 1 +if (( $# != 2 )) && ! $check_installed; then + err expected 2 args, got $# +fi if [[ ! $HOSTNAME ]]; then err "\$HOSTNAME is unset" @@ -91,11 +104,17 @@ if [[ ! $HOSTNAME ]]; then fi uninstalled-file-die() { - err "uninstalled file $1. run install-my-scripts or rerun with -f" - exit 1 + die "uninstalled file $1. run install-my-scripts or rerun with -f" } -if ! $force; then + +source /a/bin/bash_unpublished/source-state + +direction=$1 +host=$2 + + +if ! $force && { $check_installed || [[ $direction == push ]]; } ; then install_bin_files=( mount-latest-subvol check-subvol-stale @@ -110,12 +129,12 @@ if ! $force; then 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 -source /a/bin/bash_unpublished/source-state -direction=$1 -host=$2 case $direction in push) old_host=$HOSTNAME @@ -136,21 +155,22 @@ case $direction in old_shell="ssh -F $HOME/.ssh/confighome root@$old_host" # tests ssh connection. crafted this to not need to do escape chars f=/a/bin/bash_unpublished/source-state - if ! old_info=($($old_shell "hostname; sed -n s,.*MAIL_HOST=,,p $f; sed -n s,.*HOST2=,,p $f")); then + if ! old_info=$($old_shell "hostname; sed -n s,.*MAIL_HOST=,,p $f; sed -n s,.*HOST2=,,p $f"); then echo "$pre: error: failed ssh. retrying failed $old_shell with -v for more info:" $old_shell -v hostname exit 1 fi - old_hostname=${old_info[0]} - MAIL_HOST=${old_info[1]} - HOST2=${old_info[2]} + IFS=" " read -r old_hostname MAIL_HOST HOST2 <<<"$old_info" if ! $mail_only && ! $pull_reexec ; then + if ! $force; then + $old_shell switch-mail-host --check-installed + fi tmpf=$(mktemp) m scp -F $HOME/.ssh/confighome root@$old_host:/usr/local/bin/switch-mail-host $tmpf if ! diff -q $tmpf ${BASH_SOURCE[0]}; then e "found different version on old_host=$old_hostname, reexecing" - install -T $tmpf /usr/local/bin/switch-mail-host + m install -T $tmpf /usr/local/bin/switch-mail-host m /usr/local/bin/switch-mail-host --pull-reexec "${orig_args[@]}" mexit 0 fi @@ -284,7 +304,7 @@ EOF e Running initial btrbk -m btrbk-run -v $bbk_args $incremental_arg $mp_args $snapshot_arg || ret=$? +m btrbk-run -v $bbk_args $force_arg $incremental_arg $mp_args $snapshot_arg || ret=$? if (( ret )); then err "failed initial btrbk" exit $ret @@ -314,7 +334,7 @@ fi m $new_shell killall -q emacs ||: e Running main btrbk -m btrbk-run -v --fast $bbk_args $incremental_arg -m /o || ret=$? +m btrbk-run -v --fast $bbk_args $force_arg $incremental_arg -m /o || ret=$? if (( ret )); then bang="$(printf "$(tput setaf 5)█$(tput sgr0)%.0s" 1 2 3 4 5 6 7)" e $bang failed btrbk of /o. restoring old host as primary @@ -323,7 +343,7 @@ if (( ret )); then fi # new system is usable at this point -printf "$(tput setaf 5 2>/dev/null ||:)█$(tput sgr0 2>/dev/null||:)%.0s" $(eval echo "{1..${COLUMNS:-60}}") +printf "$(tput setaf 5 2>/dev/null ||:)█$(tput sgr0 2>/dev/null||:)%.0s $(eval echo "{1..${COLUMNS:-60}}")" echo # once I accidentally accepted incoming mail on old host. I used this script to copy over that mail: -- 2.30.2