Video Transcript (English)
In this video, you’ll learn how to back up and restore a MongoDB deployment by using a snapshot. To do this, we’ll use Logical Volume Manager, or LVM, along with the `dd` command.
`dd` is a command-line utility in Linux that allows you to convert and copy data. This is useful for backing up and restoring volumes created by LVM.
Before we go through an example, let’s gather some information about our existing MongoDB deployment.
First, we have created a physical volume on one of our provisioned hard drives. In this physical volume, we have created a volume group named vg0.
Inside the vg0 volume group, we have created a 600-megabyte logical volume named mdb and mounted it to the data files located at /var/lib/mongodb.
Now let’s get a reference point from our running instance.
In the terminal, we will connect to our database. Then we’ll show all of the databases we currently have so that later we can confirm that our snapshot restored everything.
Before creating a snapshot of our deployment, we need to lock the database by using the fsyncLock() command.
This will make it so nothing can be written to the database. It also flushes any write operations that have not yet been written to the disk.
We get confirmation that our database is locked, so now we can now exit the MongoDB Shell.
Now we create a snapshot volume by using lvcreate. It has a maximum size of 100 megabytes, and is named mdb-snapshot and backed by our MongoDB data store volume.
The snapshot volume needs to be large enough to hold changes that occur during the creation of the snapshot.
When we run the command, we’ll get a confirmation message that a logical volume was created.
We can also confirm that the snapshot was taken by running sudo lvs.
We see that there’s an mdb-snapshot.
With the snapshot created, it’s crucial that we reconnect to the MongoDB instance and unlock our database. If we forget to do this, our database will not be able to perform write operations. We cannot stress enough how important this step is.
We get confirmation that the database is unlocked, so we can now exit the MongoDB Shell.
Let’s archive our snapshot. Back in our terminal, we run the following command.
This command performs a complete copy of the snapshot volume, streams the data to the gzip utility for compression, and finally redirects the output to our archive file.
It will take a couple of minutes to complete, but by the end we’ll have a compressed file in our current directory.
We should move this archive to a different location for safe storage.
Next, let’s restore our database from a snapshot that we archived with dd.
In the terminal, we’ll create a new logical volume named mdb-new.
Next, we need to extract the snapshot and write it to our new logical volume.
Now we need to mount this new volume somewhere to access the data files. We’ll reuse the existing MongoDB mount point. But first, we need to stop the MongoDB service.
Let’s also delete any existing MongoDB data files. This is just for demonstration purposes to show how the entire deployment is restored.
Next, we must unmount our MongoDB deployment so that we can mount the newly restored logical volume in its place.
Finally, we mount the restored logical volume on our MongoDB database directory.
With that step completed, our database has been restored by using a snapshot.
To confirm that this worked, let’s start the MongoDB instance.
Then we log back in to the MongoDB Shell n and show our databases.
Note that when you’re done with your snapshots, it’s a good idea to remove them from your server to preserve resources.
Awesome work! In this video, you learned how to back up and restore a MongoDB deployment by using an LVM snapshot.
We also covered key commands to assist us, such as: dd, db.fsyncLock(), db.fsyncUnlock()