In the previous blog 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 will enhance the backup system by adding automatic failure email alerts using Amazon Simple Email Service (SES).
This is optional — but highly recommended for production environments.
MongoDB
→ mongodump
→ Upload to S3
→ If failure occurs
→ Send Email Alert via Amazon SES
Login to 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-1
If configured correctly, you should receive a test email.
If you receive an error:
• Verify email identities
• Check IAM permissions
• Confirm correct region
Edit your script:
nano /home/ubuntu/mongo-backup.sh
Replace content 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.sh
If 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 IAM Role instead of access keys if running on EC2
• Restrict SES permissions to specific verified identities
• Add S3 lifecycle rule for automatic cleanup
• Periodically test 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 your selected date into multiple popular date formats — instantly, accurately, and ready to copy.
Discover the simplicity of sending WhatsApp messages without saving phone numbers with Tooloogle's easy-to-use online tool. Instantly message anyone directly on WhatsApp in a hassle-free manner. Ideal for businesses and personal use alike. No registration required. Start messaging now!
Generate Custom QR Codes Easily with Tooloogle's QR Code Generator
Gold Purity calculator helps you calculate the purity of the gold by using the karat rating
Easily convert byte arrays to readable text strings with our efficient online converter tool.