Lesson 3: Restore Tools / Learn

Video Transcript (English)

Restoring data to a deployment is an important task for database administrators. Mongorestore, a command-line tool, can be used as part of a recovery strategy.

In this video, you'll learn about mongorestore, its syntax, its options, and how to use it.

The mongorestore utility can be used to load data that was previously obtained from a mongodump into a MongoDB cluster.

mongorestore can be used with standalone deployments and replica sets, but it should not be used on sharded clusters.

When using mongorestore, there are a few things to consider: First, there must be version compatibility between the source and target MongoDB clusters to prevent data loss. It’s also crucial to have version compatibility between mongodump and mongorestore. Next, mongorestore handles network partitions poorly and does not pick up where it left off on restart. It also provides limited support for point-in-time restores. 

mongorestore will not update matching documents, but will insert new ones.Additionally, mongorestore does not restore the system.profile collection. Finally, mongorestore may trigger index builds, which can be time consuming and taxing on the target cluster.

Let’s take a look at the syntax for mongorestore and how to use it. The basic syntax for mongorestore is the mongorestore command, the mongorestore command, followed by options, the connection string, and the directory or file that you want to restore from.

When authentication is enabled on a MongoDB deployment, it’s recommended that the user have the restore role when using mongorestore.

In addition, a user must have the dbAdmin or the dbAdminAnyDatabase role if the backup data includes the system.profile collection data, or if you run mongorestore with the oplogReplay option, even though the collection will not actually be restored.

Finally, it’s recommended that the user have a custom role with anyAction on anyResource as privileges when attempting a point-in-time restore.

There are a number of options available when using mongorestore.

Let’s go over some of the most useful options: nsInclude, nsExclude, drop, noIndexRestore, writeConcern, gzip, archive, and oplogReplay.

The nsInclude and nsExclude options give us more control over the namespaces that we want to restore. Both options support wildcard usage via the asterisk.

Using the drop option with mongorestore will remove matching collections from the target database before reloading them. Restored documents will get a new _id.

The mongorestore operation can impact the performance of the cluster due to index generation. The noIndexRestore eliminates this concern and does not create any indexes.

Next, the writeConcern option controls the durability requirements for the restore when using a replica set. The default writeConcern is majority.

The gzip option tells mongorestore that the source data was compressed by mongodump.

The archive option tells mongorestore to use the given filename as the singular source of data instead of a directory.

The oplogReplay option provides replica sets with a point-in-time restore option. When enabled, this option searches for oplog data in the source and applies it to the target after restoring the source documents.

Now let’s go through an example that uses mongorestore in Atlas.

Here we’re using mongorestore with increased verbosity to improve progress reporting.

Next, we’ll restore all data from a previous backup. The backup is in a compressed archive file.

Then we drop existing collections before we reload them, and let mongodump use the database and collection names from the archive file.

Our cluster has security enabled, so we specify the username in the connection string

Because this is Atlas, we don’t need to specify the authSource argument on the connection string. However, for self-managed deployments that don't have the SRV specifier, you will need to specify the authSource.

When we run the command, we’ll be prompted to enter our password in the terminal.

Great! We can see from the output that the command was successful.

Let’s recap what you learned in this video: The mongorestore utility can be used to load data that was previously obtained from a mongodump into a MongoDB cluster. mongorestore can be used with standalone deployments and replica sets, but it should not be used on sharded clusters. You also learned the basic syntax for mongorestore, which is mongorestore followed by the options, the connection string, and the directory or file to restore. 

Some of the most useful options that you can use with mongorestore are: nsInclude, nsExclude, drop, noIndexRestore, writeConcern, gzip, archive, and oplogReplay.

For more options that you can use with mongorestore, check out MongoDB’s documentation.