Data Backup and Restoration in MongoDB

Data backup is one of the vital and highly required process for any database management system. The primary reason being is that it is difficult to anticipate how and when the data can be lost. So it is an ideal and best practice that whenever a database is setup, we need to ensure that it has a provision for the data backup in case of any loss events happens.

A backup is nothing but the copy of data from the database which helps in reusing the database in case of any catastrophic event happens to the data.


Data Backup

In order to perform a data backup process in mongodb, the command mongodump should be used. This command will simply dump all the data stored into a dump directory of the mongodb. It also helps to backup the data from the remote servers as well.

In order to properly perform the data backup, follow the below mentioned instructions:

  1. Start the mongodb server with the command mongod
  2. Start the mongodb client with the command mongo in a new command prompt.
  3. Switch to the required collection and run the command mongodump. Since the mongodump is not actually command from mongodb shell, you need to execute the command as shown below.

    Mongodump in mongodb

  4. From the above screenshot observer that all the data from the collection mynewdatabase has been dumped.

Mongodump basically reads the data from the database and creates a BSON file in which the data is dumped. Mongodump writes only documents from the database. The resultant backup of the data will be of the space efficient. The backup of the data will be stored under the mongodb's bin\dump folder.

Mongodump in mongodb

Also, there is one disadvantage of using mongodump which will have some performance impact when the data of a collection is huge than the available system memory.


Data Restore

Now let us learn how to restore the backup data in mongodb. Data backup is basically used to reconstruct the data in case of a loss event. MongoDB helps to restore the backup data through its one of the utility tools called mongorestore which in turn is a command as well.

The below screenshot shows how the backup data is restored using the command mongorestore in MongoDB.

DB restore in mongodb

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