Table of contents
1.
Introduction
2.
MongoDump
2.1.
Basic Syntax
3.
Data Backup using MongoDump 
3.1.
Backup Without Argument
3.1.1.
Command 
3.1.2.
Output
3.2.
Backup With Argument
3.2.1.
Command
4.
Backup a Database at a Specific Output Directory
4.1.
Command
4.2.
Output
5.
Backup of a Specific Collection of Database
5.1.
Command 
5.2.
Example
5.2.1.
Output
6.
MongoRestore
7.
Restoration of all Database
7.1.
Command
7.1.1.
Output
8.
Restoration of a Specific Collection
8.1.
Output
8.2.
Output
8.3.
Output
9.
Frequently Asked Questions
9.1.
What are the different methods available for backing up MongoDB?
9.2.
What factors should be taken into account during the restoration process?
9.3.
How often should MongoDB backups be performed?
10.
Conclusion
Last Updated: Mar 27, 2024
Medium

MongoDB Backup and Restoration

Author Ayush Mishra
0 upvote

Introduction

MongoDB is one of the most popular unstructured databases that store data in JSON-like format. Data backup and restoration are necessary to prevent data loss and guarantee business continuity.

MongoDB Backup and Restoration

MongoDB provides tools for data backup and restoration of backup data. In this blog, we will discuss important tools and commands in MongoDB Backup and Restoration process.

MongoDump

In MongoDB, data backup is done using the built-in Mongodump tool. This tool creates a logical backup of the entire database and generates a BSON file containing indexes and data. Due to its slower speed, it is suitable for small databases. By default, data is stored in the bin file of the MongoDB folder.

Basic Syntax

The basic syntax for the mongodump command is:-

mongodump --host [host-name] --username [user-name] --password [password] 
--port [port-number] --db [database-name] --out [backup-directory]


A description of each option in the above syntax is as follows:-

  • --host: Indicate the database server's hostname or IP address.
     
  • --username: Enter the username for the database.
     
  • --password: Enter the database user's password here.
     
  • --port: Specify the MongoDB instance's port number.
     
  • --db: It indicates the name of the database you want to backup.
     
  • --out: It indicates where the backup path is located.

Data Backup using MongoDump 

Data Backup can be done using two ways:

Backup Without Argument

In this method, you did not need to specify any argument to back up the data using the mongodump command. A mongodump will connect to the local MongoDB instance on the default port no. (27017), and it will create a backup of all the databases in the current directory.

Command 

The command to restore the data without argument is:-

mongodump


Output

mongodump

Backup With Argument

In this method, you need to specify an argument to backup the data using the mongodump command to customize the backup process. This argument includes "--db", "--out", "--port", and "--collection".

Command

mongodump --db databaseName --collection collectionName --out c:\backup


Now in the context of MongoDB Backup and Restoration, we will see the commands to backup the database using mongodump with an argument.

Backup a Database at a Specific Output Directory

To back up all the data from MongoDB Database at a specific output directory, you can use the below command.

Command

mongodump --out c:\backupDatabase


Output

mongodump --db databaseName --out c:\backup

Backup of a Specific Collection of Database

To backup a specific collection in the database in a specific directory, run the following command.

Command 

mongodump --db databaseName –collection name-of-collection --out c:\backup

Example

In the below example, we are backing up data of the collection name company inside the codingninjas database to a folder name CN.

Output

mongodump --db databaseName –collection name-of-collection --out c:\backup

MongoRestore

In MongoDB backup and restoration process, mongorestore is a command line tool used to restore the data from the database backup created using the mongodump tool. It allows the user to import a BSON file into a MongoDB instance, restoring the data to its original location.

It connects to the MongoDB servers and restores the data from the specific backup directory. The backup directory must be the directory containing the files backup by mongodump. By default, mongorestore searches for data in the MongoDB bin folder.

Restoration of all Database

In MongoDB, to restore all the backup databases created by mongodump, run the following command.

Command

mongorestore dump


Output

mongorestore dump

Restoration of a Specific Collection

In MongoDB, to restore a specific collection of databases from a specific directory, run the following command.

mongorestore  –db databaseName –collection collectionName directory\collectionName.bson


Let us understand this restoration from an example. In the last section of mongodump, we have backup a collection named company from the codingninjas database at a specific directory named CN. See the following output to understand it.

Output

mongorestore  –db databaseName –collection collectionName directory\collectionName.bson

 

Now, we will drop collection “company” using the below command.

db.company.drop()


Output

db.company.drop()

 

Lastly, we will restore the collection "company" by using the following command.

mongorestore –db codingninjas –collection company c:\CNbackup\codingningjas\company.bson


Output

mongorestore –db codingninjas –collection company c:\CNbackup\codingningjas\company.bson

Frequently Asked Questions

What are the different methods available for backing up MongoDB?

MongoDB Database has many methods available for backing up the data. The different methods available for backing up MongoDB database are Filesystem Snapshots, Mongodump tools, and MongoDB Cloud Manager.

What factors should be taken into account during the restoration process?

Some factors such as Backup Validation, Compatibility, Resource Allocation, and Testing are always kept in mind and taken into account to restore their MongoDB data effectively and for anytime recovery process.

How often should MongoDB backups be performed?

The frequency of MongoDB backup preformation depends on factors such as the importance of the data, data change rate, and the recovery point objective. To minimize data loss, regular backups are necessary.

Conclusion

MongoDB Backup and Restoration process uses mongodump and mongorestore allow to create a backup of data and restore it when needed, so preventing data loss and data from corrupting.

Do not stop learning! We recommend you read some articles related to MongoDB backup and Restoration: 

1. Introduction to MongoDB

2. MongoDB Interview Questions

3. MongoDB Tutorial


Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. For placement preparations, you must look at the problemsinterview experiences, and interview bundles.

Happy Learning!!

Live masterclass