templates/docker-compose/borgmatic/backup.sh - refinements
Trap failures, ensure cleanup. Send an email even if we fail. Break borg locks in cleanup. Implement testing smtp emails.
This commit is contained in:
@@ -1,20 +1,56 @@
|
||||
#!/bin/sh
|
||||
# Run the backup and mail the logs
|
||||
|
||||
BACKUP_COMMAND="borgmatic --stats"
|
||||
LOGFILE="/tmp/backup_run_$(date +%s).log"
|
||||
# Set TEST_SMTP=1 to run in SMTP testing mode, which will just send an email
|
||||
|
||||
set -o pipefail
|
||||
if $BACKUP_COMMAND 2>&1 | tee $LOGFILE; then
|
||||
SUBJECT_PREFIX="=?utf-8?Q? =E2=9C=85 SUCCESS?="
|
||||
|
||||
# Set up environment
|
||||
/bin/sh /scripts/msmtprc.sh
|
||||
/bin/sh /scripts/env.sh
|
||||
BACKUP_COMMAND="borgmatic --stats -v 2"
|
||||
LOGFILE="/tmp/backup_run_$(date +%s).log"
|
||||
SUCCESS_PREFIX="=?utf-8?Q? =E2=9C=85 SUCCESS?="
|
||||
FAILED_PREFIX="=?utf-8?Q? =E2=9D=8C FAILED?="
|
||||
|
||||
report() {
|
||||
if [ -n "$SUCCESS" ]; then
|
||||
PREFIX="$SUCCESS_PREFIX"
|
||||
else
|
||||
PREFIX="$FAILED_PREFIX"
|
||||
fi
|
||||
echo -e "Subject: $PREFIX: $MAIL_SUBJECT\n\n$(cat $LOGFILE)\n" |
|
||||
sendmail -t "$MAIL_TO"
|
||||
}
|
||||
|
||||
testmail() {
|
||||
echo -e "Subject: TESTING!\n\ntest mail, please ignore\n" |
|
||||
sendmail -t "$MAIL_TO"
|
||||
}
|
||||
|
||||
failed() {
|
||||
echo "Exited abnormally!"
|
||||
report
|
||||
rm -f "$LOGFILE"
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
borgmatic break-lock
|
||||
echo "Done, removing $LOGFILE"
|
||||
rm -f "$LOGFILE"
|
||||
}
|
||||
|
||||
# Handle various kinds of exit
|
||||
trap failed INT QUIT KILL
|
||||
trap cleanup EXIT
|
||||
|
||||
if [ -n "$TEST_SMTP" ]; then
|
||||
echo "Testing mail to $MAIL_TO"
|
||||
testmail
|
||||
echo "Done."
|
||||
exit
|
||||
fi
|
||||
|
||||
if $BACKUP_COMMAND 2>&1 | tee "$LOGFILE"; then
|
||||
SUCCESS=1
|
||||
else
|
||||
SUBJECT_PREFIX="=?utf-8?Q? =E2=9D=8C FAILED?="
|
||||
report
|
||||
fi
|
||||
|
||||
if [ -z "$SUCCESS" ]; then
|
||||
echo -e "Subject: $SUBJECT_PREFIX: $MAIL_SUBJECT\n\n$(cat $LOGFILE)\n" |
|
||||
sendmail -t $MAIL_TO
|
||||
fi
|
||||
|
||||
rm $LOGFILE
|
||||
|
||||
Reference in New Issue
Block a user