Wednesday, May 13, 2020

Data Backup and Restoration

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.

MongoDB: Taking 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.

  1. From the above screenshot observer that all the data of the mongodb database will get backup.

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.

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.

MongoDB: Restoring Data from Backup

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.

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

Backup and restore Selected Database

To dump and restore selected database, use following commands:

C:\mongodb\bin>mongodump --db studentdb

This will backup only the selected database studentdb instead of backup the entire MongoDB databases.

C:\mongodb\bin>mongorestore --db studentdb dump/studentdb

This will restore only the selected database studentdb instead of restoring the entire MongoDB databases.

Backup and restore Selected Collection of a Database

To backup and restore selected database’s Collection, use following commands:

C:\mongodb\bin>mongodump --db studentdb --collection address

This will backup only the selected collection of a database address instead of backup the entire studentdb database.

C:\mongodb\bin>mongorestore --db studentdb --collection address 

dump/studentdb/address.bson

This will restore only the selected collection of a database address instead of restore the entire studentdb database.