Open In App

How to Create a MongoDB Dump of Database?

Last Updated : 05 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MongoDB is a popular NoSQL database known for its flexibility and scalability. However, like any database system, it is crucial to have a backup plan in place to protect our data from loss or corruption. One of the best ways to back up your MongoDB database is by creating a dump. In this article, we will go through the process of creating a MongoDB dump, ensuring that our data remains safe and secure with the help of examples, and so on.

How to Create a MongoDB Dump of Database?

Data loss or corruption can occur due to various reasons such as hardware failure, human error, or software bugs. To overcome the risk of data loss, it is important to regularly back up your MongoDB database. To dump your database for backup you call this command on your terminal which is mongodump. Mongodump is a simple MongoDB straightforward backup tool that generates high-fidelity BSON files from the underlying database. Below are the steps that help us to create a backup for an entire server are as follow:

  1. Create Direct Backups Using Mongodump
  2. Backup a Remote MongoDB Instance
  3. Backup a Secure MongoDB Instance
  4. Select Databases & Collections
  5. Change the Backup Directory
  6. Create an Archive File
  7. Compress the MongoDB Backup
  8. Restore Database

Let’s understand step by step:

Step 1: Create Direct Backups Using Mongodump

You can run the mongodump command using the below syntax from the system command line as follows:

mongodump <options> <connection-string>

This structure also permits you to connect to a Mongo database with the –uri command along with a formatted string or flag such as –user, –db, and –password. However, you can’t use more than 1 flag in a single command.

Alternatively, you can use the default configurations and create a Mongo Backup via Mongodump command as follows:

Mongodump

This statement operates on the assumption that your database resides in localhost (127.0.0.1), uses port 27017, and requires no access authentication. This backup process creates a dump folder directly in the current directory as shown in the below image.

mongodump

mongodump

Step 2: Backup a Remote MongoDB Instance

As mentioned in Step 1, you can customize a host and a port number with the –uri connection string using the following syntax:

mongodump --uri="mongodb://<host URL/IP>:<Port>" [additional options]

Moreover, you can set up a server connection with the host option using the following command:

mongodump --host="<host URL/IP>:<Port>"  [additional options]

If you also wish to specify the port number, implement the following statement:

mongodump --host="<host URL/IP>" --port=<Port> [additional options]

The following code demonstrates the process of backing up a remote MongoDB instance:

mongodump --host="10.10.10.59" --port=27017

Output:

Backup-a-Remote-MongoDB-Instance

Output

Step 3: Backup a Secure MongoDB Instance

MongoDB’s Mongodump command allows you to implement access control mechanisms for your data backups. This will require you to provide a Username, Password, and specific Authentication options in the following syntax:

mongodump --authenticationDatabase=<Database> -u=<Username> -p=<Password> [additional options

For instance, you can use a Username & Password to connect to a remote MongoDB instance using the following command:

mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword"

Output:

Backup-a-Secure-MongoDB-Instance

Output

Step 4: Select Databases & Collections

In Step 3 you learned how to back up a remote database using Mongodump. This step involves the –db and –collection options that indicate a database and a collection that requires backing up. You can run the –db option in a standalone manner, but to execute a collection, you have to specify a database. Moreover, you can remove a collection from the backup process, by using the –excludeCollection option.

To select a particular database use the following command:

mongodump  --db=<Backup Target - Database> [additional options]

If you wish to select a whole collection, execute the following:

mongodump  --db=<Backup Target - Database> --collection=<Collection Name> [additional options]

To exclude a particular collection, run the following command:

mongodump  --db=<Backup Target - Database> --excludeCollection=<Collection Name> [additional options]

Step 5: Change the Backup Directory

You can leverage the –out option to specify the backup folder’s location in the following way:

mongodump --out=<Directory Location> [additional options]

Now, if you wish to switch the backup directory with the “dbbackup” folder, run:

mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --out=dbbackup

Output:

Select-Databases-&-Collections

Output

Step 6: Create an Archive File

The Mongodump utility provides you with a method to create an archive file. You can use the –archive option can for specifying the file to be archived. In case no specification is given, the output will be in the standard form (stdout).

Keep in mind that you can’t use the –archive option along with the –out option.

mongodump --archive=<file> [additional options]

You can easily define any archive file using the following command:

mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --archive=db.archive

Output:

Create-an-Archive-File

Output

Step 7: Compress the MongoDB Backup

Now, since you know how to backup data using Mongodump, it’s time to understand the process of compressing these files. You can use the –gzip option to compress the JSON and BSON files individually using the following command:

mongodump --gzip [additional options]

For instance, the below statement will compress a complete Mongo database:

mongodump --host=10.10.10.59 --port=27017 --authenticationDatabase="admin" -u="barryadmin" -p="testpassword" --gzip

Output:

Compress-the-MongoDB-Backup

Output

Step 8: Restore Database

Apart from excellent backups, MongoDB also provides you the facility to restore your data. The Mongorestore command will seamlessly load data from the Mongodumb backups and restore your Mongo database. The mongorestore command, however, can not overwrite documents present in the database if the id for the document already exists. Otherwise, Mongorestore will generate a new database or will add data to the existing one.

The Mongorestore command requires you to mention the path to your dump directory and the syntax is s follows:

mongorestore dump/

Moreover, in the Mongostore command:

  • You must specify the –uri flag or provide every standard connection flag.
  • You can directly use the most preferred –nsInclude option to restore different collections. It allows users to choose a namespace when restoring the collections for a database.

For instance, in the following command, you can isolate and import the db1 database to restore it in your local environment:

mongorestore --db=redbase --nsInclude="db1.*" dump/

This will restore all the collections of db1 that were earlier dumped. However, it won’t touch the data stored in db2 even both dn1 and db2 have their data stored in the same dump directory.

Benefits of MongoDump

Using the Mongodump command to create Data Backups has the following benefits:

  • The Mongodump command is an efficient backup utility of MongoDB and allows you to create data backups of all sizes and varieties. For instance, it lets you back up a query, collections, or even an entire database.
  • The Mongodump command also generates a periodic snapshot of your data if the oplog is included. This straightforward method provides backups that you can filter to match your needs. This way, MongoDB addresses both the Time and Point Recovery objects.
  • The Mongodump command works well for small-scale database backups. Moreover, it’s oplog.bson file creates a backup created which will allow you to replayed the oplog data during the restoration process.

Conclusion

Overall, creating a MongoDB dump is a crucial aspect of database management, ensuring data safety and integrity. The mongodump utility simplifies the backup process, offering flexibility in selecting databases and collections for backup. With its ability to handle remote and secure MongoDB instances, mongodump provides a reliable solution for data backup needs.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads