Back Up and Restore MongoDB with mongodump and mongorestore
Create logical MongoDB backups with mongodump, restore them with mongorestore, and verify the data on your Linux VPS. Covers archives, gzip, single collections and cron.
- Beginner
- Linux VPS
- 6 min read
To back up and restore MongoDB with mongodump and mongorestore, you export the database to BSON files or a single archive, then replay that archive into the same server or a new one. This guide walks through a full dump, a compressed archive, a single-collection restore, and a cron job that keeps the backups running without you.
mongodump and mongorestore are part of the MongoDB Database Tools, which ship separately from the server since MongoDB 4.4. They read the database over a normal connection, so the server keeps running while the dump is taken.
Before you start
You need:
- A Linux VPS with MongoDB running and reachable on
127.0.0.1:27017. - A MongoDB user with the
readrole on the databases you want to dump, andreadWriteon the target for restores. Anadminuser is simplest for a full-server dump. - The Database Tools installed. Check with
mongodump --version. If the command is missing, install themongodb-database-toolspackage from the MongoDB repository for your distribution (Ubuntu 22.04/24.04, Debian 12, AlmaLinux/Rocky 8–9). - Free disk space. A dump is roughly the size of the uncompressed data, so check with
df -h /var/backupsfirst. - A place to write. This guide uses
/var/backups/mongodb, created withsudo mkdir -p /var/backups/mongodb.
mongodump produces a logical backup. It captures documents as they are read, not a single point in time, unless you use --oplog on a replica set. For a busy database, plan the dump for a quiet window or read the oplog section below.
Back up MongoDB with mongodump
1. Test the connection
Confirm the credentials work before dumping:
mongosh "mongodb://deploy:[email protected]:27017/?authSource=admin" --eval 'db.adminCommand({listDatabases:1}).databases.map(d => d.name)'You should see a list of database names. If this fails, fix authentication first — mongodump will fail the same way.
2. Dump a single database
The --out directory receives one folder per database, each holding .bson files plus a .metadata.json per collection:
sudo mongodump \
--uri="mongodb://deploy:[email protected]:27017/?authSource=admin" \
--db=appdb \
--out=/var/backups/mongodb/$(date +%F)Expected output ends with something like writing appdb.users to /var/backups/mongodb/2026-01-01/appdb/users.bson and a count of documents written.
3. Dump everything into one compressed archive
For a full-server backup, drop --db and use --archive with --gzip. This produces a single file that is easier to copy off the VPS:
sudo mongodump \
--uri="mongodb://deploy:[email protected]:27017/?authSource=admin" \
--archive=/var/backups/mongodb/full-$(date +%F).archive \
--gzipYou can also stream the archive straight to another host over SSH:
sudo mongodump --uri="mongodb://deploy:[email protected]:27017/?authSource=admin" --archive --gzip \
| ssh [email protected] 'cat > /var/backups/mongodb/full.archive.gz'4. Take a consistent dump on a replica set
On a replica set, --oplog records every write that happens during the dump, so a restore can replay them and land on a consistent state:
sudo mongodump \
--uri="mongodb://deploy:[email protected]:27017/?authSource=admin&replicaSet=rs0" \
--oplog \
--archive=/var/backups/mongodb/consistent-$(date +%F).archive \
--gzip--oplog fails on a standalone server. If you run standalone, use a maintenance window or accept that the dump is a fuzzy snapshot.
Restore MongoDB with mongorestore
5. Restore an archive
Restore into the same server or a fresh one. Point --uri at the target:
sudo mongorestore \
--uri="mongodb://deploy:[email protected]:27017/?authSource=admin" \
--gzip \
--archive=/var/backups/mongodb/full-2026-01-01.archivemongorestore inserts documents into existing collections. If the collection already has data, you get duplicate key errors on _id. To wipe the target collections first, add --drop.
--drop deletes each collection in the dump from the target database before restoring it. Any data written since the backup is gone. Test the restore on a staging VPS or a different database name before running it against production.
sudo mongorestore \
--uri="mongodb://deploy:[email protected]:27017/?authSource=admin" \
--gzip --drop \
--archive=/var/backups/mongodb/full-2026-01-01.archive6. Restore from a directory dump
If you used --out, pass the directory that contains the database folders:
sudo mongorestore \
--uri="mongodb://deploy:[email protected]:27017/?authSource=admin" \
--drop \
/var/backups/mongodb/2026-01-01/7. Restore one collection
To pull back a single collection — a common recovery after an accidental delete — use --nsInclude:
sudo mongorestore \
--uri="mongodb://deploy:[email protected]:27017/?authSource=admin" \
--nsInclude="appdb.users" \
--dir=/var/backups/mongodb/2026-01-01Add --nsFrom and --nsTo to restore into a different database for comparison:
sudo mongorestore \
--uri="mongodb://deploy:[email protected]:27017/?authSource=admin" \
--nsInclude="appdb.users" \
--nsFrom="appdb.*" --nsTo="appdb_recovered.*" \
--dir=/var/backups/mongodb/2026-01-018. Replay the oplog
If the dump was taken with --oplog, restore it with --oplogReplay to apply the writes captured during the dump:
sudo mongorestore \
--uri="mongodb://deploy:[email protected]:27017/?authSource=admin&replicaSet=rs0" \
--gzip --oplogReplay \
--archive=/var/backups/mongodb/consistent-2026-01-01.archive9. Schedule it with cron
Run a nightly archive at 03:00 and keep a log:
sudo crontab -e0 3 * * * /usr/bin/mongodump --uri="mongodb://deploy:[email protected]:27017/?authSource=admin" --archive=/var/backups/mongodb/full-$(date +\%F).archive --gzip >> /var/log/mongodump.log 2>&1The % must be escaped as \% inside cron. Add a second job or a logrotate rule to delete archives older than your retention window — find /var/backups/mongodb -name '*.archive' -mtime +14 -delete is a simple start.
A backup on the same VPS is not a backup. Copy archives to another machine or object storage with rsync or restic, and test a restore at least once a quarter.
Check it works
Count documents in the restored database and compare with the source:
mongosh "mongodb://deploy:[email protected]:27017/?authSource=admin" \
--eval 'db.getSiblingDB("appdb").users.countDocuments({})'Also list collections and check that indexes came back — mongorestore recreates indexes from the metadata files, but only if the dump included them:
mongosh "mongodb://deploy:[email protected]:27017/?authSource=admin" \
--eval 'db.getSiblingDB("appdb").users.getIndexes()'If the counts match and your application starts against the restored data, the backup is usable.
Troubleshooting
- `Failed: error connecting to db server: no reachable servers` — MongoDB is not listening where you think. Check
sudo systemctl status mongod, confirmnet.bindIpin/etc/mongod.conf, and make sure no firewall rule blocks port 27017. - `Authentication failed` — the user exists in a different database. Add
?authSource=adminfor admin users, or the database the user was created in. - `E11000 duplicate key error` during restore — the target already holds those
_idvalues. Restore into an empty database, or re-run with--dropafter reading the warning above. - Dump finishes with zero documents — the
--dbname is wrong or the user lacks thereadrole. Run thelistDatabasescheck from step 1 and confirm the database name. - `mongodump: command not found` — the Database Tools are not installed. Install the
mongodb-database-toolspackage for your distribution and re-check withmongodump --version.
Next steps
- Install MongoDB on Ubuntu and secure it if you have not locked down authentication yet.
- Back up your Linux VPS with rsync, restic and cron to copy dumps off the server automatically.
- How to back up and restore MySQL databases with mysqldump if you also run MySQL or MariaDB alongside MongoDB.
- Pick a plan with enough disk for dumps and archives on the Linux VPS hosting page.
Was this guide helpful?
Still stuck?
Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.
Related guides
Databases
Reset a Forgotten MySQL Root Password on Linux
Recover access to MySQL or MariaDB on a Linux VPS by starting the server with skip-grant-tables, setting a new root password and restarting normally.
- Intermediate
- Linux VPS
- 5 min read
Databases
MySQL Point-in-Time Recovery Using Binary Logs
Recover a MySQL or MariaDB database to an exact moment after a bad write or dropped table by restoring a full backup and replaying binary logs up to the incident.
- Advanced
- Linux VPS
- 7 min read
Databases
How to create a database and user for a web app in MySQL or MariaDB
Set up a utf8mb4 database and a dedicated user with a strong password for WordPress, Laravel or any web app, then test the login before you configure the app.
- Beginner
- Linux VPS
- 4 min read