From 76b1fa0674492210ba9e56e8d0dfa0af26ccd536 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Wed, 13 Oct 2021 15:43:30 -0400 Subject: [PATCH] improve error handling (With some rebased changes by Andrew. Trying to keep password variable from printing to stdout, and therefore potentially email). --- kaya | 28 ++++++++++++++++++---------- kaya-client | 6 ++++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/kaya b/kaya index 0c4454e..dc44983 100755 --- a/kaya +++ b/kaya @@ -18,6 +18,11 @@ # Copyright 2021 Andrew Engelbrecht # +if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi +shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4 +set -eE -o pipefail +trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR + # defaults @@ -28,6 +33,7 @@ kaya usage: kaya [-c /etc/kaya.conf] [-u root] www1.example.com backup -- EOF + exit $1 } function set_defaults() { @@ -43,13 +49,11 @@ function get_params() { case $key in -c|--conf) global_conf="$2" - shift - shift + shift 2 ;; -u|--user) remote_user="$2" - shift - shift + shift 2 ;; --) shift @@ -57,8 +61,8 @@ function get_params() { break ;; -*) - kaya_usage - exit 1 + echo "kaya: error: unrecognized option $key" >&2 + kaya_usage 1 ;; *) positional+=("$key") @@ -72,15 +76,18 @@ function get_params() { hostname="$1" action="$2" - [[ -z ${hostname} ]] && kaya_usage && exit 1 + if [[ -z ${hostname} ]]; then + echo "kaya: error: missing hostname argument" >&2 + kaya_usage 1 + fi case $action in backup) # currently the only option is to backup ;; *) - kaya_usage - exit 1 + echo "kaya: error: missing action argument" >&2 + kaya_usage 1 ;; esac } @@ -95,7 +102,8 @@ function create_backup_dir() { RESTIC_PASSWORD="$(cat "${password_file}")" restic -r "${backup_dir}" init > /dev/null touch "${htpasswd_file}"; chmod 600 "${htpasswd_file}" - flock -w 10 "${htpasswd_file}.flock" -c "htpasswd -i -B '${htpasswd_file}' '${hostname}'" < "${password_file}" 2>&1 | grep -E -v "(Adding|Updating) password for user" 1>&2 + flock -w 10 "${htpasswd_file}.flock" -c "htpasswd -i -B '${htpasswd_file}' '${hostname}'" < "${password_file}" \ + |& { grep -E -v "(Adding|Updating) password for user" >&2 ||:; } echo "kaya: Waiting 15s for rest-server file reload (first snapshot only)..." sleep 15 diff --git a/kaya-client b/kaya-client index 86f8af4..4bad064 100755 --- a/kaya-client +++ b/kaya-client @@ -18,6 +18,12 @@ # Copyright 2021 Andrew Engelbrecht # +if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi +shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4 +set -eE -o pipefail +trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" exit status: $?, PIPESTATUS: ${PIPESTATUS[*]}" >&2' ERR + + # settings come in on stdin read -r port read -r hostname -- 2.25.1