MongoDB Backup And Restore

MongoDB provides the utilities to take backup and perform the restore operations. To take backup MongoDB provides the mongodump command that takes backup of complete data of the server, Database, or the Collection into the Dump directory. MongoDB provides the mongorestore command to restore complete data from the backup directory.

The following are the tools MongoDB provides for backup and restore operation.

  • mongodump
  • mongorestore

Now let us see each MongoDB tool in detail.


mongodump MongoDB Backup Tool

MongoDB uses the mongodump command to take backup of complete data.

Syntax:

The following is the syntax of mongodump command.

mongodump

Example:

Now let's back up the complete MongoDB data using the mongodump command. Start the MongoDB server with the following command and run the mongodump command to start back up as mentioned below.

cloudduggu@ubuntu:~$ sudo systemctl start mongod
cloudduggu@ubuntu:~$ sudo systemctl status mongod
cloudduggu@ubuntu:~$ mongodump

Output:

We can see from the below output that the MongoDB backup has been taken successfully.

Mongodb mongodump operation


mongorestore MongoDB Restore Tool

MongoDB provides the mongorestore command to perform the restore operation.

Syntax:

The following is the syntax of mongorestore command.

mongorestore

Example:

We have taken the complete data backup in the backup section now in this section we will see the restore operation. Suppose the database "mongodb_test" that we have already created in the "database section" got dropped and we have to restore the complete data or to test this condition we can drop this database "mongodb_test" manually and try to restore it from the backup using the below command.

> use mongodb_test
> db.dropDatabase()
> show dbs

Output:

Mongodb drop database

Now let's restore the complete data using the MongoDB mongorestore command.

cloudduggu@ubuntu:~$ sudo systemctl start mongod
cloudduggu@ubuntu:~$ sudo systemctl status mongod
cloudduggu@ubuntu:~$ mongorestore

Output:

We can see from the below output that MongoDB restores operation is completed successfully.

Mongodb restore operation

Mongodb restore operation output