Snapshots are the copy of a filesystem, partition or a volume at a specific time. When a snapshot is captured, it copies all the data from a filesystem. In case of data loss, this snapshot can also be used to recover data. In this tutorial, we will use the lvm tool to snapshot a volume or a directory.

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-648.png" data-lazy- height="644" src="data:image/svg xml,” width=”644″>

Now create a directory ‘/home/$USER/lv02’ and mount the lv02 filesystem on this directory.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-650.png" data-lazy- height="146" src="data:image/svg xml,” width=”642″>

Copy some data in this filesystem.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-651.png" data-lazy- height="95" src="data:image/svg xml,” width=”746″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-653.png" data-lazy- height="112" src="data:image/svg xml,” width=”618″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-655.png" data-lazy- height="253" src="data:image/svg xml,” width=”660″>

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.

OR

[email protected]:~$ sudo lvcreate –size 1GB –snapshot –name lv02_snap /dev/vg01/lv02

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-657.png" data-lazy- height="129" src="data:image/svg xml,” width=”658″>

After creating the snapshot, check the logical volumes using the lvs command in the terminal.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-659.png" data-lazy- height="201" src="data:image/svg xml,” width=”706″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-661.png" data-lazy- height="129" src="data:image/svg xml,” width=”706″>

List all the filesystems mounted on the system using the df command in the terminal.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-662.png" data-lazy- height="186" src="data:image/svg xml,” width=”705″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-664.png" data-lazy- height="129" src="data:image/svg xml,” width=”706″>

The file can be accessed through the snapshot. Copy some more data to the original logical volume lv02.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-665.png" data-lazy- height="146" src="data:image/svg xml,” width=”706″>

Again, display all the logical volumes using the lvs command in the terminal.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-667.png" data-lazy- height="201" src="data:image/svg xml,” width=”705″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-669.png" data-lazy- height="177" src="data:image/svg xml,” width=”714″>

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’.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-670.png" data-lazy- height="316" src="data:image/svg xml,” width=”710″>

It will ask for snapshot details. Provide the snapshot name and the size and click on ‘OK’.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-672.png" data-lazy- height="436" src="data:image/svg xml,” width=”337″>

It will create a snapshot of the logical volume lv01.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-673.png" data-lazy- height="111" src="data:image/svg xml,” width=”488″>

Verify from the command line whether the snapshot has been created or not using the lvs command.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-674.png" data-lazy- height="236" src="data:image/svg xml,” width=”724″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-675.png" data-lazy- height="129" src="data:image/svg xml,” width=”722″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-676.png" data-lazy- height="163" src="data:image/svg xml,” width=”722″>

Check from the kvpm whether the data from the logical volume lv01 has been copied to the snapshot or not.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-677.png" data-lazy- height="70" src="data:image/svg xml,” width=”491″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-678.png" data-lazy- height="163" src="data:image/svg xml,” width=”722″>

Using the GUI tool, select the snapshot, click on ‘unmount fs’ then click on delete to remove the snapshot.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-679.png" data-lazy- height="334" src="data:image/svg xml,” width=”709″>

Conclusion

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.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/usama-1.jpg" height="112" src="data:image/svg xml,” width=”112″>

Usama Azad

A security enthusiast who loves Terminal and Open Source. My area of expertise is Python, Linux (Debian), Bash, Penetration testing, and Firewalls. I’m born and raised in Wazirabad, Pakistan and currently doing Undergraduation from National University of Science and Technology (NUST). On Twitter i go by @UsamaAzad14