MongoDB - Day 17 (Backup And Restore)

Before reading this article, I highly recommend reading the following previous parts of the series:

In this article we will describe the process of creating and restoring the database using mongodump and mongorestore command. Besides these two methods, MongoDB provides various ways to backup and restore the database, some methods are given below:

  • Backup by copying underlying data files.
  • Backup using mongodump tool.
  • MongoDB Cloud manager backup.
  • Ops Manager Backup Software.

Today we will discuss backup using Mongodump tool.

Mongodump Tool

The mongodump tool reads data from a MongoDB database and creates high fidelity BSON files. The mongorestore tool can populate a MongoDB database with the data from these BSON files. The Mongodump and mongorestore utilities works for BSON data dumps. Mongodump and mongorestore tools provide efficient backup for small MongoDB deployments, but not efficient for backup of large system. Mongodump doesn’t capture content of a database, instead mongodump captures documents of database. During backup process, mongodump reduces the performance of mongod. Not only mongodump create a traffic for running database instance but also force the database to read all data through memory.

During Backup or Restore process consider the following guidelines.

  • Make sure that backup and restore contains a consistence data state. Sometimes backup and restore impact the data integrity or consistency if an update process running during the backup process.

  • Don’t import or export data if backup or restore process generates an adverse effect on database or production system.

  • Use a reliable file name for backup database so that we can easily identity the content of backup at the time of importing backup files.

  • Import or export database only when it is required because backup and restore process slow the mongod process.

Basic mongodump Operations

The mongodump utility takes the backup of current running mongod or mongos instance. Using mongodump utility we can take back entire server, database, collection or a specific part of a selected collections. We will discuss each scenario with examples.

The mongodump command without any arguments

When we run mongodump without any arguments, mongodump takes the backup of mongod instance that is connected to the local system(127.0.0.1) and port number 27017. This method stores the database backup in current directory with dump/ named.

Syntax

mongodump


Method

Step 1: Open a command prompt and run “mongod” command. This command runs a mongod instance on the localhost (127.0.0.1) and port number 27017.

Step 2: Open another command prompt and run “mongodump” command.

Example


                                                         Figure 1: Command Prompt

In above example we run a “mongodump” command, this command created the backup of all the database and stored in “C:\users\Pankaj-Choudhary” directory with “dump” named.


                                                               Figure 2: Create a Backup

In above image we can see that backup of all the databases has been created and store in “dump\Temp” directory. Here “Demo” and “Temp” are name of database.

Run Mongodump for a Specific Port

If we don’t define the port number in mongodump command, it connects to the MongoDB instance on the local host or 127.0.0.0 , port number 27017 and create the backup in /dump directory. But we can specify the hostname and port number in mongodump command using --host and --port parameters.

Syntax

mongodump --host hostname –-port port_number

Example

  1. Mongodump –-host Pankaj --port 12345  

                                                                  Figure 3: Mongodump

In above example we connect the mongodump command with a MongoDB instance, whose hostname is “Pankaj” running on port number 12345.

Specify the Output Directory

The mongodump command by default create a dump directory and store backup files in this directory. But we can change the directory path to store the backup files at a specific location using “out” or “o” parameters.

Syntax

mongodump --out directory_path

Example
  1. Mongodump -- out \Datad\Backup\ 

                                                         Figure 4: Output the Directory

In above example we create the backup of MongoDB instance and store files in “Datad\Backup” directory. We can see this directory in the following image.


                              Figure 5: Create a Backup of MongoDB

Backup of Specific Database

We can take backup of specific database using “db” parameter. If we don’t use the “db” parameter then mongodump command takes the backup of all the databases.

Syntax

mongodump --db DataBase_Name

Example
  1. mongodump --db Temp  

                                                      Figure 6: Backup of Specific Database

In above example we took the backup of “Temp” database.

Backup of Specific Collection

In previous method we learned how to take backup of a specific database. Such that we can also take backup of a specific collection within a database.

Syntax

mongodump --collection Collection_Name --db DataBase_Name

Example
  1. mongodump --collection order_details --db Temp
Output


                                                      Figure 7: Output Collection


                                                   Figure 8: Backup of Specific Collection

In above example we took backup of “order_details” collection of “Temp” database.

Restore the Backup Data

The mongorestore command is used to restore the backup data. This command restores all the data from backup directory. The mongorestore utility restores data by connecting to a running mongod or mongos instance directly. Using mongorestore command we can restore either complete backup or a partition of backup.

Now we consider some different ways to restore the backup.

Restore all Backup

We can restore the backup data by using simple “mongorestore” command without any parameters. But remember that this command restores the backup from default directory “dump”. If dump directory does not exist then it will throw an error.

Syntax


mongorestore

Example


                                                         Figure 9: Restore all Backup

Drop parameter

The drop parameter is used to drop all collection from target database before storing the collections from the dumped database.

Syntax

mongorestore --drop

Example


                                                                     Figure 10: Drop Parameter

Above command first removes all collection from database that are present in backup files and after that stores that collections.

Restore Specific Database

The mongorestore command restores all the databases that are present in backup files. But we can restore a specific database using “db” parameter.

Syntax

mongorestore --db DataBase_Name Path

Example
  1. mongorestore --db Demo dump/Demo  

                                                   Figure 11: Restore Specific Database

In above example we restored the “Demo” database.

Restore Specific Collection

In previous method we have shown how to restore a specific database such that we can restore a specific collection using “collection” parameter.

Syntax

mongorestore --collection Collection_Name --db DataBase_Name Path

Example
  1. mongorestore --collection Employee --db Demo dump/Demo/Employee.BSON  

                                                Figure 12: Restore Specific Database2

In above example we restored the “Employee” collection of “Demo” database.

Today we learnt how we can take backup of data and restore this data. Backup and restoring data generates a high traffic and makes server busy for that particular time. But taking backup of data is a good approach. It protects us from data loss. It is a good approach to take the backup of data after a certain time period.
 
Thanks for reading this article.