From 4bf0b1d600c1e8d5be4fb6f09d8ad38f3c3ed279 Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Sun, 29 Oct 2023 19:47:52 +0000 Subject: [PATCH] Add do-sbosrcarch script. --- do-sbosrcarch | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100755 do-sbosrcarch diff --git a/do-sbosrcarch b/do-sbosrcarch new file mode 100755 index 0000000..1cbaf88 --- /dev/null +++ b/do-sbosrcarch @@ -0,0 +1,87 @@ +#!/bin/bash +# Version: 0.1.0 +# Copyright (c) 2023: +# Darren 'Tadgy' Austin +# Licensed under the terms of the GNU General Public License version 3. +# +# Wrapper around /opt/bin/sbosrcarch to keep a log of the session, and email it. + +# Configuration. +COMMAND="/opt/bin/sbosrcarch" +LOGSDIR="/var/log/mirroring/$(printf "%(%Y/%m)T")" +LOGFILE="$(printf "%(%Y%m%d-%H%M%S)T")-sbosrcharch-$$" + +# Only allow one copy of the script to run at any time. +# shellcheck disable=SC2154 +if [[ "$FLOCK" != "$0" ]]; then + # shellcheck disable=SC2093 + exec env FLOCK="$0" flock -E 10 -e -n "$0" "$0" "$@" + ERR="$?" + if (( ERR == 10 )); then + # File is locked, exit now. + exit 0 + elif (( ERR > 0 )); then + printf "%s: %s\\n" "${0##*/}" "flock execution error" >&2 + exit 1 + fi +fi + +# Source the mail configuration. +source /etc/mail.conf "sbosrcarch" 2>/dev/null || { + printf "%s: %s\\n" "${0##*/}" "Failed to source /etc/mail.conf" >&2 + exit 1 +} + +# Make sure the logs directory exists. +mkdir -p "$LOGSDIR" 2>/dev/null || { + printf "%s: %s\\n" "${0##*/}" "Failed to create logs directory" >&2 + exit 1 +} + +# Make sure the command to do the work is runnable. +[[ ! -x "$COMMAND" ]] && { + printf "%s: '%s' %s\\n" "${0##*/}" "$COMMAND" "is not executable" >&2 + exit 1 +} + +# Do the sbosrcarch work. +"$COMMAND" "${@:-update}" >"$LOGSDIR/$LOGFILE" 2>&1 +ERR="$?" +printf "\\n" >>"$LOGSDIR/$LOGFILE" +"$COMMAND" "status" >>"$LOGSDIR/$LOGFILE" 2>&1 + +# Tell the sysadmin what went on. +if (( "$ERR" == 0 )); then + if [[ -n "${EMAIL_TO[*]}" ]]; then + mailx "${MAILX_ARGS[@]}" -S "from='$EMAIL_FROM'" -s "SBosrcarch report" "${EMAIL_TO[@]}" <<-EOF 2>/dev/null || \ + printf "%s: %s\\n" "${0##*/}" "mailx command failed" >&2 + Exit code: $ERR + Logfile: $LOGSDIR/$LOGFILE.gz + Output: + $(< "$LOGSDIR/$LOGFILE") + EOF + else + printf "%s: %s\\n" "${0##*/}" "no recipient configured for mail delivery" >&2 + RET=1 + fi +else + # Updating failed, tell the admin. + [[ -x "/opt/bin/pushover" ]] && CONFIG_FILE="mirroring" /opt/bin/pushover -T "SBosrcarch" -p '-1' -m "SBosrcarch failed" + if [[ -n "${EMAIL_TO[*]}" ]]; then + mailx "${MAILX_ARGS[@]}" -S "from='$EMAIL_FROM'" -s "SBosrcarch failure" "${EMAIL_TO[@]}" <<-EOF 2>/dev/null || \ + printf "%s: %s\\n" "${0##*/}" "mailx command failed" >&2 + Exit code: $ERR + Logfile: $LOGSDIR/$LOGFILE.gz + Output: + $(< "$LOGSDIR/$LOGFILE") + EOF + else + printf "%s: %s\\n" "${0##*/}" "no recipient configured for mail delivery" >&2 + RET=1 + fi +fi + +# Compress the log to save some space. +gzip -9 "$LOGSDIR/$LOGFILE" 2>/dev/null || { printf "%s: %s '%s'\\n" "${0##*/}" "failed to compress" "$LOGSDIR/$LOGFILE" >&2; RET=1; } + +exit "${RET:-0}"