Video Transcript (English)
There are many ways to back up a MongoDB database. One of them is to use mongodump. In this video, we’ll go through an example of using mongodump to back up a replica set.
mongodump is not ideal for backing up large systems, but it’s well suited for small deployments and for seeding data.
mongodump should not be used on sharded clusters.
For production-quality backup and recovery strategies, we strongly recommend one of our coordinated backup and restore alternatives, such as: MongoDB Atlas, MongoDB Cloud Manager, or MongoDB OpsManager
Now we’ll go through an example of using mongodump step by step.
First, let’s create a new backup 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 backup user.
Here, we create a user named backup-admin with a password of backup-pass. We’ll also give this user a built-in role called backup. The backup role provides the minimum privileges needed to back up data.
With the user created, we can now exit the shell.
Next, to use mongodump, type mongodump in the command line followed by the options.
The first option we use is the oplog option to capture incoming write operations during the mongodump operation. The resulting oplog.bson file provides an effective point-in-time snapshot of the deployment.
Note that the point in time is the time when the backup is completed, which happens only if the oplog window is large enough.
We also use the gzip option to have mongodump compress the output for us.
Next, we use the archive option to specify the file location for the dump file. In this case, we send it to mongodumpapril-2023.gz
Our cluster has security enabled, so we specify the username in the connection string.
For self-managed deployments that don't have the SRV specifier, you will need to specify the authSource.
Before running mongodump, 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 reduce the strain on server resources or its data cache and minimize any performance issues, we can use the readPreference option and set it to secondary or secondaryPreferred.
Note that using anything other than the primary can result in stale data.
In this case, we don’t mind potentially having stale data so that we can limit the impact on the performance of the cluster. So, we’ll add the read preference option to direct reads to the secondary.
When we run the command, we’ll be prompted to enter our password in the terminal.
Then, we should get the following output. It shows where the file was sent and which collections were copied. We can also see the oplog being copied.
Let’s say that we only want to back up a specific database or collection. We can do that by using the db and collection options. The db option can be specified as a command line argument or as part of the connection string.
Here, we will back up only the neighborhoods collection in the sample_restaraunts database. When specifying a single database or collection, we can’t use the oplog option.
When we run the command, we’ll be prompted to enter our password in the terminal.
The output confirms that only the neighborhoods collection was copied.
Great job!
In this video, we went through an example of using mongodump to back up a replica set.
You also learned how to tailor mongodump to back up a specific collection by using the db and collection options.