Intermediate Linux Skills – Working With LVM Snapshots

21st February, 2017 by
Logical volumes are one of Linux’s methods of getting greater flexibility. These are how the Operating System can use the physical drives within a computer. While under traditional partitioning you would create fixed partitions of primary or extended types within a disk. With Logical Volume Management (LVM), you can create partitions that can span over multiple physical drives and be moved, resized and backed up – all without having to stop a running system while doing so.

This flexibility comes at a cost, in that dealing with LVM volumes can be more complicated than dealing with traditional partitioning. In this article, we’ll be looking at the snapshot facility of LVM in order to create backups of live systems without needing to take them offline.

Taking A Snapshot

The way snapshots work is by making a duplicate of the logical volume you desire to backup. To do this you will need to have an amount of space available on a volume group, the same size or larger than the logical volume of which you are taking a snapshot. For information on managing your logical volumes, I’ll refer you back to a previous article on logical volume management.

For the sake of this example, we are going to be dealing with a server containing one volume group named “volume1”, and we will be taking a snapshot of the logical volume named “root” that is 50GB in size.

For this we will use the command:

sudo lvcreate -L50G -n rootsnapshot -s /dev/volume1/root

From the above command, we can see from the “-L” flag that we are making a 50GB logical volume. The “-n” flag shows we are naming the new volume “rootsnapshot”. The “-s” flag denotes that our new volume is a snapshot of the “/dev/volume1/root” volume.

Backed Up And Ready

With this command, our snapshot image is created and is a duplicate of the root volume at that moment in time. As changes continue to be written to the root volume, the snapshot volume will remain unchanged. From this snapshot, you can now perform backups of constantly changing files such as databases without the backup containing corrupted data. Alternately, you could use dd to create a full disk image backup ready to restore in the case of a major failure of the root volume. To do this you will need a volume with suitable space to store an image of the snapshot. In this case, you would need 50GB. We’ll be using a 300GB volume mounted to the filesystem as /backups.

To create the disk image of the snapshot we’d use a dd command as follows:

sudo dd if=/dev/volume1/rootsnapshot of=/backups/rootsnapshot.img

Here we’ve told dd to use an input file (if) of our rootsnapshot volume and create an output file (of) of /backups/rootsnapshot.img. Note that this will take some time, and the larger your snapshot file the longer it will take to create. Helpfully dd doesn’t provide any information of what it’s doing so you will be left with just a blinking cursor for the duration of the backup.

If you are running a new version of dd (from GNU CoreUtils 8.24 or higher) there’s now a status option which can provide progress:

sudo dd if=/dev/volume1/rootsnapshot of=/backups/rootsnapshot.img status=progress

In The Event Of An Emergency…

So now you have an image of your root filesystem from the time the snapshot was taken, ready to restore in the case of an emergency. Once the backup image is taken you can then remove the snapshot logical volume.

sudo lvremove /dev/volume1/rootsnapshot

Recovering from the snapshot backup image is also a straightforward process. Booting your system from a live CD, you can mount the /backups volume and then use dd to copy the snapshot backup over the original volume.

For example, imagine we’ve booted from the recovery CD and are at a root shell. At this point, we can mount our backups volume:

mkdir /mnt/backups

mount /dev/volume1/backups /mnt/backups

Then we use dd to recover:

dd if=/mnt/backups/rootsnapshot.img of=/dev/volume1/root

This will then copy the data from our snapshot image over the damaged root volume. Once the dd command completes, the recovery CD can be removed and the system rebooted. It should then boot up from the root logical volume as it would have before, with the data that had been stored in the snapshot.

(Visited 263 times, 1 visits today)