In this data-driven business world, data and information are precious commodities. Companies that can protect their data from sudden server crashes and ensure high data availability, enjoy a certain advantage over their peers. This is why businesses are constantly seeking ways to Backup their data securely and have fast Data Restoration options at their disposal. 

MongoDB is a popular database that companies from numerous verticals prefer for data storage. Since businesses store sensitive information creating secure data backups using MongoDB is necessary. To make things easier, MongoDB provides the mongodump and mongorestore commands that can cater to your Data Backup and Restoration needs.

This article will guide you through a detailed approach using which you can create secure Data Backup for your MongoDB data via the mongodump command. Read along to learn the benefits of using Mongodump and how to restore data using the Mongorestore commands!

What is Mongodump in MongoDB?

mongodump is a MongoDB utility used to create backups of your databases and collections. Here’s a breakdown of its key features and important points:

Functionality:

  • Exports data from a MongoDB instance to a set of BSON files.
  • Can back up entire databases, specific collections, or even individual documents.
  • Supports various output formats, including JSON and BSON.

Key Concepts about mongodump

  • Data Consistency: For consistent backups, especially in a replica set environment, use the --oplog option to capture oplog entries along with the data dump.
  • Compression: Employ --gzip for efficient storage and faster transfer of backups.
  • Authentication: If your MongoDB instance requires authentication, use the --username and --password options to provide credentials.
  • Output Location: Specify the --out option to control where the backup files are stored.
  • Granularity: Control the scope of your backup using options like --db (for specific databases) and --collection (for specific collections).
Seamlessly Migrate data from MongoDB with Hevo!

Say goodbye to the hassle of manually connecting MongoDB. Embrace Hevo’s user-friendly, no-code platform to streamline your data migration effortlessly.

Choose Hevo to:

  • Access 150+(60 free sources) connectors, including MongoDB.
  • Ensure data accuracy with built-in data validation and error handling.
  • Eliminate the need for manual schema mapping with the auto-mapping feature.

Don’t just take our word for it—try Hevo and discover how Hevo has helped industry leaders like Whatfix connect Redshift seamlessly and why they say,” We’re extremely happy to have Hevo on our side.” 

Get Started with Hevo for Free

Ways to Backup & Restore Data using Mongodump

You can implement the Mongodump database command in the following ways to generate secure Data Backups for your MongoDB instances:

Case 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: Backup Initialization

Case 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

It will generate the following output:

mongodump: Remote Instance
Integrate MongoDB to BigQuery
Integrate MongoDB to Databricks
Integrate MongoDB Atlas to PostgreSQL

Case 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"

It will generate the following output:

mongodump: Secure Remote Instance

Case 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]

Case 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

It will provide you with the following output:

mongodump: Authentication

Case 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

The output on your screen will be similar to the below image.

Mongodump: Archive file

Case 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

The output on your screen will be as follows:

Mongodump: Compression of Data Backup

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

Learn more about the use of Mongorestore here. , visit our blog.

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.

Limitations of Using mongodump for Creating Backups

  • No Point-in-Time Recovery: mongodump only captures a snapshot of the data at the time of the dump, making it impossible to recover data to a specific point in time (unlike oplog-based backups).
  • Limited Scalability: It may struggle with very large databases or collections because it dumps data into BSON files, which can result in long processing times for both backups and restores.
  • Locking Issues: During the backup process, mongodump may cause performance issues, especially in older versions of MongoDB, as it can impact write operations in large or busy databases.
  • No Backup for Indexes: mongodump does not back up indexes by default. After restoring from a mongodump, MongoDB will need to recreate the indexes, which can be time-consuming.
  • Error-Prone: Running so many long commands can be time consuming and might lead to various errors.

To overcome these limitations, use Hevo’s no-code automated pipeline for seamlessly migrating your MongoDB data to any location.

Alternatives for Data Backup in MongoDB

MongoDB has other built-in options for data backup in the case of data loss events. The three alternatives to mongodump for data backup are described below.

1. MongoDB Atlas

MongoDB Atlas is the hosted MongoDB built-in backup option that supports two fully-managed methods for data backup:

  • Cloud Backups: It utilizes the inherent snapshot functionality of the cloud service provider in the deployment to deliver resilient backup solutions. These backups offer:
    • On-demand Snapshots: it allows the user to capture the immediate snapshot of their deployment at a given point in time.
    • Continuous Cloud Backups: This allows the user to schedule periodic backups of their deployment.
  • Legacy Backups: It takes incremental backup of your data in your deployment.

2. MongoDB Cloud Manager & Ops Manager

MongoDB Cloud Manager serves as a hosted backup, monitoring, and automation service designed for MongoDB. It facilitates the backup and restoration of MongoDB replica sets and sharded clusters through an intuitive graphical user interface.

  • MongoDB Cloud Manager: The MongoDB Cloud Manager facilitates the backup and restoration of MongoDB deployments, consistently performing backups for replica sets and sharded clusters by reading oplog data. It generates periodic snapshots and supports point-in-time recovery for both replica sets and sharded clusters.
  • MongoDB Ops Manager: Ops Manager empowers MongoDB subscribers to deploy the core software driving MongoDB Cloud Manager on their infrastructure. It offers on-premise solutions akin to MongoDB Cloud Manager and is accessible through Enterprise Advanced subscriptions.

3. File system backups

To back up a MongoDB deployment, you can duplicate MongoDB’s underlying data files. Utilizing point-in-time snapshots on the volume storing MongoDB data allows creating precise backups. File system snapshots, an OS volume manager feature, serve as a baseline for data backup. Snapshot mechanisms vary based on the storage system, such as Linux’s Logical Volume Manager (LVM) or Amazon’s EBS for EC2.

For an accurate snapshot of a running mongod process, enable journaling, ensuring the journal resides on the same logical volume as other MongoDB data files. Without journaling, snapshot consistency isn’t guaranteed.

To ensure consistency in a sharded cluster snapshot, disable the balancer and capture snapshots from each shard and a config server simultaneously.

Conclusion

This article explained the Mongodump command and how it operates to create data backups. It further elaborated on an 8-step method using which you can easily create your MongoDB backup Database. Furthermore, the blog listed down the benefits of implementing the Mongodump data backup process.

Read through these essentials for a deeper insight into MongoDB:

Visit our Website to Explore Hevo

MongoDB is a great tool for storing your business data. However, at times, you need to transfer this data to a Data Warehouse for further analysis. Building an in-house solution for this process could be an expensive and time-consuming task Hevo Data, on the other hand, offers a No-code Data Pipeline that can automate your data transfer process, hence allowing you to focus on other aspects of your business like Analytics, Customer Management, etc. This platform allows you to transfer data from sources like MongoDB to Cloud-based Data Warehouses like Snowflake, Google BigQuery, Amazon Redshift, etc. It will provide you with a hassle-free experience and make your work life much easier.

FAQ on Back Up And Restore MongoDB

How do I start MongoDB backup?

1. Open Terminal
2. Run mongodump command
3. Schedule your backups

Where are MongoDB backups stored?

1. Local backup using mongodump is stored in the directory specified
2. Cloud backup is stored in MongoDB Atlas Cloud Storage

What are the different types of backups in MongoDB?

1. Logical Backup
2. Physical Backup
3. Snapshot Backup
4. Cloud Backup

Abhinav Chola
Research Analyst, Hevo Data

Abhinav Chola, a data science enthusiast, is dedicated to empowering data practitioners. After completing his Master’s degree in Computer Science from NITJ, he joined Hevo as a Research Analyst and works towards solving real-world challenges in data integration and infrastructure. His research skills and ability to explain complex technical concepts allow him to analyze complex data sets, identify trends, and translate his insights into clear and engaging articles.