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 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.
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
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".
In the below example, we are backing up data of the collection name company inside the codingninjas database to a folder name CN.
Output
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
Restoration of a Specific Collection
In MongoDB, to restore a specific collection of databases from a specific directory, run the following command.
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
Now, we will drop collection “company” using the below command.
db.company.drop()
Output
Lastly, we will restore the collection "company" by using the following command.
mongorestore –db codingninjas –collection company c:\CNbackup\codingningjas\company.bson
Output
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:
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 problems, interview experiences, and interview bundles.