Notify about backup with mail
This commit is contained in:
@@ -58,6 +58,10 @@ REQUIRED_CMDS=(borg rclone docker timeout flock date find)
|
||||
|
||||
# =================================================================
|
||||
|
||||
# Email notification (set to your address to receive backup summary).
|
||||
# Leave empty to skip notifications. The script auto-detects mail/sendmail/msmtp.
|
||||
MAIL_TO="${MAIL_TO:-}"
|
||||
|
||||
SELF="$(readlink -f "$0")"
|
||||
mkdir -p "$LOGDIR"
|
||||
LOGFILE="${BORG_BACKUP_LOGFILE:-$LOGDIR/backup-$(date +%Y-%m-%d-%H%M%S).log}"
|
||||
@@ -89,6 +93,68 @@ run_cmd() {
|
||||
"$@"
|
||||
}
|
||||
|
||||
# Detect available mail transport. Returns the command name or empty string.
|
||||
find_mail_cmd() {
|
||||
for cmd in mail sendmail msmtp; do
|
||||
command -v "$cmd" >/dev/null 2>&1 && { echo "$cmd"; return 0; }
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
# Send an email notification about the backup result.
|
||||
# Called from cleanup() so the log file is complete.
|
||||
# Non-fatal — mail failures do not affect the backup exit code.
|
||||
send_notification() {
|
||||
local exit_code=$1 status started ended
|
||||
[[ -n "$MAIL_TO" ]] || return 0
|
||||
|
||||
local mail_cmd
|
||||
mail_cmd="$(find_mail_cmd)" || { log "Notification skipped: no mail command found"; return 0; }
|
||||
|
||||
if [ "$exit_code" -eq 0 ]; then
|
||||
status="SUCCESS"
|
||||
else
|
||||
status="FAILED (exit $exit_code)"
|
||||
fi
|
||||
|
||||
started="$(head -1 "$LOGFILE" 2>/dev/null | sed -n 's/^\[\(.*\)\].*/\1/p' || echo "?")"
|
||||
ended="$(date '+%F %T')"
|
||||
|
||||
# Build message body with headers and log tail.
|
||||
{
|
||||
echo "Subject: Backup $status - $(hostname -s)"
|
||||
echo "To: $MAIL_TO"
|
||||
echo ""
|
||||
echo "===== Backup Summary ====="
|
||||
echo "Host: $(hostname -f)"
|
||||
echo "Archive: $ARCHIVE"
|
||||
echo "Status: $status"
|
||||
echo "Started: $started"
|
||||
echo "Ended: $ended"
|
||||
echo "Log: $LOGFILE"
|
||||
echo ""
|
||||
echo "----- Last 80 lines of backup log -----"
|
||||
tail -80 "$LOGFILE" 2>/dev/null || echo "(log unavailable)"
|
||||
} | case "$mail_cmd" in
|
||||
mail)
|
||||
mail -s "Backup $status - $(hostname -s)" "$MAIL_TO"
|
||||
;;
|
||||
sendmail)
|
||||
sendmail "$MAIL_TO"
|
||||
;;
|
||||
msmtp)
|
||||
msmtp "$MAIL_TO"
|
||||
;;
|
||||
esac
|
||||
|
||||
local rc=$?
|
||||
if [ "$rc" -eq 0 ]; then
|
||||
log "Notification sent to $MAIL_TO (via $mail_cmd)"
|
||||
else
|
||||
log "WARNING: notification via $mail_cmd failed (exit $rc)"
|
||||
fi
|
||||
}
|
||||
|
||||
container_running() {
|
||||
[[ "$(docker inspect -f '{{.State.Running}}' "$1" 2>/dev/null || echo false)" == "true" ]]
|
||||
}
|
||||
@@ -143,6 +209,7 @@ cleanup() {
|
||||
log "=== Backup FAILED (exit $exit_code) ==="
|
||||
fi
|
||||
log "Full log: $LOGFILE"
|
||||
send_notification "$exit_code"
|
||||
}
|
||||
|
||||
# Returns 0 if the repo is encrypted, 1 if not.
|
||||
|
||||
Reference in New Issue
Block a user