How to Perform Backup and Restore in MongoDB 399
It’s no surprise that MongoDB is one of the most widely used database systems– not only is it powerful, scalable and reliable, but it’s also easy to manage. If you’re using MongoDB to store and manage your data, it’s important to know how to protect your database and data in the case of a disaster or outage. In this article, we’ll explain how to backup a MongoDB database and also how to restore a database from a backup.
Before we move on to the instructions, let’s take a moment to review the prerequisites for this task. There are a few system requirements to keep in mind:
First, you should ensure that the MongoDB service is running.
You’ll need to have a version of MongoDB Compass installed. In this tutorial, we’ll be using Compass Community Edition.
NOTE: Throughout this article, we assume that the version of MongoDB Compass Community being used is 1.18.0.
Backing Up MongoDB Database using
Let’s begin by discussing a simple way to create backups in MongoDB.
MongoDB uses a command called
mongodump to dump data into a specific directory. You can use the MongoDB mongodump command to create a backup of a specific database, collection or even an entire server. This command can be used with options to provide a more controlled backup process.
The syntax is:
Let’s look at an example where we want to backup a database called warlordDatabase, and we want to store it in a certain backup directory. The naming conventions you use for your backups should be descriptive and specific, so that you can quickly glance at it and understand what the backup represents.
In this example, the backup command would look like the following:
After executing the command, you would see something like this in the terminal:
2019-06-04T19:03:10.845+0800 done dumping warlordDatabase.warlordCollection (6 documents)
NOTE: Some of the additional options available with the
mongodump command are
--db, which specifies the database name,
--out, which specifies the output directory where the data should be dumped, and
date +"%m-%d-%y" which grabs the current date and displays it in the specified format.
Restoring a MongoDB Database
Once you’ve backed up your MongoDB database, you can then use that backup to restore it. When you restore a MongoDB database from a previous backup, you’re bringing back an exact copy of the MongoDB data taken from a specific time, including indexes, data types and all associated data.
To restore a MongoDB backup, use the
mongorestore command and supply the location of a binary backup produced by
The MongoDB mongorestore command syntax should look like the following:
After performing the MongoDB restore, you should see something like this in your terminal:
2019-06-04T20:19:52.593+0800 building a list of collections to restore from /var/backups/mongoDB_backups/06-04-19/warlordDatabase dir
2019-06-04T20:19:52.621+0800 reading metadata for warlordDatabase.warlordCollection from /var/backups/mongoDB_backups/06-04-19/warlordDatabase/warlordCollection.metadata.json
2019-06-04T20:19:52.655+0800 restoring warlordDatabase.warlordCollection from /var/backups/mongoDB_backups/06-04-19/warlordDatabase/warlordCollection.bson
2019-06-04T20:19:52.660+0800 no indexes to restore
2019-06-04T20:19:52.660+0800 finished restoring warlordDatabase.warlordCollection (6 documents)
NOTE: additional options available for the
mongorestore command are
--drop which specifies the name of the database to be restored.
mongorestore command will restore the data on the server where the backup was created. To migrate the data to another server, simply copy the backup directory to that machine, then perform the restore process.
If you’re storing data in MongoDB, it’s essential to know how to backup a MongoDB database. Fortunately, it’s easy to take the steps needed to safeguard your data in the event of an outage or a disaster. With the step-by-step instructions included in this tutorial, you’ll know how to perform a backup and restore your database if needed.