Data Backup and Restoration in MongoDB

Welcome to a tutorial on Data Backup and Restoration in MongoDB.

Data backup is an important and highly required process for any database management system. Its core reason is that is difficult to anticipate how and when the data can be lost. This is ideal that anytime a database is set up, there is a need to ensure that has a provision for that data backup in case of a data loss.

A backup is the copy of data from the database that helps in reusing the database in case of any catastrophic event that happens to our data.

 

MongoDB: Taking Data Backup

In MongoDB, to perform a data backup process, the command mongodumpis used. This command simply dumps all the data stored into a dump directory of the mongodb. In addition, the command helps to back up the data from the remote servers.

 

Follow the steps below to properly perform data backup.

Step 1: Start the mongoDB server with the command mongod

Step 2: Start the mongoDB client with the command mongo in a new command prompt.

Step 3: Switch to the required collection and then run the command mongodump. But, the mongodump is not commanded from the mongodb shell, so you have to execute the command as shown below.

 

Step 4: From the above example, you will notice that all the data from the collection mynewdatabase has been dumped.

However, Mongodump reads the data from the database and creates a BSON file in which the data is dumped. Also, Mongodump writes only documents from the database. Therefore, the resultant backup of the data will be the space efficiency. Thus, the backup of the data will be stored under the mongodb's bindump folder.

 

But, there is a disadvantage of making use of mongodump which will have some performance impact when the data of a collection is larger than the available system memory.

 

MongoDB: Restoring Data from Backup

Data backup is typically used to reconstruct the data in the event of a loss. However, MongoDB helps to restore the backup data through one of its utility tools called mongorestore, a command.

Check out the screenshot which shows how the backup data is restored by making use of the command mongorestore in MongoDB.

 

The example above, shows that the dumped collection data has been restored successfully.