MongoDB Backup and Restore Tutorial

In this MongoDB tutorial we learn one of the most important things in database management, backup and restore.

We cover how to install and use the MongoDB database tools to backup and restore a database.

What is data backup

A backup is simply a copy of data from the database, or the whole database. In the event that data has been lost, we can simply restore the copy.

Data backup is a vital and best practice process for any database management system.

MongoDB database tools

These tools are released as a separate package, so you will need to download and install them as well.

  1. Go to the official download page , choose a package for your system and download the installer.
  2. Once the file has downloaded, run the installer and follow the steps to install the database tools.
MongoDB Optional Tools Download

After the installation, restart your ‘mongod’ server.

If you are having trouble installing the database, please see the official Database Tools installation documentation .

How to create a database backup

To create a binary backup (dump) of the database contents, we use the mongodump utility.

This utility can export data from either the mongod or mongos instances.

As an example, let’s create a small collection with two documents that we’ll backup.

Example:
db.users.insert([
    {
        "name": "John",
        "email": "john@example.com"
    },
    {
        "name": "Jane",
        "email": "jane@example.com"
    }
])

There is no syntax for mongodump because it’s a utility, not a shell command.

Open a new terminal or command prompt and navigate to the tools directory located at /MongoDB/Tools/100/bin/.

Example:
cd C:\Program Files\MongoDB\Tools\100\bin

Once inside the ‘bin’ directory, we can run the mongodump utility.

Example:
mongodump

This will run the utility and start the backup process.

Output:
writing admin.system.version to dump\admin\system.version.bson
done dumping admin.system.version (1 document)
writing test.users to dump\test\users.bson
done dumping test.users (2 documents)

Your output may look different than what we have above, but look at the last 3 lines.

It shows the collections that were backed up and how many documents they contain.

The backup is stored with the tools at /MongoDB/Tools/100/bin/dump/. We can confirm the files were backed up by going to the ‘dump’ directory.

Each collection will be backed up in its own folder. The folder contents should look similar to the following.

Example:
collection.bson
collection.metadata.json

It’s also possible for us to refine our backup command with flags. Let’s see some popular flags.

FlagDescription
--host=koderhq.comSpecify a host name
--host=koderhq.com:27017Optionally, we can specify a port number as well
--host=koderhq.com --port=27017Specify a port number
--db=myDBSpecify a database name to backup
--collection=usersSpecify a collection name to backup
--/data/backup/Specify an output directory for the dump
--gzipCreates a gzipped backup file
--archive=test.db.archiveCreates an archived backup file
--archiveThe name can be omitted

How to restore a database backup

To restore a backup, we can use the mongorestore utility tool, which is used in the same manner as the mongodump utility.

By default, mongorestore will restore the contents of the _dump_ directory.

Before we restore the backup data, we will open a new mongo process and delete some collections in our database so we can demonstrate.

Example:
// cd into directory
cd C:\Program Files\MongoDB\Server\4.4\bin

// run the database process
mongo

// delete 'users' collection
db.users.drop()

Now let’s restore the previously exported backup.

Example:
mongorestore
Output:
using default 'dump' directory
preparing collections to restore from
reading metadata for test.users from dump\test\users.metadata.json
restoring test.users from dump\test\users.bson
finished restoring test.users (2 documents, 0 failures)
2 document(s) restored successfully. 0 document(s) failed to restore.