An LVM snapshot is an exact mirror copy of an LVM partition that has all the data from the LVM volume from the time the snapshot was created. The main advantage of LVM snapshots is that they can reduce the amount of time that your services/applications are down during backups because a snapshot is usually created in fractions of a second. After the snapshot has been created, we can back up the snapshot while our services and applications are in normal operation.
LVM snapshot is the feature provided by LVM(Logical Volume Manager) in Linux. While creating lvm snapshot, one of most common question comes to our mind is that what should be the size of snapshot?
“snapshot size can vary depending on your requirement but a minimum recommended size is 30% of the logical volume for which you are taking the snapshot but if you think that you might end up changing all the data in logical volume then make the snapshot size the same as logical volume”.
Scenario: We will take snapshot of /home which is LVM based parition.
# df -h /home/ Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_home 5.0G 139M 4.6G 3% /home
Taking snapshot of /dev/mapper/VolGroup-lv_home partition
LVM snapshot is created using lvcreate command, one must have enough free space in the volume group otherwise we can’t take the snapshot, Exact syntax is given below:
# lvcreate -s -n [Snapshot Name] -L [Size of snapshot] [lvm volume for which to take snapshot]
For example:
# lvcreate -s -n home_snap -L1G /dev/mapper/VolGroup-lv_home Logical volume "home_snap" created
Now verify the newly create LVM ‘home_snap’ using lvdisplay command:
# lvdisplay /dev/mapper/VolGroup-lv_home
Now Create the mount point(directory ) and mount it:
# mkdir /mnt/home-backup # mount /dev/mapper/VolGroup-home_snap /mnt/home-backup/ # ls -l /mnt/home-backup/
The above command will show all directories and files that we know from our /home partition.
Now take the backup of snapshot of /opt directory
# tar zcpvf /opt/home-backup.tgz /mnt/home-backup/
If you want the bitwise backup , then use the below command:
# dd if=/dev/mapper/VolGroup-home_snap of=/opt/bitwise-home-backup 10485760+0 records in 10485760+0 records out 5368709120 bytes (5.4 GB) copied, 79.5741 s, 67.5 MB/s
Restoring Snapshot Backup
If anything goes wrong with your /home file system, then you can restore the backup that we have taken in the above steps. You can also mount the lvm snapshot on /home folder.
Remove LVM Snapshot
Once you are done with the lvm snapshot backup and restore activity, you should umount and remove the lvm snapshot partition using the below commands as the snapshot is consuming system resources like disk space of respective volume group.
# umount /mnt/home-backup/ # lvremove /dev/mapper/VolGroup-home_snap Do you really want to remove active logical volume home_snap? [y/n]: y Logical volume "home_snap" successfully removed