LVM Snapshot


LVM snapshot is a special type of logical volume which contains all the data in the source logical volume at the time of snapshot creation. Once the snapshot is created, all the changes made to the source local volume will only be saved in snapshot.So the amount of space necessary for snapshot volume is dependent on the amount of changes we do to the source logical volume.

If the snapshot logical volume becomes full it will be dropped.If the snapshot size equals the origin size, it will never overflow.

 

Step 1:

Below are the logical volume and mount point of the logical volume. I am going to create a LVM snapshot for the logical volume LV01.

[root@server1 /]# df -h /mount_point
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VG01-LV01 9.8G 52M 9.2G 1% /mount_point

 

[root@server1 /]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
LV01 VG01 -wi-ao—- 10.00g
LV02 VG01 -wi-a—– 40.00m

 

Step 2:

Before creating a LVM snapshot. Verfiy that your volume group(here VG01) have enough space to create snapshot.
I created the snapshot(LV01_snapshot) for the source logical volume LV01 with the size 4GB

[root@server1 /]# lvcreate -s -L 4G -n LV01_snapshot /dev/mapper/VG01-LV01
Logical volume “LV01_snapshot” created

 

[root@server1 /]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
LV01 VG01 owi-aos— 10.00g
LV01_snapshot VG01 swi-a-s— 4.00g LV01 0.00
LV02 VG01 -wi-a—– 40.00m

 

Step 3:

Now verify the files present in the LVM snapshot by mounting it.

[root@server1 /]# mount /dev/mapper/VG01-LV01_snapshot /mnt/
[root@server1 ~]# ls /mnt/
abcd.txt file1 file2 file3 lost+found

Also list the files present in the source logical volume LV01 which is mounted in /mount_point/

[root@server1 /]# ls /mount_point/
abcd.txt file1 file2 file3 lost+found

Both mount point have the same files. Now remove some of the files present is source logical volume LV01.

[root@server1 /]# rm -f /mount_point/abcd.txt

 

Step 4:

Now we can restore the deleted files from the source logical volume using the snapshot

[root@server1 ~]# umount /mnt
[root@server1 ~]# lvconvert –merge /dev/mapper/VG01-LV01_snapshot
Logical volume VG01/LV01 contains a filesystem in use.
Can’t merge over open origin volume.
Merging of snapshot LV01_snapshot will start next activation.

Now we can see the LVM snapshot is removed

[root@server1 ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
LV01 VG01 Owi-aos— 10.00g
LV02 VG01 -wi-a—– 40.00m

Now deactivate and activate the source logical volume to complete the restoration. This should be done offline.

[root@server1 ~]# umount /mount_point/
[root@server1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 20G 4.5G 15G 23% /
devtmpfs 485M 0 485M 0% /dev
tmpfs 490M 0 490M 0% /dev/shm
tmpfs 490M 6.7M 484M 2% /run
tmpfs 490M 0 490M 0% /sys/fs/cgroup
/dev/sda1 97M 70M 28M 72% /boot

 

[root@server1 ~]# lvchange -a n /dev/mapper/VG01-LV01
[root@server1 ~]# lvchange -a y /dev/mapper/VG01-LV01

Now we can see the deleted file is restored.

[root@server1 ~]# cd /mount_point/
[root@server1 mount_point]# ls
abcd.txt file1 file2 file3 lost+found

 

Optional:

In case we don’t need the snapshot we can delete it use lvremove

[root@server1 mount_point]# lvremove /dev/mapper/VG01-LV01_snapshot