Video Transcript (English)
While snapshots are better for larger systems, the command-line utility mongorestore is a useful tool for restoring or seeding smaller systems.
In this video, we’ll go through an example of using mongorestore with the oplogReplay option to restore a replica set.
When using mongorestore, we must ensure that the source and target major versions are the same. For example, if we have a mongodump file from a server running MongoDB 6.X, we need to restore it to a server running a 6.X major version.
We also need to ensure that we are using the same version of mongorestore to load the data files as the version of mongodump that we used to create them.
Now let’s look at an example of using mongorestore.
First, we will create a new restore user for the deployment. Access control is enabled, so we specify an administrative user in the connection string and enter the password when prompted. We’re connecting to the admin database, which is where we intend to create our new restore user.
Here, we create a user named restore-admin with a password of restore-pass. We’ll also give this user a built-in role called restore. The restore role provides the minimum privileges needed to restore data.
With the user created, we can now exit the shell.
Before running mongorestore, it’s important to consider the performance impact on the target cluster. Not only do the tools create traffic for a running database instance, they also force the database to read all data into memory.
To use mongorestore, type mongorestore in the command line followed by the options.
First, we specify the drop option to remove any existing collections from the database that we are restoring.
Next, the source file is compressed, so we specify the gzip option.
Then, we use the oplogReplay option, which will replay the oplog entries from the oplog.bson file.
We use the noIndexRestore option to reduce the impact on the system. By doing this, we will need to rebuild the indexes when the restore is complete.
Then, we use the archive option to specify the file location of the dump file. In this case we are usingmongodumpapril-2023.
Finally, we specify the connection string to the replica set. The connection string contains the username and authSource to authenticate as restore-admin.
When we run the command, we’ll be prompted to enter our password in the terminal.
After doing so, we’ll see the collections that were restored and that our oplog was replayed
Awesome job!
In this video, you learned how to restore a MongoDB replica set by using mongorestore with the oplogReplay option.