diff --git a/mirror-wrapper b/mirror-wrapper index f8ed6e0..20dcc4e 100755 --- a/mirror-wrapper +++ b/mirror-wrapper @@ -8,15 +8,16 @@ 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="845" # Used to prevent unnecessary emails - only messages over this size are sent. +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%%.*}\" " EMAIL_TO=("Systems' Administrator ") -# Functions +# 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" "$(cat "$LOGSDIR/$LOGFILE" | sed -re 's/^/ /g')" | mail -r "$EMAIL_FROM" -s "Mirroring $1" "${EMAIL_TO[@]}" >/dev/null 2>&1 || { + # 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 } @@ -31,7 +32,7 @@ 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' $0 $@" >&2 + printf "%s: %s: %s\\n" "${0##*/}" "to run manually:" "su '$MIRRORING_USER' -c \"$0 $*\"" >&2 exit 1 } @@ -63,45 +64,64 @@ if [[ "$FLOCK" != "$0" ]]; then fi fi -# Drop the caches as sometimes old user/groups are cached. -sudo /opt/sbin/drop-caches +ERR=0 # Make sure there's no errant ownerships. -printf "%s: %s\\n" "INFO" "Scanning depository directory for correct ownerships/permissions" | tee "$LOGSDIR/$LOGFILE" -GLOBIGNORE="$DEPOSITORY/sbosrcarch" -WRONG="$(find "$DEPOSITORY/"* \( \ - \( \( -type b -o -type c -o -type p -o -type s \) -a \! -path "$DEPOSITORY/slackware/slackware-3.0/disk1/live/dev/*" \) -o \ - \( -type l -a \! -perm 777 \) -o \ - \( -type d -a \! -perm 755 -a \! -perm 2755 \) -o \ - \( -type f -a \! -perm 644 -a \! -perm 755 \) -o \ - \( \! -user "$MIRRORING_USER" \) -o \ - \( \! -group "$DEPOSITORY_GROUP" \) \ - \) -a -printf "%u:%g\t%M\t%p\n")" -if [[ -n "$WRONG" ]]; then - printf "%s: %s\\n" "ERROR" "This sync has been aborted!" | tee -a "$LOGSDIR/$LOGFILE" - printf "%s: %s\\n" "WARNING" "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" - # Do the mirroring work. +[[ $(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" + 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="$?" -fi +} # Tell the sysadmin what went on. -if (( "$ERR" == 0 )); then +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="$?" + (( $(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 - # 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" + + # 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. diff --git a/sbosrcarch-wrapper b/sbosrcarch-wrapper index ea52ffa..8f58c7f 100755 --- a/sbosrcarch-wrapper +++ b/sbosrcarch-wrapper @@ -16,7 +16,8 @@ EMAIL_CC=("Urchlay ") # 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" "$(cat "$LOGSDIR/$LOGFILE" | sed -re 's/^/ /g')" | mail -r "$EMAIL_FROM" -s "SboSrcArch $1" "$(for i in $(seq -s ' ' 0 $(( ${#EMAIL_CC[@]} -1 ))); do echo ${EMAIL_CC[$i]:+-c "${EMAIL_CC[$i]}"}; done)" "${EMAIL_TO[@]}" >/dev/null 2>&1 || { + # 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 "SboSrcArch $1" "$(for i in $(seq -s ' ' 0 $(( ${#EMAIL_CC[@]} -1 ))); do echo ${EMAIL_CC[$i]:+-c "${EMAIL_CC[$i]}"}; done)" "${EMAIL_TO[@]}" >>"$LOGSDIR/$LOGFILE" 2>&1 || { printf "%s: %s\\n" "${0##*/}" "mail command failed" >&2 return 1 } @@ -31,7 +32,7 @@ umask 027 # Only run for the configured sbosrcarch user. [[ "$(whoami)" != "$SBOSRCARCH_USER" ]] && { printf "%s: %s\\n" "${0##*/}" "must be run by the '$SBOSRCARCH_USER' user" >&2 - printf "%s: %s: %s\\n" "${0##*/}" "to run manually:" "su '$SBOSRCARCH_USER' $0 $@" >&2 + printf "%s: %s: %s\\n" "${0##*/}" "to run manually:" "su \"$SBOSRCARCH_USER\" -c \"$0 $*\"" >&2 exit 1 } @@ -63,45 +64,66 @@ if [[ "$FLOCK" != "$0" ]]; then fi fi -# Drop the caches as sometimes old user/groups are cached. -sudo /opt/sbin/drop-caches +ERR=0 # Make sure there's no errant ownerships. -printf "%s: %s\\n" "INFO" "Scanning sbosrcarch directory for correct ownerships/permissions" | tee "$LOGSDIR/$LOGFILE" -WRONG="$(find "$DEPOSITORY/" \( \ - \( -type b -o -type c -o -type p -o -type s \) -o \ - \( -type l -a \! -perm 777 \) -o \ - \( -type d -a \! -perm 755 -a \! -perm 2755 \) -o \ - \( -type f -a \! -perm 644 -a \! -perm 755 \) -o \ - \( \! -user "$SBOSRCARCH_USER" \) -o \ - \( \! -group "$DEPOSITORY_GROUP" \) \ - \) -a -printf "%u:%g\t%M\t%p\n")" -if [[ -n "$WRONG" ]]; then - printf "%s: %s\\n" "ERROR" "This run has been aborted!" | tee -a "$LOGSDIR/$LOGFILE" - printf "%s: %s\\n" "WARNING" "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" - # Do the sbosrcarch work. +[[ $(printf "%(%H)T") == "04" ]] && { + printf "%s: %s\\n" "INFO" "Scanning sbosrcarch 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. + WRONG="$(find "$DEPOSITORY/" \( \ + \( -type b -o -type c -o -type p -o -type s \) -o \ + \( -type l -a \! -perm 777 \) -o \ + \( -type d -a \! -perm 755 -a \! -perm 2755 \) -o \ + \( -type f -a \! -perm 644 -a \! -perm 755 \) -o \ + \( \! -user "$SBOSRCARCH_USER" \) -o \ + \( \! -group "$DEPOSITORY_GROUP" \) \ + \) -a -printf "%u:%g\t%M\t%p\n")" + + # Report findings. + if [[ -n "$WRONG" ]]; then + printf "%s: %s\\n" "ERROR" "This run 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 sbosrcarch work. +(( ERR == 0 )) && { umask 022 - printf "%s: %s: %s\\n" "INFO" "Beginning sbosrcarch run" "sg \"$DEPOSITORY_GROUP\" -c \"$COMMAND ${1:-update}${2:+ $2}${3:+ $3}\"" | tee -a "$LOGSDIR/$LOGFILE" - TMP_OUTPUT="$(sg "$DEPOSITORY_GROUP" -c "$COMMAND ${1:-update}${2:+ $2}${3:+ $3} 2>&1")" + + printf "%s: %s: %s\\n" "INFO" "Beginning sbosrcarch run" "sg \"$DEPOSITORY_GROUP\" -c \"$COMMAND ${*:-update}\"" | tee -a "$LOGSDIR/$LOGFILE" + TMP_OUTPUT="$(sg "$DEPOSITORY_GROUP" -c "$COMMAND ${*:-update} 2>&1")" ERR="$?" - sg "$DEPOSITORY_GROUP" -c "$COMMAND status 2>&1" | tee -a "$LOGSDIR/$LOGFILE" + + [[ "$*" != *status* ]] && { + sg "$DEPOSITORY_GROUP" -c "$COMMAND status 2>&1" | tee -a "$LOGSDIR/$LOGFILE" + (( ERR += PIPESTATUS[0] )) + } + printf "\\n%s\\n" "$TMP_OUTPUT" | tee -a "$LOGSDIR/$LOGFILE" - (( ERR += $? )) -fi +} # Tell the sysadmin what went on. if (( "$ERR" == 0 )); then # Send a report. - notify "report" - ERR="$?" + notify "report" || { + [[ -x /opt/bin/pushover-client ]] && /opt/bin/pushover-client "mirroring" -p -1 -s "notify() failure" -m "Check logfile: $LOGSDIR/$LOGFILE.xz" + } else - # sbosrcarch failed, tell the admin. - notify "failure" || [[ -x /opt/bin/pushover-client ]] && /opt/bin/pushover-client "mirroring" -p -1 -s "SBoSrcArch failure" -m "Check log in email" ERR="1" + # sbosrcarch failed, tell the admin. + if notify "failure"; then + [[ -x /opt/bin/pushover-client ]] && /opt/bin/pushover-client "mirroring" -p -1 -s "SBoSrcArch failure" -m "Check email / logfile: $LOGSDIR/$LOGFILE.xz" + else + [[ -x /opt/bin/pushover-client ]] && /opt/bin/pushover-client "mirroring" -p -1 -s "notify() failure" -m "Check logfile: $LOGSDIR/$LOGFILE.xz" + fi fi # Compress the log to save some space.