Catch silent backup failures before they cost you data — wire Amazon SES into your MongoDB backup script for instant email alerts.
In the previous post — automated MongoDB backup to AWS S3 — we configured automated MongoDB backups to AWS S3 on Ubuntu.
However, in production systems, backups without monitoring are risky. If a backup fails and nobody knows, your data-protection strategy fails silently.
In this guide we enhance the backup system by adding automatic failure email alerts using Amazon Simple Email Service (SES). Optional but highly recommended for any production environment.
MongoDB
→ mongodump
→ Upload to S3
→ If failure occurs
→ Send email alert via Amazon SES
Log in to the AWS Console
Open Amazon Simple Email Service (SES)
Go to Verified Identities
Click Create Identity
Choose Email Address
Enter sender email (example: alerts@yourdomain.com)
Click Create
Verify the email from your inbox
Important: If SES is in Sandbox mode, you must also verify the recipient email address. For production systems, request Production Access inside SES.
Go to: IAM → Users → select your backup IAM user → Attach policy.
Add the following permission:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ses:SendEmail",
"ses:SendRawEmail"
],
"Resource": "*"
}
]
}This allows your server to send emails using SES.
Run:
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-1If configured correctly, you should receive a test email. If you see an error, check:
Verified email identities
IAM permissions
The correct AWS region
Edit your script:
nano /home/ubuntu/mongo-backup.shReplace its contents with:
#!/bin/bash
MONGO_URI="mongodb://localhost:27017/mydatabase"
BACKUP_DIR="/home/ubuntu/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/ubuntu/mongo-backup.log"
send_failure_email() {
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
/usr/bin/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/bin/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"chmod +x /home/ubuntu/mongo-backup.shIf mongodump fails → email is sent
If S3 upload fails → email is sent
If everything succeeds → no email is sent
This ensures alerts are triggered only when necessary.
Use an IAM role instead of access keys if running on EC2
Restrict SES permissions to specific verified identities
Add an S3 lifecycle rule for automatic cleanup
Periodically test the restore process
Review logs weekly
You now have:
Automated MongoDB backup
Cloud storage in S3
Automatic failure detection
Email alert system
Production-ready monitoring
Creating helpful tools and sharing productivity insights to make your work easier.
Convert dates between DD/MM/YYYY, MM/DD/YYYY, YYYY-MM-DD, and 20+ other date formats. Free online date format converter with custom format support and one-click copy.
Send a WhatsApp message to any number without saving it to your contacts. Free, instant, no signup — perfect for businesses and one-off chats.
Generate custom QR codes for URLs, vCards, Wi-Fi, text, and more — high-resolution PNG and SVG download, free.
Convert between gold karats, purity percentage, touch, tunch, and 916/750/585/375 hallmark markings. Free online gold karat calculator and purity converter.
Convert byte arrays to strings online. Decode space- or comma-separated bytes (decimal, hex, or binary) to UTF-8 text. Free browser-based byte-to-string converter.