How to Perform Backup and Restore in MongoDB 399
Introduction
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.
Prerequisites
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 mongodump
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:
mongodump <options>
.
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:
1 | sudo mongodump --db warlordDatabase_062019 --out /var/backups/mongoDB_backups/`date +"%m-%d-%y"` |
After executing the command, you would see something like this in the terminal:
1 2 | 2019-06-04T19:03:10.844+0800 writing warlordDatabase.warlordCollection to 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 mongodump
.
The MongoDB mongorestore command syntax should look like the following:
1 | sudo mongorestore --db warlordDatabase --drop /var/backups/mongoDB_backups/06-04-19/warlordDatabase/ |
After performing the MongoDB restore, you should see something like this in your terminal:
1 2 3 4 5 6 7 | 2019-06-04T20:19:52.593+0800 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead 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) 2019-06-04T20:19:52.660+0800 done |
NOTE: additional options available for the mongorestore
command are --drop
which specifies the name of the database to be restored.
The 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.
Conclusion
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.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started