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!

Introduction to Mongodump for Backing Up Data

MongoDB uses a tool called mongodump to provide you with faster data backups inside MongoDB databases. This tool contains commands which will enable you to backup your data at the level of queries, collections, or even an entire database. Moreover, MongoDB offers a complementary tool called mongorestore that allows you to recover your data from the distributed backups into a new or existing MongoDb database.

The MongoDB mongodump is a simple approach that empowers you to create data backups by filtering the specific data to suit your needs. However, Mongodump is efficient only for small deployments, and its overhead complexity will increase significantly if applied to larger systems. This is because the Mongodump command was not designed to be a scalable option. Since it is not an incremental technique, you must deal with a complete data dump for each snapshot point. This makes Mongodump a highly resource-intensive option. So, it is better to implement MongoDB mongodump for backing up small-scale databases, and for large data collections, you can use other facilities provided by MongoDB.

Steps to Backup & Restore Data using Mongodump

The following steps will allow you to implement the Mongodump database command to generate secure Data Backups for your MongoDB instances:

Simplify your Data Analysis with Hevo’s No-code Data Pipeline

Hevo is the only real-time ELT No-code Data Pipeline platform that cost-effectively automates data pipelines that are flexible to your needs. With integration with 150+ Data Sources (40+ free sources) such as MongoDB, we help you not only export data from sources & load data to the destinations but also transform & enrich your data, & make it analysis-ready.

Start for free now!

Get Started with Hevo for Free

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: Backup Initialization
Image Source

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

It will generate the following output:

mongodump: Remote Instance
Image Source

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"

It will generate the following output:

mongodump: Secure Remote Instance
Image Source

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

It will provide you with the following output:

mongodump: Authentication
Image Source

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

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

Mongodump: Archive file
Image Source

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

The output on your screen will be as follows:

Mongodump: Compression of Data Backup
Image Source

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.

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.

mongodump Actions

  • Starting with MongoDB 4.4, mongodump is now independently released with its own versioning (starting at 100.0.0), separate from the MongoDB Server. Previously, mongodump was released in conjunction with the MongoDB Server and shared matching version numbers.
  • mongodump allows exporting data from various MongoDB setups, including standalone deployments, replica sets, sharded clusters, and serverless instances.
  • By default, mongodump employs the read preference primary. To customize this default behavior, you can specify the read preference using the --readPreference command-line option or within the --uri connection string.
  • mongodump excludes the local database content from its output. The output includes documents in the database and their index definitions but omits the index data.
  • mongodump utilizes the Extended JSON v2.0 (Canonical) format for its metadata files. To parse and restore these files, use mongorestore, which supports Extended JSON v2.0 in either Canonical or Relaxed mode.
  • mongodump will overwrite output files in the backup data folder if they already exist. Prior to executing the mongodump command iteratively, ensure you either no longer require the files in the output folder (default is the dump/ folder) or rename the folders/files accordingly.
  • mongodump fails under the following conditions:
    • If mongodump is initiated during a resharding operation.
    • If the reshardCollection command is executed while a mongodump operation is in progress.
  • When executed on a mongod instance utilizing the WiredTiger storage engine, mongodump produces uncompressed data.
  • mongodump may impact the performance of the mongod instance, particularly when dealing with data larger than system memory, as it can displace the working set from memory.
  • To execute mongodump on a MongoDB deployment with access control enabled, ensure you possess privileges granting the find action for each database to be backed up. The built-in backup role provides the necessary privileges for backing up any databases.
  • For standalone or replica set deployments, mongodump can be included in a backup strategy alongside mongorestore for partial backups using a query, synchronization between production and staging or development environments, or transitioning the storage engine for a standalone instance.
  • For 4.2+ sharded clusters with ongoing sharded transactions mongodump and mongorestore cannot be integrated into a backup strategy. Backups generated with mongodump do not preserve the atomicity guarantees of transactions across shards.

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.

Image Source

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.

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.

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.

Want to take Hevo for a spin? Sign Up for a 14-day free trial and experience the feature-rich Hevo suite first hand. Look at our unbeatable pricing to select the right plan for you!

Share your views on MongoDB’s backup using the Mongodump command in the comments section!

mm
Former Research Analyst, Hevo Data

Abhinav is a data science enthusiast who loves data analysis and writing technical content. He has authored numerous articles covering a wide array of subjects in data integration and infrastructure.

No Code Data Pipeline For Your Data Warehouse