130 lines
4.7 KiB
Bash
Executable file
130 lines
4.7 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"
|
|
DEPOSITORY="/data/depository"
|
|
MIRRORING_USER='SLACKWAREUKINT\mirroring'
|
|
DEPOSITORY_GROUP='SLACKWAREUKINT\depository'
|
|
LOGSDIR="/var/log/duplication/mirroring/$(printf "%(%Y/%m)T")"
|
|
LOGFILE="$(printf "%(%Y%m%d-%H%M%S)T")-$$"
|
|
MIN_LOGFILE_SIZE="1210" # 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 )) && {
|
|
# shellcheck disable=SC2094
|
|
printf "%s: %s\\n%s: %s\\n%s:\\n%s\\n" "Exit code" "$ERR" "Logfile" "$LOGSDIR/$LOGFILE.xz" "Output" "$(sed -re 's/^/ /g' <"$LOGSDIR/$LOGFILE")" | mail -r "$EMAIL_FROM" -s "Mirroring $1" "${EMAIL_TO[@]}" >>"$LOGSDIR/$LOGFILE" 2>&1 || {
|
|
printf "%s: %s\\n" "${0##*/}" "mail command failed" >&2
|
|
return 1
|
|
}
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
# Logs are only for root.
|
|
umask 027
|
|
|
|
# Only run for the configured mirroring user.
|
|
[[ "$(whoami)" != "$MIRRORING_USER" ]] && {
|
|
printf "%s: %s\\n" "${0##*/}" "must be run by the '$MIRRORING_USER' user" >&2
|
|
printf "%s: %s: %s\\n" "${0##*/}" "to run manually:" "su '$MIRRORING_USER' -c \"$0 $*\"" >&2
|
|
exit 1
|
|
}
|
|
|
|
# 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
|
|
}
|
|
|
|
# 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
|
|
|
|
ERR=0
|
|
|
|
# Make sure there's no errant ownerships.
|
|
[[ $(printf "%(%H)T") =~ (02|08|14|20) ]] && {
|
|
printf "%s: %s\\n" "INFO" "Scanning depository directory for correct ownerships/permissions" | tee "$LOGSDIR/$LOGFILE"
|
|
|
|
# Drop the caches as sometimes old user/groups are cached.
|
|
sudo /opt/sbin/drop-caches
|
|
|
|
# Scan files.
|
|
GLOBIGNORE="$DEPOSITORY/sbosrcarch"
|
|
WRONG="$(find "$DEPOSITORY/"* -not -name .rsync-tmp -a \( \
|
|
\( \( -type b -o -type c -o -type p -o -type s \) -a -not -path "$DEPOSITORY/slackware/slackware-3.0/disk1/live/dev/*" \) -o \
|
|
\( -type l -a -not -perm 777 \) -o \
|
|
\( -type d -a -not -perm 755 -a -not -perm 2755 \) -o \
|
|
\( -type f -a -not -perm 644 -a -not -perm 755 \) -o \
|
|
\( -not -user "$MIRRORING_USER" \) -o \
|
|
\( -not -group "$DEPOSITORY_GROUP" \) \
|
|
\) -a -printf "%u:%g\t%M\t%p\n")"
|
|
|
|
# Report findings.
|
|
if [[ -n "$WRONG" ]]; then
|
|
printf "%s: %s\\n" "ERROR" "This sync has been aborted!" | tee -a "$LOGSDIR/$LOGFILE"
|
|
printf "%s: %s\\n" "ERROR" "The following paths have erronious ownerships/permissions:" | tee -a "$LOGSDIR/$LOGFILE"
|
|
printf "%s\\n" "$WRONG" | sed -re 's/^/ /g' | tee -a "$LOGSDIR/$LOGFILE"
|
|
ERR=-1
|
|
else
|
|
printf "%s: %s\\n" "INFO" "No ownership/permission issues found" | tee -a "$LOGSDIR/$LOGFILE"
|
|
fi
|
|
}
|
|
|
|
# Do the mirroring work.
|
|
(( ERR == 0 )) && {
|
|
umask 022
|
|
export MIRRORING_USER
|
|
printf "%s: %s: %s\\n" "INFO" "Beginning sync command" "sg \"$DEPOSITORY_GROUP\" -c \"$COMMAND $*\"" | tee -a "$LOGSDIR/$LOGFILE"
|
|
sg "$DEPOSITORY_GROUP" -c "$COMMAND $* 2>&1" | tee -a "$LOGSDIR/$LOGFILE"
|
|
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" || {
|
|
[[ -x /opt/bin/pushover-client ]] && /opt/bin/pushover-client "mirroring" -p -1 -s "notify() failure" -m "Check logfile: $LOGSDIR/$LOGFILE"
|
|
}
|
|
}
|
|
else
|
|
ERR="1"
|
|
|
|
# Mirroring failed, tell the admin.
|
|
if notify "failure"; then
|
|
[[ -x /opt/bin/pushover-client ]] && /opt/bin/pushover-client "mirroring" -p -1 -s "Mirroring failure" -m "Check email / logfile: $LOGSDIR/$LOGFILE"
|
|
else
|
|
[[ -x /opt/bin/pushover-client ]] && /opt/bin/pushover-client "mirroring" -p -1 -s "notify() failure" -m "Check logfile: $LOGSDIR/$LOGFILE"
|
|
fi
|
|
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"
|