How to Snapshot a Volume
In the previous tutorial (How to Create Logical Volumes and Filesystems), we have created two logical volumes (lv01, lv02) on top of a volume group (vg01). We also created filesystems on top of these logical volumes. Now in this tutorial, we will write some data in lv01 logical volume and then we will take a snapshot of this logical volume. After this, we will verify whether the snapshot contains the same data as lv01’s logical volume.
First of all, use the lsblk command to display all the block devices containing volume groups and logical volumes.
Now create a directory ‘/home/$USER/lv02’ and mount the lv02 filesystem on this directory.
Copy some data in this filesystem.
The above command will create a file in the lv02 filesystem. Verify if the file is present in the logical volume using the cat command.
Check for the free space in the volume group and the size of the filesystem on top of logical volume. Use the vgs command to check free space in the volume group. Similarly, use the lvs command to check the size of the filesystem.
The size of lv02 logical volume is 5 GB and the free space in the volume group vg01 is almost 15 GB. For testing purposes, we will create a snapshot of only 1GB. Before creating a snapshot of a logical volume, always make sure you have enough space to create the snapshot. Use the lvcreate command along with the -s flag to create the snapshot of the logical volume.
After creating the snapshot, check the logical volumes using the lvs command in the terminal.
It is showing the snapshot of the logical volume lv02 in the volume group vg01 and the size of the snapshot that is 1GB. The Origin column shows the origin of the snapshot that is lv02. Currently, the Data% for lv02_snap is 0.01. We will check it again after copying some data to the original logical volume lv02.
Mount the lv02_snap logical volume on the system using the mount command.
List all the filesystems mounted on the system using the df command in the terminal.
It is showing the original filesystem as well as the snapshot mounted on the system. Use the cat command to verify whether the file is present in this snapshot of the logical volume lv02 or not.
The file can be accessed through the snapshot. Copy some more data to the original logical volume lv02.
Again, display all the logical volumes using the lvs command in the terminal.
Previously the Data% was 0.01 and now it is 5.53. The data from the logical volume lv02 has been copied to the snapshot successfully.
Snapshot a Volume Using GUI Tool
So far, we have been using the command line interface to create a snapshot of logical volumes. Now, we will use the kvpm which is a GUI tool to manage logical volumes to create a snapshot of logical volume. We’ve already discussed the installation procedure of the kvpm in the previous tutorial (How to Create Logical Volumes and Filesystems). Open kvpm form the command line using the following command.
From the top, go to the vg01 volume group tab.
It will display all the logical volumes from the volume group. In order to create a snapshot of the volume group lv01, select the lv01 logical volume and click on ‘Snapshot’.
It will ask for snapshot details. Provide the snapshot name and the size and click on ‘OK’.
It will create a snapshot of the logical volume lv01.
Verify from the command line whether the snapshot has been created or not using the lvs command.
Snapshot of size 1GB from logical volume lv01 has been created. Currently the Data% in the lv01_snap is 0. Now, mount the snapshot on the system using the mount command.
Copy some data to the logical volume lv01 and check from the GUI tool whether the snapshot of the lv01 occupies the space or not.
Check from the kvpm whether the data from the logical volume lv01 has been copied to the snapshot or not.
How to Remove a Snapshot
In order to remove a snapshot of a logical volume properly, unmount the snapshot using the umount command first then use the lvremove command to remove the snapshot.
Using the GUI tool, select the snapshot, click on ‘unmount fs’ then click on delete to remove the snapshot.
Having backups of your data on a daily basis is one of the best strategies to avoid data loss. System administrators use different techniques to generate snapshots of volumes. In this tutorial, we used the lvm tool to create snapshots of the logical volume which can be restored in case of data loss.