Databases

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.

BeginnerLinux server6 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 read role on the databases you want to dump, and readWrite on the target for restores. An admin user is simplest for a full-server dump.
  • The Database Tools installed. Check with mongodump --version. If the command is missing, install the mongodb-database-tools package 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/backups first.
  • A place to write. This guide uses /var/backups/mongodb, created with sudo mkdir -p /var/backups/mongodb.

Note: 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:

Bash
mongosh "mongodb://deploy:YourPassword@127.0.0.1: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:

Bash
sudo mongodump \
  --uri="mongodb://deploy:YourPassword@127.0.0.1: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:

Bash
sudo mongodump \
  --uri="mongodb://deploy:YourPassword@127.0.0.1:27017/?authSource=admin" \
  --archive=/var/backups/mongodb/full-$(date +%F).archive \
  --gzip

You can also stream the archive straight to another host over SSH:

Bash
sudo mongodump --uri="mongodb://deploy:YourPassword@127.0.0.1:27017/?authSource=admin" --archive --gzip \
  | ssh deploy@203.0.113.10 '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:

Bash
sudo mongodump \
  --uri="mongodb://deploy:YourPassword@127.0.0.1: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:

Bash
sudo mongorestore \
  --uri="mongodb://deploy:YourPassword@127.0.0.1:27017/?authSource=admin" \
  --gzip \
  --archive=/var/backups/mongodb/full-2026-01-01.archive

mongorestore 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.

Warning: --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.

Bash
sudo mongorestore \
  --uri="mongodb://deploy:YourPassword@127.0.0.1:27017/?authSource=admin" \
  --gzip --drop \
  --archive=/var/backups/mongodb/full-2026-01-01.archive

6. Restore from a directory dump

If you used --out, pass the directory that contains the database folders:

Bash
sudo mongorestore \
  --uri="mongodb://deploy:YourPassword@127.0.0.1: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:

Bash
sudo mongorestore \
  --uri="mongodb://deploy:YourPassword@127.0.0.1:27017/?authSource=admin" \
  --nsInclude="appdb.users" \
  --dir=/var/backups/mongodb/2026-01-01

Add --nsFrom and --nsTo to restore into a different database for comparison:

Bash
sudo mongorestore \
  --uri="mongodb://deploy:YourPassword@127.0.0.1:27017/?authSource=admin" \
  --nsInclude="appdb.users" \
  --nsFrom="appdb.*" --nsTo="appdb_recovered.*" \
  --dir=/var/backups/mongodb/2026-01-01

8. Replay the oplog

If the dump was taken with --oplog, restore it with --oplogReplay to apply the writes captured during the dump:

Bash
sudo mongorestore \
  --uri="mongodb://deploy:YourPassword@127.0.0.1:27017/?authSource=admin&replicaSet=rs0" \
  --gzip --oplogReplay \
  --archive=/var/backups/mongodb/consistent-2026-01-01.archive

9. Schedule it with cron

Run a nightly archive at 03:00 and keep a log:

Bash
sudo crontab -e
0 3 * * * /usr/bin/mongodump --uri="mongodb://deploy:YourPassword@127.0.0.1:27017/?authSource=admin" --archive=/var/backups/mongodb/full-$(date +\%F).archive --gzip >> /var/log/mongodump.log 2>&1

The % 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.

Tip: 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:

Bash
mongosh "mongodb://deploy:YourPassword@127.0.0.1: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:

Bash
mongosh "mongodb://deploy:YourPassword@127.0.0.1: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, confirm net.bindIp in /etc/mongod.conf, and make sure no firewall rule blocks port 27017.
  • Authentication failed — the user exists in a different database. Add ?authSource=admin for admin users, or the database the user was created in.
  • E11000 duplicate key error during restore — the target already holds those _id values. Restore into an empty database, or re-run with --drop after reading the warning above.
  • Dump finishes with zero documents — the --db name is wrong or the user lacks the read role. Run the listDatabases check from step 1 and confirm the database name.
  • mongodump: command not found — the Database Tools are not installed. Install the mongodb-database-tools package for your distribution and re-check with mongodump --version.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.