backup.sh - log with timestamps
This commit is contained in:
@@ -12,13 +12,31 @@ LOGFILE="/tmp/backup_run_$(date +%s).log"
|
|||||||
SUCCESS_PREFIX="=?utf-8?Q? =E2=9C=85 SUCCESS?="
|
SUCCESS_PREFIX="=?utf-8?Q? =E2=9C=85 SUCCESS?="
|
||||||
FAILED_PREFIX="=?utf-8?Q? =E2=9D=8C FAILED?="
|
FAILED_PREFIX="=?utf-8?Q? =E2=9D=8C FAILED?="
|
||||||
|
|
||||||
|
# Helper function to prepend a timestamp and the first parameter to every line of STDIN
|
||||||
|
_indent() {
|
||||||
|
while read -rs line; do
|
||||||
|
printf "%s%s\n" "$(date +%s)" "$1" "$line"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
# This function prepends timestamps to stderr and stdout of the
|
||||||
|
# command supplied as parameters to this.
|
||||||
|
log() {
|
||||||
|
# Adapted from https://stackoverflow.com/a/31151808
|
||||||
|
{
|
||||||
|
"$@" 2>&1 1>&3 3>&- | _indent " ! "
|
||||||
|
} 3>&1 1>&2 | _indent " | " >>"$LOGFILE"
|
||||||
|
}
|
||||||
|
|
||||||
report() {
|
report() {
|
||||||
if [ -n "$SUCCESS" ]; then
|
if [ -n "$SUCCESS" ]; then
|
||||||
|
log echo SUCCESS
|
||||||
PREFIX="$SUCCESS_PREFIX"
|
PREFIX="$SUCCESS_PREFIX"
|
||||||
else
|
else
|
||||||
|
log echo FAILED
|
||||||
PREFIX="$FAILED_PREFIX"
|
PREFIX="$FAILED_PREFIX"
|
||||||
fi
|
fi
|
||||||
echo -e "Subject: $PREFIX: $MAIL_SUBJECT\n\n$(cat $LOGFILE)\n" |
|
printf "Subject: $PREFIX: $MAIL_SUBJECT\n\n$(cat $LOGFILE)\n" |
|
||||||
sendmail -t "$MAIL_TO"
|
sendmail -t "$MAIL_TO"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,7 +46,7 @@ testmail() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
failed() {
|
failed() {
|
||||||
echo "Exited abnormally!"
|
log echo "Exited abnormally!"
|
||||||
report
|
report
|
||||||
rm -f "$LOGFILE"
|
rm -f "$LOGFILE"
|
||||||
}
|
}
|
||||||
@@ -50,7 +68,9 @@ if [ -n "$TEST_SMTP" ]; then
|
|||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if $BACKUP_COMMAND 2>&1 | tee "$LOGFILE"; then
|
|
||||||
|
echo "STARTED" >> "$LOGFILE"
|
||||||
|
if log $BACKUP_COMMAND; then
|
||||||
SUCCESS=1
|
SUCCESS=1
|
||||||
report
|
report
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user