MongoDB Backup and Restore
Hardware fails, people delete the wrong collection, and software bugs corrupt data. A backup is a saved copy of your database that lets you return to a healthy state after any of these events. Every serious MongoDB setup includes a backup plan and a tested way to restore it.
The Photo Album Picture
A backup works like a photo album of your data. Each photo captures how the database looked at one moment. A restore pulls a photo out of the album and rebuilds the database from it.
Live database Backup file Restored database
+---------------+ mongodump +-------------+ mongorestore +---------------+
| shop | ---------> | shop.archive | ----------> | shop (copy) |
| orders, users | +-------------+ | orders, users |
+---------------+ stored on a safe disk +---------------+Backup Methods at a Glance
| Method | Best For | Output |
|---|---|---|
| mongodump / mongorestore | Small and medium databases | BSON files |
| mongoexport / mongoimport | Moving a single collection as JSON or CSV | Readable text files |
| File system snapshot | Large databases | Copy of the data folder |
| Cloud backup service | Automatic schedules and point-in-time recovery | Managed by the provider |
mongodump: Save the Data
The mongodump tool belongs to the MongoDB Database Tools package. Run it from a normal terminal, not from inside mongosh.
Back Up Everything
mongodump --uri="mongodb://localhost:27017" --out=/backup/fullThe command creates one folder per database inside /backup/full.
Back Up One Database
mongodump --uri="mongodb://localhost:27017" --db=shop --out=/backup/shopBack Up One Collection
mongodump --uri="mongodb://localhost:27017" --db=shop --collection=orders --out=/backup/ordersCompress into a Single File
mongodump --uri="mongodb://localhost:27017" --db=shop --gzip --archive=/backup/shop.archiveThe --gzip option shrinks the files, and the --archive option packs everything into one file that is easy to copy and store.
mongorestore: Bring the Data Back
Restore from a Folder
mongorestore --uri="mongodb://localhost:27017" /backup/fullRestore from an Archive
mongorestore --uri="mongodb://localhost:27017" --gzip --archive=/backup/shop.archiveReplace Existing Data
mongorestore --uri="mongodb://localhost:27017" --drop /backup/fullThe --drop option deletes each existing collection before MongoDB restores it from the backup. Use it with care, because it removes current data.
Restore to a Different Name
mongorestore --nsFrom="shop.*" --nsTo="shop_test.*" --gzip --archive=/backup/shop.archiveThis command rebuilds the shop database under the name shop_test. Safe testing of a backup depends on this kind of copy.
Export and Import for Readable Files
The mongoexport tool writes documents as JSON or CSV text. People use it to share data with spreadsheets or other systems:
mongoexport --uri="mongodb://localhost:27017" --db=shop --collection=products --out=products.json
mongoimport --uri="mongodb://localhost:27017" --db=shop --collection=products --file=products.jsonExport files lose some data type details, such as exact number types. Choose mongodump for full backups and mongoexport for data sharing.
Point-in-Time Recovery
A nightly backup misses everything that happens after it finishes. A replica set records every change in the oplog. The --oplogReplay and --oplogLimit options of mongorestore, paired with a dump taken using --oplog, replay those changes up to a chosen second.
Backup at 02:00 ---------------- Mistake at 14:30 ---------------- Now
| |
full dump oplog replay stops
just before 14:30Managed services such as MongoDB Atlas offer continuous backups that handle this process automatically.
Backup Plan Checklist
- Schedule backups with a task scheduler, such as cron on Linux.
- Store at least one copy on a different machine or in a different location.
- Follow the 3-2-1 rule: three copies, two storage types, one copy off-site.
- Encrypt backup files and limit who can read them.
- Record the backup time and database size in a log.
- Delete old backups according to a clear retention rule.
Test the Restore
An untested backup carries no guarantee. Restore a copy to a test server on a regular schedule and confirm that document counts and key records match the original. Teams that practice a restore handle a real emergency calmly.
Quick Restore Test
mongorestore --nsFrom="shop.*" --nsTo="restore_check.*" --gzip --archive=/backup/shop.archive
mongosh --eval "db.getSiblingDB('restore_check').orders.countDocuments()"Compare the printed number with the order count in the live database.
Summary
Backups protect data against hardware failure, mistakes, and software bugs. The mongodump and mongorestore tools handle most small and medium needs, while snapshots and cloud services suit large deployments. Oplog replay brings a database back to an exact moment. A solid plan combines scheduled backups, off-site copies, encryption, and regular restore tests.
