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

By RunRacks Team · Published

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.

Back up MongoDB with mongodump

1. Test the connection

Confirm the credentials work before dumping:

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

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

Terminal
sudo mongodump \
  --uri="mongodb://deploy:[email protected]:27017/?authSource=admin" \
  --archive=/var/backups/mongodb/full-$(date +%F).archive \
  --gzip

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

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

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

Terminal
sudo mongorestore \
  --uri="mongodb://deploy:[email protected]: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.

Terminal
sudo mongorestore \
  --uri="mongodb://deploy:[email protected]: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:

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

Terminal
sudo mongorestore \
  --uri="mongodb://deploy:[email protected]: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:

Terminal
sudo mongorestore \
  --uri="mongodb://deploy:[email protected]: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:

Terminal
sudo mongorestore \
  --uri="mongodb://deploy:[email protected]: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:

Terminal
sudo crontab -e
Text
0 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>&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.

Check it works

Count documents in the restored database and compare with the source:

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

Terminal
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, 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 RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.

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