Creating backups from LVM snapshots

Written by
Shubhra Prakash Nandi's picture

Logical volume management (LVM) on Linux provides an easy way to create a point in time snapshot of the logical volume (LV) hosting a filesystem. These snapshots freeze the data at that point of time when the snapshot was created even though actual data may keep getting modified on the real logical volume. This allows you to access the data exactly how it was at the point of time when the snapshot was created making way to create point in time backups of the data.

The LVM snapshots are created using copy-on-write (CoW) method. Snapshots donot hold any data when they are created. As data is written to the real LV blocks, original blocks are copied over to the snapshot before they are written over in the real LV. So when the snapshot is read by the system all unwritten blocks are read from the real LV and modified blocks are read from the snapshot. This makes it possible for the size of snapshot to be really small even though the real LV size may be much bigger than what is allocated to it's snapshot.

Now lets see how we can take a backup of the data after creating a snapshot of a LV.

Let's say we have a logical volume /dev/VG1/LV1 of size 50GB where the VG1 size is 55GB. Now let's create it's snapshot of size 1GB.

lvcreate --size "1G" -s -n "LV1_SNAPSHOT1" "/dev/VG1/LV1"

The above command will create a snapshot for logical volume /dev/VG1/LV1 by the name LV1_SNAPSHOT1.

This snapshot will be good till either the real LV gets 1GB of data modified in it after the snapshot was created or you explicitly drop the snapshot. Once the snapshot size gets filled, it will be automatically dropped and will no longer be usable.

Now let's mount this snapshot to location /mnt/lvm_snapshot and backup the data from it. This can be done using the below commands.

mount -o ro /dev/mapper/VG1-LV1_SNAPSHOT1 /mnt/lvm_snapshot
rsync --archive --delete --verbose /mnt/lvm_snapshot/* <backup location>
umount /mnt/lvm_snapshot
lvremove -f "/dev/VG1/LV1_SNAPSHOT1";

This achieves our purpose of creating a LVM snapshot.

A few important considerations about using a LVM snapshot

  1. Snapshot size should be chosen as per the time it takes you to take a complete backup of the data from the snapshot, from the time the snapshot was created, and how fast new data is written to your real LV. If data is written at the rate of 600MB/hour on your real LV and it takes you 5min from the time the snapshot was created to take a complete backup from the snapshot, then 50MB + 5MB (buffer) i.e. a snapshot size of 55MB should be sufficient for you.
  2. Since snapshots work on CoW method, it has to do two write operations for a single write operation on the real LV i.e. one on the real LV and one on the snapshot making the real LV slower during write operations. So as a rule remove the snapshot as soon as data from it has been backed up.
  3. Also snapshots cannot be created unless the volume group (VG) on which the real LV resides has free space equivalent to or more than the new snapshot size. If all of the space in the VG has been allocated to LVs then snapshots cannot be created for the LVs residing in it till free space is added to the VG.
Zircon - This is a contributing Drupal Theme
Design by WeebPal.