Step-by-step guide to scheduling daily MongoDB backups, uploading them to AWS S3, and managing local retention — production-ready and battle-tested.
✅ Automated MongoDB backup
✅ Upload to AWS S3
✅ Run daily at 2 AM IST
✅ Retain only the last 7 days locally
✅ Production-safe cron execution
MongoDB
↓
mongodump (gzip archive)
↓
AWS S3 upload
↓
Local retention cleanup
We install AWS CLI using Snap:
sudo snap install aws-cli --classicVerify:
aws --versionIn the AWS Console:
Go to IAM → Users → Create User
Enable programmatic access
Attach a policy
For quick setup:
AmazonS3FullAccessFor production (recommended), use a restricted policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:PutObject",
"s3:ListBucket"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::your-bucket-name",
"arn:aws:s3:::your-bucket-name/*"
]
}
]
}Save:
Access Key
Secret Key
aws configureEnter:
Access Key
Secret Key
Region (e.g. ap-south-1)
Output format: jsonCreate the file:
nano /home/ubuntu/mongo-backup.shPaste:
#!/bin/bash
# === MongoDB Daily Backup Script ===
set -e
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
LOG_FILE="/home/ubuntu/mongo-backup.log"
mkdir -p "$BACKUP_DIR"
echo "Starting backup: $DATE" >> "$LOG_FILE"
# Dump database
/usr/bin/mongodump --uri="$MONGO_URI" --archive="$BACKUP_DIR/$BACKUP_NAME" --gzip >> "$LOG_FILE" 2>&1
# Upload to S3
/snap/bin/aws s3 cp "$BACKUP_DIR/$BACKUP_NAME" "$S3_BUCKET/" >> "$LOG_FILE" 2>&1
# Cleanup old local backups
find "$BACKUP_DIR" -type f -mtime +$RETENTION_DAYS -delete
echo "Backup completed successfully: $BACKUP_NAME" >> "$LOG_FILE"Make it executable:
chmod +x /home/ubuntu/mongo-backup.shTest manually:
/home/ubuntu/mongo-backup.shIf your server is in UTC, 2 AM IST = 20:30 UTC (previous day).
Edit cron:
crontab -eAdd:
30 20 * * * /home/ubuntu/mongo-backup.sh >> /home/ubuntu/mongo-backup.log 2>&1Go to S3 → Bucket → Management → Lifecycle Rule and create a rule to delete objects after 30 days. This prevents infinite storage growth.
Use an IAM role instead of access keys (if running on EC2)
Restrict S3 permissions to a specific bucket
Enable S3 versioning
Enable server-side encryption (SSE-S3 or SSE-KMS)
Periodically test the restore process — a backup you've never restored is not a backup
Up next: add failure email alerts via Amazon SES so you know immediately when a backup breaks.
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.