random-commands/mirror-wrapper
Darren 'Tadgy' Austin 041ebf1b1a Many small updates to various scripts.
deleted:    cronjob-update-packages-list.new
deleted:    cronjob-updatedb-mirrors
renamed:    rsync-notify-upload -> notify-rsync-upload
modified:   colour-changelog
modified:   cronjob-clean-php
modified:   cronjob-dehydrated
modified:   cronjob-fix-log-acls
modified:   cronjob-rotate-logs-today-symlink
modified:   cronjob-update-mirrors-search-db
modified:   cronjob-update-packages-list
modified:   cronjob-warn-git-status
modified:   cronjob-warn-smtp-queue
modified:   do-backup
modified:   dovecot-service-checksuspended
modified:   firewall-initscript
modified:   git-auto-merge
modified:   mirror
modified:   mirror-new-slackware-release.gpg
modified:   mirror-wrapper
modified:   notify-rsync-upload
modified:   sbosrcarch-wrapper
2026-05-22 00:27:36 +01:00

75 lines
2.4 KiB
Bash
Executable file

#!/bin/bash
# Wrapper around /opt/bin/mirror to keep a log of the mirroring session, and email it.
# Configuration.
COMMAND="/opt/bin/mirror"
LOGSDIR="/var/log/duplication/mirroring/$(printf "%(%Y/%m)T")"
LOGFILE="$(printf "%(%Y%m%d-%H%M%S)T")-$$"
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() {
[[ -n "$EMAIL_FROM" ]] && (( "${#EMAIL_TO[@]}" != 0 )) && {
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
}
}
return 0
}
# 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
# Logs are only for root.
umask 027
# Make sure the logs directory exists.
# shellcheck disable=SC2174
mkdir -p -m 750 "$LOGSDIR" 2>/dev/null || {
printf "%s: %s\\n" "${0##*/}" "Failed to create logs directory '$LOGSDIR'" >&2
exit 1
}
# Make sure the command to do the work is runnable.
[[ ! -x "$COMMAND" ]] && {
printf "%s: %s\\n" "${0##*/}" "'$COMMAND' is not executable" >&2
exit 1
}
# Do the mirroring work.
"$COMMAND" "$@" >"$LOGSDIR/$LOGFILE" 2>&1
ERR="$?"
# Tell the sysadmin what went on.
if (( "$ERR" == 0 )); then
# 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.
(( $(stat --printf="%s" "$LOGSDIR/$LOGFILE") > MIN_LOGFILE_SIZE )) && notify "report"
ERR="$?"
else
# Mirroring failed, tell the admin.
notify "failure" || [[ -x /opt/bin/pushover-client ]] && /opt/bin/pushover-client "mirroring" -p -1 -s "Mirroring failure" -m "Check log in email"
ERR="1"
fi
# 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
exit "$ERR"