LVM Snapshot : Backup & restore LVM Partition in linux
An LVM snapshot is an exact mirror copy of an LVM partition which 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 / application 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 same as logical volume "
Scenario : We will take snapshot of /home which is LVM based parition.
[root@localhost ~]# 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 >
[root@localhost ~]# 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
Now Create the mount point(directory ) and mount it
[root@localhost ~]# mkdir /mnt/home-backup [root@localhost ~]# mount /dev/mapper/VolGroup-home_snap /mnt/home-backup/ [root@localhost ~]# ls -l /mnt/home-backup/
Above command will show all directories and files that we know from our /home partition
Now take the backup of snapshot on /opt folder .
[root@localhost ~]# tar zcpvf /opt/home-backup.tgz /mnt/home-backup/
If you want the bitwise backup , then use the below command :
[root@localhost ~]# 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 above steps. You can also mount the lvm snapshot on /home folder.
Remove LVM snapshot
Once you are done with lvm snapshot backup and restore activity , you should umount and remove lvm snapshot partition using below commands as snapshot is consuming system resources like diskspace of respective voulme group.
[root@localhost ~]# umount /mnt/home-backup/ [root@localhost ~]# 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