Wednesday, August 17, 2011

LVM Snapshots

First of all some of important facts about LVM Snapshots

A snapshot volume does not have to be as large as the source volume

LVM snapshots use a copy on write (CoW) technology

The snapshot volume only needs to be large enough to accommodate the maximum number of changes that might occur to the source volume during the life of the snapshot.

Snapshot is really just a copy of the inode tree at the point in time you created it

Let us understand this with an example. Suppose you have a 20GB Logical Volume where you storing your data. Now suppose in normal circumstance approx 500MB of data get some changes daily. In this case you need to create 500MB LVM snapshot for your 20GB LVM. The modified data of LVM stored on snapshot instead of LVM. So when it is time to read from the snapshot , any changed blocks are read from the snapshot area. But any blocks that haven't changed since the snapshot are read from the original.

LVM snapshots can be used for taking data backup of live system. You can keep snapshot for a given period of time after that you can take backup of snapshot for its further uses. It you need to recover data from snapshot you need to mount it separately and get data from it.

Command to create snapshot:

root#lvcreate -L 500M -s -n databackup /dev/vg1/lv1

Here databackup is name of snapshot and its size will be 500MB. This is snapshot of Logical Volume lv1.

To recover data from snapshot, you need to mount it as following and then perform recovery

root#mount -t ext3 /dev/vg1/databackup /mnt

To take backup of LVM snapshot for further references, you may use following command

root#tar -cf /mybackup/lv1backup /mnt

After taking it is advisable to remove snapshot, for that you can use following command

root#umount /mnt

root#lvremove /dev/vg1/databackup