Backup MongoDB – mongodump
Backup MongoDB – To backup data in MongoDB, MongoDB provides mongodump utility to backup data at different levels. Using mongodump, you may backup only a particular collection of a database, or single database, or all databases of a specified MongoDB instance.
mongodump creates a binary export of MongoDB data, usually as .bson files with matching metadata files. These files can later be restored with mongorestore. It is useful for small to medium backups, development copies, migration preparation, and collection-level exports.
In this tutorial, we will learn how to use mongodump utility to take backup of MongoDB Databases/Collections.
For production systems, take backups during a planned window, store the dump outside the database server, and test restoration on a separate MongoDB instance. A backup is not complete until you know that it can be restored correctly.
MongoDB mongodump Backup Syntax for Databases and Collections
mongodump is an utility that could be run from a Terminal.
Following are the usages of mongodump utility.
1. To create backup for a collection in database, use mongodump with options: Collection Name and Database Name.
mongodump --collection COLLECTION_NAME --db DB_NAME
2. To create backup for a database, use mongodump with options: Database Path and Output Directory for Backup.
mongodump --dbpath DB_PATH --out /OUTPUT/DIRECTORY/FOR/BACKUP
The --dbpath style is a legacy/offline pattern and is not the preferred approach for normal modern deployments. In most current MongoDB backups, run mongodump against a running mongod or mongos process by using connection options such as host, port, database name, authentication options, or a MongoDB connection URI.
3. To create backup all the databases linked to a specific MongoDB instance, use mongodump with options: Host Name and Port Number at which MongoDB instance is running.
mongodump --host HOST_NAME --port PORT_NUMBER
4. Running mongodump utility without any options creates backup for all databases linked to the MongoDB running at localhost (127.0.0.1) and port 27017.
mongodump
Note : When no –out option is specified, a directory named dump is created at the location from which mongodump utility is run.
Before Running mongodump on a MongoDB Database
Before taking a MongoDB backup with mongodump, confirm these details.
- The MongoDB server is reachable from the machine where you run
mongodump. - The backup user has the required read permissions for the database or deployment being dumped.
- The output location has enough free disk space for the dump.
- The backup folder name includes a date or build identifier so that older dumps are not overwritten accidentally.
- You have a restore plan using
mongorestore, especially for production data.
You can check whether the database tools are available by running the following command.
mongodump --version
If the command is not found, install MongoDB Database Tools for your operating system before continuing.
Examples for MongoDB Backup
Make sure MongoDB Instance is running. If not, start one as shown below :
~$ sudo mongod -dbpath /var/lib/mongodb/
Backup MongoDB
Open a Terminal and run mongodump utility with no arguments
~$ mongodump
2018-01-07T18:06:41.181+0530 writing admin.system.version to
2018-01-07T18:06:41.182+0530 done dumping admin.system.version (1 document)
2018-01-07T18:06:41.182+0530 writing school.students to
2018-01-07T18:06:41.182+0530 writing tutorialkart.people to
2018-01-07T18:06:41.182+0530 writing tutorialkart.stores to
2018-01-07T18:06:41.183+0530 writing tutorialkart.customers to
2018-01-07T18:06:41.183+0530 done dumping school.students (9 documents)
2018-01-07T18:06:41.183+0530 writing tutorialkart.webpages to
2018-01-07T18:06:41.184+0530 done dumping tutorialkart.people (8 documents)
2018-01-07T18:06:41.184+0530 writing newdb.users to
2018-01-07T18:06:41.184+0530 done dumping tutorialkart.customers (5 documents)
2018-01-07T18:06:41.184+0530 writing school.totals to
2018-01-07T18:06:41.184+0530 done dumping newdb.users (4 documents)
2018-01-07T18:06:41.184+0530 writing fruits.seasonal to
2018-01-07T18:06:41.185+0530 done dumping tutorialkart.stores (5 documents)
2018-01-07T18:06:41.185+0530 writing tutorialkart.myNewCollection to
2018-01-07T18:06:41.185+0530 done dumping school.totals (3 documents)
2018-01-07T18:06:41.185+0530 done dumping tutorialkart.webpages (5 documents)
2018-01-07T18:06:41.185+0530 done dumping fruits.seasonal (1 document)
2018-01-07T18:06:41.186+0530 done dumping tutorialkart.myNewCollection (0 documents)
~$ cd dump/
~/dump$ ls
admin fruits newdb school tutorialkart
~/dump$
This creates a dump directory in the current working directory. Each database gets its own folder, and each collection is stored as a BSON file with a corresponding metadata file.
Backup MongoDB Collection
In this example, we shall backup “webpages” collection of “tutorialkart” db.
~$ mongodump --collection webpages --db tutorialkart
2018-01-07T18:24:16.928+0530 writing tutorialkart.webpages to
2018-01-07T18:24:16.953+0530 done dumping tutorialkart.webpages (5 documents)
~$ cd dump
~/dump$ ls
tutorialkart
A collection-level dump is useful when you need to copy only one collection instead of backing up the full database. For a clearer backup path, specify --out with the collection backup command.
mongodump --db tutorialkart --collection webpages --out /home/tk/backups/tutorialkart-webpages
Backup MongoDB Database
In this example, we shall backup “tutorialkart” db.
~$ mongodump --db tutorialkart
2018-01-07T18:25:43.148+0530 writing tutorialkart.people to
2018-01-07T18:25:43.149+0530 writing tutorialkart.customers to
2018-01-07T18:25:43.149+0530 writing tutorialkart.stores to
2018-01-07T18:25:43.149+0530 writing tutorialkart.webpages to
2018-01-07T18:25:43.149+0530 done dumping tutorialkart.people (8 documents)
2018-01-07T18:25:43.150+0530 writing tutorialkart.myNewCollection to
2018-01-07T18:25:43.150+0530 done dumping tutorialkart.customers (5 documents)
2018-01-07T18:25:43.150+0530 done dumping tutorialkart.stores (5 documents)
2018-01-07T18:25:43.150+0530 done dumping tutorialkart.myNewCollection (0 documents)
2018-01-07T18:25:43.150+0530 done dumping tutorialkart.webpages (5 documents)
~$ cd dump/
~/dump$ ls
tutorialkart
~/dump$ cd tutorialkart/
~/dump/tutorialkart$ ls
customers.bson myNewCollection.bson people.bson stores.bson webpages.bson
customers.metadata.json myNewCollection.metadata.json people.metadata.json stores.metadata.json webpages.metadata.json
Database-level backup is the most common mongodump option when you want all collections from one database. It is easier to restore selectively later because each collection is written as a separate file under the database folder.
Backup MongoDB Databases linked to a specific MongoDB instance
In this example, we shall backup all databases linked to the MongoDB instance running at 127.0.0.1:27017
~$ mongodump --host 127.0.0.1 --port 27017
2018-01-07T18:06:41.181+0530 writing admin.system.version to
2018-01-07T18:06:41.182+0530 done dumping admin.system.version (1 document)
2018-01-07T18:06:41.182+0530 writing school.students to
2018-01-07T18:06:41.182+0530 writing tutorialkart.people to
2018-01-07T18:06:41.182+0530 writing tutorialkart.stores to
2018-01-07T18:06:41.183+0530 writing tutorialkart.customers to
2018-01-07T18:06:41.183+0530 done dumping school.students (9 documents)
2018-01-07T18:06:41.183+0530 writing tutorialkart.webpages to
2018-01-07T18:06:41.184+0530 done dumping tutorialkart.people (8 documents)
2018-01-07T18:06:41.184+0530 writing newdb.users to
2018-01-07T18:06:41.184+0530 done dumping tutorialkart.customers (5 documents)
2018-01-07T18:06:41.184+0530 writing school.totals to
2018-01-07T18:06:41.184+0530 done dumping newdb.users (4 documents)
2018-01-07T18:06:41.184+0530 writing fruits.seasonal to
2018-01-07T18:06:41.185+0530 done dumping tutorialkart.stores (5 documents)
2018-01-07T18:06:41.185+0530 writing tutorialkart.myNewCollection to
2018-01-07T18:06:41.185+0530 done dumping school.totals (3 documents)
2018-01-07T18:06:41.185+0530 done dumping tutorialkart.webpages (5 documents)
2018-01-07T18:06:41.185+0530 done dumping fruits.seasonal (1 document)
2018-01-07T18:06:41.186+0530 done dumping tutorialkart.myNewCollection (0 documents)
~$ cd dump/
~/dump$ ls
admin fruits newdb school tutorialkart
~/dump$
Use the host and port form when the MongoDB instance is not using the default local connection or when you want the command to clearly document the server being backed up.
Backup to a particular directory
–out option could be used to create backup at a particular directory
~$ mongodump --out /home/tk/mongo_dump/
2018-01-07T18:37:19.793+0530 writing admin.system.version to
2018-01-07T18:37:19.795+0530 done dumping admin.system.version (1 document)
2018-01-07T18:37:19.795+0530 writing school.students to
2018-01-07T18:37:19.795+0530 writing tutorialkart.people to
2018-01-07T18:37:19.795+0530 writing tutorialkart.stores to
2018-01-07T18:37:19.795+0530 writing tutorialkart.customers to
2018-01-07T18:37:19.796+0530 done dumping school.students (9 documents)
2018-01-07T18:37:19.796+0530 writing tutorialkart.webpages to
2018-01-07T18:37:19.797+0530 done dumping tutorialkart.people (8 documents)
2018-01-07T18:37:19.797+0530 writing newdb.users to
2018-01-07T18:37:19.797+0530 done dumping tutorialkart.stores (5 documents)
2018-01-07T18:37:19.797+0530 writing school.totals to
2018-01-07T18:37:19.798+0530 done dumping tutorialkart.customers (5 documents)
2018-01-07T18:37:19.798+0530 writing fruits.seasonal to
2018-01-07T18:37:19.798+0530 done dumping fruits.seasonal (1 document)
2018-01-07T18:37:19.799+0530 writing tutorialkart.myNewCollection to
2018-01-07T18:37:19.799+0530 done dumping school.totals (3 documents)
2018-01-07T18:37:19.799+0530 done dumping newdb.users (4 documents)
2018-01-07T18:37:19.799+0530 done dumping tutorialkart.myNewCollection (0 documents)
2018-01-07T18:37:19.800+0530 done dumping tutorialkart.webpages (5 documents)
~$ cd /home/tk/mongo_dump/
~/mongo_dump$ ls
admin fruits newdb school tutorialkart
For recurring backups, use a dated directory name so that one dump does not replace another dump.
mongodump --db tutorialkart --out /home/tk/mongo_backups/tutorialkart-$(date +%F)
mongodump Backup with MongoDB Authentication
If authentication is enabled, provide a username, password, and authentication database. In many installations, the user credentials are stored in the admin database even when the actual data is in another database.
mongodump --host 127.0.0.1 --port 27017 \
--username backupUser \
--password \
--authenticationDatabase admin \
--db tutorialkart \
--out /home/tk/mongo_backups/tutorialkart-auth-backup
When --password is supplied without a value, the tool asks for the password interactively. This is safer than placing the password directly in shell history.
mongodump Backup Using a MongoDB Connection URI
A connection URI is useful when the MongoDB deployment requires multiple connection details, such as replica set name, authentication source, or TLS settings. Keep credentials protected if you use a URI in scripts.
mongodump --uri="mongodb://backupUser@127.0.0.1:27017/tutorialkart?authSource=admin" \
--out /home/tk/mongo_backups/tutorialkart-uri-backup
Use the connection string format that matches your MongoDB deployment. For cloud-hosted or managed MongoDB, copy the connection string from the provider and adjust it for backup use.
Archive and Compressed MongoDB Backup with mongodump
By default, mongodump writes a directory tree. You can also write a single archive file and compress the output. This is convenient when moving the backup to another server or storing it in object storage.
mongodump --db tutorialkart --archive=/home/tk/mongo_backups/tutorialkart.archive --gzip
A compressed archive is easier to copy, but a directory dump is easier to inspect manually. Choose the format based on how you plan to store and restore the backup.
Verify MongoDB Backup Files Created by mongodump
After running mongodump, check that the backup directory or archive exists and contains data. For a directory-style dump, you should see database folders and .bson files.
find /home/tk/mongo_backups/tutorialkart-$(date +%F) -type f | sort
ls -lh /home/tk/mongo_backups/
For a more reliable check, restore the dump to a separate test database or test server using mongorestore. Do not test a restore directly on a production database unless you have a controlled restore plan.
Useful mongodump Options for MongoDB Backups
| Option | Purpose | Example use |
|---|---|---|
--db | Selects one database to dump. | --db tutorialkart |
--collection | Selects one collection from a database. | --collection webpages |
--out | Writes a directory-style dump to a chosen path. | --out /home/tk/backups |
--host and --port | Connects to a specific MongoDB server and port. | --host 127.0.0.1 --port 27017 |
--uri | Uses a MongoDB connection string. | --uri="mongodb://..." |
--archive | Writes the dump as a single archive file or stream. | --archive=backup.archive |
--gzip | Compresses the dump output. | --archive=backup.archive --gzip |
When mongodump Is Not Enough for a MongoDB Backup Plan
mongodump is a logical backup tool. It reads data from MongoDB and writes export files. For very large databases, strict point-in-time recovery needs, high write traffic, or enterprise backup policies, you may need a broader backup strategy that includes filesystem snapshots, managed backup features, replica set planning, oplog-based recovery, or dedicated backup software.
For development and many controlled backup tasks, mongodump is simple and effective. For production systems, match the backup method to the recovery time, recovery point, database size, and operational risk.
FAQ on MongoDB Backup with mongodump
Does mongodump backup all MongoDB databases by default?
Yes. If you run mongodump without --db or --collection, it connects to the default local MongoDB instance and creates a dump for the databases it can read.
How do I backup only one MongoDB collection with mongodump?
Use both --db and --collection. For example, mongodump --db tutorialkart --collection webpages backs up only the webpages collection from the tutorialkart database.
Where does mongodump store the MongoDB backup?
If you do not specify --out, mongodump creates a dump directory in the location from where you run the command. Use --out when you want to store the backup in a specific directory.
Can mongodump backup a remote MongoDB server?
Yes. Use --host and --port, or use --uri with the correct connection string. If authentication is enabled, include the username and authentication database, and enter the password securely.
Is a mongodump backup enough for production MongoDB?
It depends on the production requirements. mongodump is useful for logical backups, but large or high-write systems may require additional backup methods and a tested restore procedure.
Editorial QA Checklist for This mongodump Backup Tutorial
- Confirm that collection backup examples include both
--dband--collection. - Keep the original
mongodumpexamples unchanged while explaining modern connection-based usage. - Warn readers to test MongoDB backups with
mongorestoreon a separate test instance. - Use dated output directory names in new recurring backup examples.
- Check that new command-line blocks use the
language-bashPrismJS class.
Conclusion
In this MongoDB Tutorial, we have learnt to Backup MongoDB Databases and the control provided by mongodump utility for various levels in backup : Collection , Single Database, All Databases linked to a MongoDB Instance. We also looked at output directories, authentication, connection URI usage, compressed archives, and verification steps that make a MongoDB backup more reliable.
TutorialKart.com