Automated MongoDB Backups — Production‑Ready Guide
Source: Dev.to
Why Backups Matter (More Than You Think)
Production databases will fail — human error, cloud outages, bad deployments, or accidental deletes. The only real safety net is a reliable, automated backup system that you trust enough to restore from at 3 AM.
In this guide we walk through a battle‑tested MongoDB backup system built using:
mongodump/mongorestore- Node.js +
node-cron - Amazon S3
- MongoDB (as backup metadata store)
By the end you’ll understand how to safely back up MongoDB, store it in S3, clean old backups, and fully restore your database if disaster strikes.
System Goals
We wanted a system that:
- Backs up multiple MongoDB databases
- Runs daily, weekly, and monthly automatically
- Stores backups safely in S3
- Tracks everything in MongoDB
- Deletes expired backups automatically
- Can fully restore a deleted database
- Requires no manual steps or risky shortcuts
Core Concept: MongoDB Dumps
MongoDB provides a native backup tool called mongodump.
mongodump --uri="" --archive --gzip --oplog
What This Does
--archive→ outputs a single file (easy to store)--gzip→ compresses the backup--oplog→ ensures consistent snapshots for replica sets
Result: one .archive.gz file per backup. This backup is read‑only — your original database is never modified.
Supporting Multiple Databases
Instead of hard‑coding one database, define a generic list of MongoDB connections:
const databases = [
{ name: "app-db", uri: process.env.MONGO_URI_1 },
{ name: "analytics-db", uri: process.env.MONGO_URI_2 }
];
Each entry:
- Uses the same backup logic
- Generates its own backup file
- Stores metadata separately
This keeps the system generic and reusable.
Automated Cron Jobs
All backups run inside the Node.js app using node-cron.
| Job Type | Schedule |
|---|---|
| Daily | Every day |
| Weekly | Every Sunday |
| Monthly | 1st day of month |
| Cleanup | Every day |
Each cron job:
- Runs
mongodump - Uploads the file to S3
- Saves metadata in MongoDB
No external cron servers are required.
S3 Storage Structure
Backups are uploaded as .archive.gz files to an S3 bucket. A recommended folder structure:
s3://mongo-backups/
{dbName}/
daily/
weekly/
monthly/
{dbName}→ sanitized MongoDB database namedaily | weekly | monthly→ backup frequency
Production Safety Rules
- Database names are sanitized (lowercase, no spaces)
- Files are uploaded only after the dump succeeds
- Local files are deleted only after the S3 upload succeeds
This structure works for any project or organization and scales cleanly.
Backup Metadata in MongoDB
MongoDB stores generic backup metadata, independent of any company or product:
{
"dbName": "app-db",
"backupType": "daily",
"storageProvider": "s3",
"storageKey": "mongo-backups/app-db/daily/backup.archive.gz",
"createdAt": ISODate(),
"expireAt": ISODate()
}
Why this matters
- MongoDB becomes the source of truth
- Every backup is auditable
- Cleanup logic is safe and deterministic
- Nothing is deleted unless it is explicitly tracked
Automatic Cleanup of Expired Backups
A separate daily cron job:
- Finds backups where
expireAt < now - Deletes the file from S3
- Deletes the record from MongoDB
If a backup isn’t recorded in MongoDB, it is not deleted. This prevents accidental data loss and unlimited storage growth.
Disaster Recovery (Restore Process)
If any MongoDB database is deleted or corrupted:
Step 1: Download the latest backup
aws s3 cp s3://mongo-backups/{dbName}/daily/latest.archive.gz .
Step 2: Restore using mongorestore
mongorestore --gzip --archive=latest.archive.gz --drop
--dropclears existing data- The database is rebuilt from the archive
This process works for any MongoDB deployment.
Safety Guarantees
- Original DB is never modified
- Backups are full & consistent
- Storage growth is controlled
- Every backup is tracked
- Restore is always possible
This is production‑grade, not a demo script.
Final Thought
This guide demonstrated a generic, production‑safe MongoDB backup strategy using mongodump, scheduled jobs, S3 storage, MongoDB‑tracked metadata, automated cleanup, and a reliable restore flow — adaptable to any project or organization.
Happy building 🚀