RHEL6 – Snapshotting an LVM Volume

Oh-snapSnapshotting in the context of LVM is the process of taking a point-in-time image of a filesystem and creating a separate copy of that filesystem on another filesystem… using LVM of course.

Snapshoting can be useful if you need to take a backup of a filesystem but want the filesystem to be a quiesced state when you do so. Using LVM and snapshots you just backup the snapshot of the original filesystem.

First lets create the “master” filesystem using /dev/sdc1 which has partitioned as Linux LVM in fdisk.

So create the physical volume

>pvcreate /dev/sdc1

Then create a volume group containing the physical volume

>vgcreate test_vg /dev/sdc1

Then create a logical volume called v1 which is 5gb in size. Note that this volume group is 10gb in total size.

>lvcreate -n v1 -L 5G test_vg

Then create a filesystem on that volume

>mkfs -t ext4 /dev/test_vg/v1

Then mount this volume to /v1 and put some data in it.

>mkdir /v1

>mount /dev/test_vg/v1 /v1

>cp /var/tmp/VMwareTools-8.3.2-257589.tar /v1

Ok, now that we have a logical volume with some data in it, lets make a snapshot of it.

First lets make a mountpoint for our snapshot

>mkdir /v2

Now lets make the snapshot of /dev/test_vg/v1. Note that a snapshot volume can be as large or a small as you like but it must be large enough to hold all the changes that occur on the original volume during the lifetime of the snapshot. In this example I am creating a snapshot of 4gb.

>lvcreate -n snapvol -L 4G -s /dev/test_vg/v1

Using lvs or lvscan you can take a look at the snapshot volume.

>lvscan

  ACTIVE   Original ‘/dev/test_vg/v1’ [5.00 GiB] inherit
  ACTIVE   Snapshot ‘/dev/test_vg/snapvol’ [4.00 GiB] inherit

Now lets mount the snapshot readonly.

>mount -o ro /dev/test_vg/snapvol /v2

Now you can browse the contents of the snapshot, unmount it and mount it as needed. And back it up as well. Make sure that you remove the snapshot when done backing it up

>lvremove /dev/test_vg/snapvol