Learn How to Backup and Data Restore features in MongoDB

0
4298
Data Restore

Data Restore

In this chapter, we are going to learn about the data backup and data restore features of MongoDB. In today’s world where almost all the businesses have become online, it is very crucial to protect the business data against any loss or damage in case of disaster or unwelcomed outages. Therefore, irrespective of the type of the database that an application operates on, it is mandatory to take and retain the data backup regularly in order to make data available during data server crash or data corruption due to virus attack. Data backup is one of the prerequisite that every small to big organization prefer to have during their application operation in order to counter the threat of cyberattacks and prevent loss of business data due to corruption.

Data Backup feature in MongoDB
In MongoDB, it is quite simple to take a data backup and it gets triggered just by firing a mongodump command on the command line. When this command is executed, the system will dump the entire data present on the server into the dump directory of MongoDB. It also provides the number of options through which we can limit the amount of data or create data backup of our remote server.

The following is the basic syntax to take the date backup in MongoDB.

Let’s understand it with the help of the following example.

Step 1: – Start a MongoDB session specifying the dbpath as shown below. It will wait for the MongoDB client to make connections on the port 27017.

Step 2: – Open another command line window and execute ‘mongo.exe’ command that will connect the client at port 27017 as shown below.

Step 3: – Create a database ‘application_db’ and store few documents or record inside it as shown above.
Step 4: – Check how many databases exist on the server by using ‘show dbs’ command as shown below. There are two databases i.e. local and ‘application_db’ which we have just created.

Step 5: – Let’s trigger the data backup after executing ‘mongodump’ command as shown below. Here, we have to open a new command prompt window and navigate to the bin directory (here C:\Program Files\MongoDB\Server\3.2\bin) of the MongoDB. Execute ‘mongodump’ command to complete the backup of the ‘application_db’.

Capture

Here, we can observe the two documents or records which we created in step 3, has been backed up successfully, under the dump directory as shown below.
Capture2

The following is a list of available options which could be used along the mongodump command on the command line window.

S No.

Syntax

Description

Example

1.

mongodump

–host HOST_NAME

–port PORT_NUMBER

This command will take the backup of all the databases which are specified in the current mongodb instance.

mongodump

–host APARAJITA-PC

–port 27017

2.

mongodump

–dbpath DB_PATH

–out BACKUP_DIRECTORY

This command will take the backup of the database present at the specified path.

mongodump

–dbpath “/mongodb_data/db/” –out “/mongodb_data/backup/”

3.

mongodump

–collection COLLECTION

–db DB_NAME

This command will take the backup of the specified collection of a specified database.

mongodump

–collection mycollection

–db test

Data Restore feature in MongoDB
In MongoDB, it is quite simple to restore data and it gets triggered just by firing a mongorestore command on the command line. When this command is executed, the system will restore all of the data from the backup directory. The following is the basic syntax to take the date backup in MongoDB.

Let’s understand it with the help of the following example.

Step 1: – Firstly, we will drop the database by executing ‘db.dropDatabase ()’ command as shown below. This command will drop the database ‘application_db’ which we had created before.

Step 2: – Next, we will restore the ‘application_db’ database from the backup or dump by executing ‘mongorestore’ command as shown below. Here, we have to open a new command prompt window and navigate to the bin directory (here C:\Program Files\MongoDB\Server\3.2\bin) of the MongoDB. Now, execute ‘mongorestore’ command to complete the data restoration of the ‘application_db’.

Step 3: – Check how many databases exist on the server by using ‘show dbs’ command as shown below. There are two databases i.e. local and ‘application_db’ (which we have just restored from backup).

Conclusion: –
In this chapter, we have learnt about the data backup and data restore features of the MongoDB with the help of suitable examples.

LEAVE A REPLY

Please enter your comment!
Please enter your name here