# 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() {
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
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 "$@"
if [[ ! $tmp ]]; then
s ssh-add /root/.ssh/home
fi
- install-my-scripts
s jdo switch-mail-host "$@"
return $ret
}
}
+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
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
-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:
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"
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
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
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
fi
}
+pre="check-subvol-stale:${SSH_CLIENT:+ $HOSTNAME:}"
+
d() {
if $verbose; then
- printf "%s\n" "$*"
+ printf "$pre %s\n" "$*"
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; }
exit $1
}
-
+pre="mount-latest-subvol:${SSH_CLIENT:+ $HOSTNAME:}"
tu() {
while read -r line; do
}
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" "$*"
"$@"
}
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
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
Note: Uses GNU getopt options parsing style
EOF
- exit $1
+ exit 0
}
script_name="${BASH_SOURCE[0]}"
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"
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;;
done
-(( $# == 2 )) || usage 1
+if (( $# != 2 )) && ! $check_installed; then
+ err expected 2 args, got $#
+fi
if [[ ! $HOSTNAME ]]; then
err "\$HOSTNAME is unset"
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
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
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
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
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
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: