Lesson 4: Filesystem Archives on a MongoDB Server / Learn

Video Transcript (English)

In this video, you’ll learn how to back up and restore a MongoDB deployment by using an archive file taken from the filesystem of a volume snapshot. We will use the Linux Logical Volume Manager and the `tar` utility.

Tape Archive, or tar, is a Linux tool to create and manipulate streaming archive files. tar makes moving files from one server to another much easier. It can also compress the data when the compression option is enabled.

Before we show 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.

Let’s go through an example.

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 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 a mdb-snapshot.

With the snapshot created, it’s crucial that we reconnect to the MongoDB shell 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.

Back in the terminal, we make a new temporary directory named mongodbsnap.

Next, we mount the snapshot volume we took earlier as read-only on the directory that we just created.

Finally, we use `tar` to create a new archive of all the files in the mongodbsnap directory.

After a couple of seconds, 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 tar.

In the terminal, we will create a new directory.

Next, we extract the compressed `tar` file into the new directory.

When this is complete, we’ll stop the mongodb service before we update the dbPath in the MongoDB configuration file.

Then we need to make the mongodb user and group the owner of the mdb directory and all of its files. Otherwise, the service will fail to start.

Now let’s open the mongod.conf file and change the dbPath to /mdb.

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 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 archive file taken from the filesystem of a volume snapshot. We covered how to do this by using the Linux Logical Volume Manager and the `tar` utility.