Update the mirror-wrapper script.

This commit is contained in:
Darren 'Tadgy' Austin 2026-05-21 19:19:41 +01:00
commit 554548db72

View file

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
# Version: 0.1.0 # Version: 0.1.1
# Copyright (c) 2023: # Copyright (c) 2023-2026:
# Darren 'Tadgy' Austin <darren (at) afterdark.org.uk> # Darren 'Tadgy' Austin <darren (at) afterdark.org.uk>
# Licensed under the terms of the GNU General Public License version 3. # Licensed under the terms of the GNU General Public License version 3.
# #
@ -10,7 +10,25 @@
COMMAND="/opt/bin/mirror" COMMAND="/opt/bin/mirror"
LOGSDIR="/var/log/duplication/mirroring/$(printf "%(%Y/%m)T")" LOGSDIR="/var/log/duplication/mirroring/$(printf "%(%Y/%m)T")"
LOGFILE="$(printf "%(%Y%m%d-%H%M%S)T")-$$" LOGFILE="$(printf "%(%Y%m%d-%H%M%S)T")-$$"
MIN_LOGFILE_SIZE="550" MIN_LOGFILE_SIZE="550" # Used to prevent unnecessary emails - only messages over this size are sent.
# Where from/to to send emails. Comment for no emailing.
EMAIL_FROM="\"Server: ${HOSTNAME%%.*}\" <noreply@slackware.uk.net>"
EMAIL_TO=("Systems' Administrator <sysadmin@slackware.uk>")
# Functions
notify() {
if [[ -n "$EMAIL_FROM" ]] && (( "${#EMAIL_TO[@]}" != 0 )); then
printf "%s: %s\\n%s: %s\\n%s:\\n%s\\n" "Exit code" "$ERR" "Logfile" "$LOGSDIR/$LOGFILE.xz" "Output" "$(<"$LOGSDIR/$LOGFILE")" | mail -r "$EMAIL_FROM" -s "Mirroring $1" "${EMAIL_TO[@]}" >/dev/null 2>&1 || {
printf "%s: %s\\n" "${0##*/}" "mail command failed" >&2
return 1
}
else
printf "%s: %s\\n" "${0##*/}" "no sender and/or recipient configured for mail delivery" >&2
return 1
fi
return 0
}
# Only allow one copy of the script to run at any time. # Only allow one copy of the script to run at any time.
# shellcheck disable=SC2154 # shellcheck disable=SC2154
@ -27,21 +45,19 @@ if [[ "$FLOCK" != "$0" ]]; then
fi fi
fi fi
# Source the mail configuration. # Logs are only for root.
source /etc/mail.conf "backups" 2>/dev/null || { umask 027
printf "%s: %s\\n" "${0##*/}" "Failed to source /etc/mail.conf" >&2
exit 1
}
# Make sure the logs directory exists. # Make sure the logs directory exists.
mkdir -p "$LOGSDIR" 2>/dev/null || { # shellcheck disable=SC2174
printf "%s: %s\\n" "${0##*/}" "Failed to create logs directory" >&2 mkdir -p -m 750 "$LOGSDIR" 2>/dev/null || {
printf "%s: %s\\n" "${0##*/}" "Failed to create logs directory '$LOGSDIR'" >&2
exit 1 exit 1
} }
# Make sure the command to do the work is runnable. # Make sure the command to do the work is runnable.
[[ ! -x "$COMMAND" ]] && { [[ ! -x "$COMMAND" ]] && {
printf "%s: '%s' %s\\n" "${0##*/}" "$COMMAND" "is not executable" >&2 printf "%s: %s\\n" "${0##*/}" "'$COMMAND' is not executable" >&2
exit 1 exit 1
} }
@ -49,42 +65,17 @@ mkdir -p "$LOGSDIR" 2>/dev/null || {
"$COMMAND" "$@" >"$LOGSDIR/$LOGFILE" 2>&1 "$COMMAND" "$@" >"$LOGSDIR/$LOGFILE" 2>&1
ERR="$?" ERR="$?"
# Compress the log to save some space.
xz -9 "$LOGSDIR/$LOGFILE" 2>/dev/null || printf "%s: %s\\n" "${0##*/}" "failed to compress '$LOGSDIR/$LOGFILE'" >&2
# Tell the sysadmin what went on. # Tell the sysadmin what went on.
if (( "$ERR" == 0 )); then if (( "$ERR" == 0 )); then
# Only email a mirroring report if it's not a Slackware tree only hourly run. # Only email a mirroring report if it's not the hourly Slackware tree only run.
# The size of the log file determines if it gets emailed. # The size of the log file determines if it gets emailed.
(( $(stat --printf="%s" "$LOGSDIR/$LOGFILE") > MIN_LOGFILE_SIZE )) && { (( $(stat --printf="%s" "$LOGSDIR/$LOGFILE") > MIN_LOGFILE_SIZE )) && notify "report"
if [[ -n "${EMAIL_TO[*]}" ]]; then exit "$?"
mailx "${MAILX_ARGS[@]}" -S "from=$EMAIL_FROM" -s "Mirroring report" "${EMAIL_TO[@]}" <<-EOF 2>/dev/null || \
{ printf "%s: %s\\n" "${0##*/}" "mailx command failed" >&2; RET=1; }
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 else
# Mirroring failed, tell the admin. # Mirroring failed, tell the admin.
[[ -x /opt/bin/pushover-client ]] && /opt/bin/pushover-client "mirroring" -p -1 -m "Mirroring failure" notify "failure" || [[ -x /opt/bin/pushover-client ]] && /opt/bin/pushover-client "mirroring" -p -1 -s "Mirroring failure" -m "Check log in email"
if [[ -n "${EMAIL_TO[*]}" ]]; then exit 1
mailx "${MAILX_ARGS[@]}" -S "from=$EMAIL_FROM" -s "Mirroring failure" "${EMAIL_TO[@]}" <<-EOF 2>/dev/null || \
{ printf "%s: %s\\n" "${0##*/}" "mailx command failed" >&2; RET=1; }
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
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}"