SN

MongoDB Backup Failure Alerts: Email Setup with SES

Add MongoDB backup failure alerts to an Ubuntu cron script with Amazon SES. Check dump and S3 upload errors, test email delivery, and review monitoring gaps.

6 min readUpdated October 26, 2026

The cron job ran. The log said "Starting backup". A rotated IAM key had broken S3 uploads, and I did not notice for three days. That is why I added MongoDB backup failure alerts to the Ubuntu backup script.

This guide uses Amazon Simple Email Service (SES) to attempt an email when mongodump or the S3 upload returns an error. It covers sender verification, IAM permissions, a delivery test, and the script changes.

Key takeaways

  • Verify a sender in the AWS Region used by the script.
  • Verify the recipient too while SES is in the sandbox.
  • Check dump and upload exit codes, then attempt an email on failure.
  • Test the failure path. No email can mean success, a missed job, or a broken alert.

MongoDB backup failure alerts: the workflow

MongoDB
   ↓
mongodump
   ↓
Upload to S3
   ↓
If failure occurs → Send email alert via Amazon SES

1. Verify an SES sender

Use the same AWS Region throughout the setup:

  1. Log in to the AWS Console
  2. Open Amazon Simple Email Service (SES)
  3. Go to Verified identities
  4. Click Create identity
  5. Choose Email Address
  6. Enter the sender email (e.g. alerts@yourdomain.com)
  7. Click Create
  8. Verify the email from your inbox

SES sandbox: Verify the recipient as well as the sender while your account is in the sandbox. Sandbox status is regional. AWS explains the restrictions and production-access process. Production access removes the verified-recipient restriction, not the requirement to verify the sender.

2. Allow the backup identity to send email

Add SES permissions to the IAM identity used by the backup process. On EC2, that may be a role rather than a user. This example policy allows sending against any resource:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["ses:SendEmail", "ses:SendRawEmail"],
      "Resource": "*"
    }
  ]
}

Before using this beyond a test, scope permissions to the verified sending identity where your configuration allows it. The backup process also needs its existing S3 permissions.

3. Test email delivery from Ubuntu

Replace the sender and recipient placeholders. Run this as the operating-system user that owns the backup cron job:

aws ses send-email \
  --from alerts@yourdomain.com \
  --destination ToAddresses=your@email.com \
  --message "Subject={Data=SES Test Email},Body={Text={Data=Amazon SES is working successfully}}" \
  --region ap-south-1

Confirm that the command succeeds and the message reaches the inbox. If it fails, check:

  • Sender verification and sandbox recipient verification
  • The IAM identity and send permissions
  • The AWS Region
  • Spam filtering if SES accepts the message but it does not reach the inbox

4. Add failure handling to the backup script

Open the script from the backup guide:

nano ~/mongo-backup.sh

Replace its contents with the following example, keeping your own database URI, bucket, sender, recipient, and region. The commands are supplied as a reference; test them in your environment before scheduling.

#!/bin/bash
 
# Same `snap run aws-cli.aws` approach as the previous post — see there for the cron/PATH rationale.
 
MONGO_URI="mongodb://localhost:27017/mydatabase"
BACKUP_DIR="$HOME/bkp"
DATE=$(date +"%Y-%m-%d_%H-%M-%S")
S3_BUCKET="s3://your-bucket-name"
BACKUP_NAME="backup-$DATE.gz"
RETENTION_DAYS=7
 
SES_FROM="alerts@yourdomain.com"
SES_TO="your@email.com"
AWS_REGION="ap-south-1"
 
LOG_FILE="$HOME/mongo-backup.log"
 
send_failure_email() {
  snap run aws-cli.aws ses send-email \
    --from "$SES_FROM" \
    --destination ToAddresses="$SES_TO" \
    --message "Subject={Data=Mongo Backup FAILED},Body={Text={Data=Backup failed on $(hostname) at $DATE. Check logs at $LOG_FILE}}" \
    --region "$AWS_REGION"
}
 
mkdir -p "$BACKUP_DIR"
 
echo "Starting backup: $DATE" >> "$LOG_FILE"
 
# Run mongodump
mongodump --uri="$MONGO_URI" \
  --archive="$BACKUP_DIR/$BACKUP_NAME" --gzip >> "$LOG_FILE" 2>&1
 
if [ $? -ne 0 ]; then
  echo "Mongo dump failed" >> "$LOG_FILE"
  send_failure_email
  exit 1
fi
 
# Upload to S3
snap run aws-cli.aws s3 cp "$BACKUP_DIR/$BACKUP_NAME" "$S3_BUCKET/" >> "$LOG_FILE" 2>&1
 
if [ $? -ne 0 ]; then
  echo "S3 upload failed" >> "$LOG_FILE"
  send_failure_email
  exit 1
fi
 
# Cleanup old local backups
find "$BACKUP_DIR" -type f -mtime +$RETENTION_DAYS -delete
 
echo "Backup completed successfully: $BACKUP_NAME" >> "$LOG_FILE"

5. Make it executable and test the failure path

chmod +x ~/mongo-backup.sh

Keep the cron entry from the backup guide. It redirects the script's output to the log, including output from the SES command. A direct manual run does not apply that outer redirection, so SES output may appear in your terminal.

Test a controlled failure with a separate copy of the script and non-production settings. Check the nonzero exit status, the backup log, and the inbox. Then restore the intended settings and confirm a successful run.

What the script detects

  • A failed mongodump triggers an email attempt and exits with status 1.
  • A failed S3 upload triggers an email attempt and exits with status 1.
  • A successful dump and upload do not send email.

It does not detect a cron job that never starts, a hung command, or every possible setup or cleanup failure. It also cannot guarantee delivery if SES, credentials, or the network fail. Check backup freshness from outside this script.

Checks for unattended runs

  • Prefer an IAM role on EC2 and restrict SES access to the sending identity.
  • Keep S3 retention separate from local cleanup.
  • Review the backup log and recent S3 objects, not just the inbox.
  • Test a restore periodically.
  • Use an independent monitor if a missing backup must page someone.

FAQ

Why use SES rather than SNS or a Slack webhook?

I want the alert in an email inbox, and this script already uses AWS CLI. SES fits that setup. If your team monitors a different channel, keep the exit-code checks and replace the notification function. Compare current pricing and operational requirements rather than assuming one service is always cheapest.

Why does this script omit set -e?

The script checks $? immediately after the dump and upload commands so it can log the failure and attempt an alert. Adding set -e without restructuring that handling can exit before those checks run. The explicit checks here cover those two commands, not every command in the script.

What happens if SES cannot send the alert?

The script still exits with status 1 after the failed backup step, but the email may never arrive. Under the shown cron redirection, SES output goes to the backup log. An independent freshness check or second notification path is needed to cover a broken email path.

Will I get another alert every night?

Yes, if the backup keeps failing and email delivery works. This example has no deduplication. If you add it, record a successful send rather than merely an attempted send, and clear the state after recovery.

Can I use SMTP instead?

Yes, but this example does not implement it. You would need to replace the notification function and test authentication, provider limits, and failure reporting for the SMTP service you choose.

Check the recovery path next

This adds failure handling to the MongoDB backup-to-S3 workflow, not complete monitoring.

Next, restore an archive from S3 into a scratch database. A successful dump, upload, and email test still do not prove that the application can use the restored data.