Home Linux Disk Management How To Use LVM Snapshot To Backup Your Data In Linux

How To Use LVM Snapshot To Backup Your Data In Linux

Linux LVM Snapshots - Create, Extend And Restore LVM Volume Snapshots In Linux

By Karthick
Published: Updated: 4.2K views

In the last article, we have seen how to expand and shrink volume groups and logical volumes. In this article, we will focus on how to protect the data by creating LVM snapshots.

What is LVM Snapshots?

There are two options to safeguard your data from disk failures or other issues. You can take full or incremental backups and store N number of copies. Alternatively with LVM, you can create a snapshot volume that will take snapshots when changes are made to the source volume.

LVM snapshots use a copy-on-write mechanism to take snapshots. Initially, when you create a snapshot volume it will hold some metadata about the source logical volume and its block details. When you make any changes in the source volume, LVM will monitor the changes and take a snapshot of the modified blocks. Here LVM just stores the changes blocked to the snapshot volume.

Lab Setup

I have attached two new disks to my Ubuntu machine which will be used for demonstration purposes. If you wish to know how the following command works in detail, then check our introduction to the LVM guide given below.

Linux Logical Volume Manager (LVM) Guide For Beginners

$ lsblk /dev/sd[cd]
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdc    8:32   0  10G  0 disk 
sdd    8:48   0  15G  0 disk 

Both the disks are initialized as the physical volume.

$ sudo pvcreate /dev/sd[cd]
Physical volume "/dev/sdc" successfully created.
Physical volume "/dev/sdd" successfully created.

A volume group named "ostechnix_lab" is created from both the initialized physical volume.

$ sudo vgcreate ostechnix_lab /dev/sd[cd]
Volume group "ostechnix_lab" successfully created

A Logical volume named "lab" is created from the "ostechnix_lab" volume group. The logical volume size is 5GB.

$ sudo lvcreate -L 5GB -n lab ostechnix_lab
Logical volume "lab" created.

Volume is formatted with the ext4 file system and mounted on /opt/lvm_lab/.

$ sudo mkfs.ext4 /dev/ostechnix_lab/lab
$ mkdir /opt/lvm_lab
$ sudo mount /dev/ostechnix_lab/lab /opt/lvm_lab/

After creating the logical volume, currently there are nearly 20GB of free space.

$ sudo vgdisplay
  --- Volume group ---
  VG Name               ostechnix_lab
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               24.99 GiB
  PE Size               4.00 MiB
  Total PE              6398
  Alloc PE / Size       1280 / 5.00 GiB
  Free  PE / Size       5118 / 19.99 GiB
  VG UUID               LCGSEd-WTnT-lUEN-N0a8-QWhI-TqDi-lUZExd

Creating Snapshot Volume

I am going to create a snapshot volume of 5GB. Run the following command to create a snapshot volume:

$ sudo lvcreate -L +5G --snapshot --name snap /dev/mapper/ostechnix_lab-lab
  Logical volume "snap" created.

Let me walk through what each flag in the above command does.

  • -L +5G => The space value should be passed to -L flag. Here 5GB is allocated.
  • --snapshot => This flag creates the volume as a snapshot volume. You can also use -s instead of --snapshot flag.
  • --name => Name given to the snapshot volume. In my case "snap" is the name of my snapshot volume.
  • /dev/mapper/ostechnix_lab-lab => This is the source volume location.

Run the "lvs" command to see the snapshot volume. Take a look at the "origin" section from the below output. It points to the source volume "lab" and the data% is "0.01". snapshots will be taken if any changes to the blocks in that volume.

$ sudo lvs
  LV   VG            Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lab  ostechnix_lab owi-aos--- 5.00g                                                    
  snap ostechnix_lab swi-a-s--- 5.00g      lab    0.01  

You can also mount the snapshot volume and see what files and directories are present.

$ mkdir /mnt/LVM_Snapshot/
$ mount /dev/mapper/ostechnix_lab-snap /mnt/LVM_Snapshot/

How To Restore LVM Snapshot

Run the following command to create some random data in the source volume of 1GB in size.

$ dd if=/dev/zero of=/data/dummy_file2 bs=1G count=1 oflag=dsync

Binary file is created and the snapshot volume space is around 20% which you can see under the "Data%" section.

$ ls -lh /opt/lvm_lab/ostechnix_sample.txt 
-rw-r--r-- 1 root root 1.0G Feb 19 15:46 /opt/lvm_lab/ostechnix_sample.txt
$ sudo lvs
  LV   VG            Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lab  ostechnix_lab owi-aos--- 5.00g                                                    
  snap ostechnix_lab swi-a-s--- 5.00g      lab    20.09    

To restore the LVM snapshot follow the below procedure. An important point to note is once the snapshot is restored the logical volume will be automatically removed.

STEP 1: Unmount the file system where the source logical volume is mounted.

$ sudo umount /opt/lvm_lab

STEP 2: Run the lvconvert command to initiate the restore process.

$ sudo lvconvert --merge /dev/ostechnix_lab/snap 
sudo lvconvert --merge /dev/ostechnix_lab/snap
  Merging of volume ostechnix_lab/snap started.
  ostechnix_lab/lab: Merged: 80.04%
  ostechnix_lab/lab: Merged: 80.42%
  ostechnix_lab/lab: Merged: 81.00%
  ostechnix_lab/lab: Merged: 81.43%
  ostechnix_lab/lab: Merged: 89.06%
  ostechnix_lab/lab: Merged: 98.04%
  ostechnix_lab/lab: Merged: 100.00%

You can run the lvs command and under the Attr section it will display "O" which means snapshot restore is in progress.

STEP 3: Once the merge process is complete the snapshot volume will be automatically removed. You can verify it by running the lvs command.

$ sudo lvs
  LV   VG            Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lab  ostechnix_lab -wi-ao---- 5.00g    

STEP 4: Mount the logical volume and check if the data is restored.

$ sudo mount /dev/ostechnix_lab/lab /opt/lvm_lab

LVM Snapshot Volume Sizing and Corruption

It is always recommended to allocate an equal amount of space to the snapshot volume considering the source logical volume will use its full space. In some cases without any forecasting, upfront snapshot volume will be allocated with less space than the logical volume. In such cases, if the source logical volume is fully utilized and snapshot volume will not have any space beyond the allocated space and will get corrupted.

If the snapshot volume is corrupted, it is of no use and we have to remove it and create a new snapshot volume.

To overcome this issue, you have three options.

  • Allocate the size of the snapshot volume and source volume equally.
  • Manually extend the snapshot volume size.
  • Automatically extend the snapshot volume size. This is the recommended method.

To extend the size of the logical volume manually run the following command. Here 3GB of size is added to the snapshot volume.

$ sudo lvextend -L +3G /dev/ostechnix_lab/snap

To extend the size automatically you have to edit the file "/etc/lvm/lvm.conf". You have to modify two important parameters.

The autoextend_threshold parameter will automatically extend the volume when a certain percentage of snapshot volume is filled. For example, if the threshold is set to 50% for 2GB volume, then after 1GB of space is filled the space will automatically be added.

In other words, if you have 2GB snapshot volume and the threshold is set to 50 which means if 50% of space is used which is 1GB, then auto extend should happen.

You have to set how much space should be added to the snapshot volume by setting the autoextend_percent parameter.

Set Autoextend_percent Parameter
Set Autoextend_percent Parameter

Conclusion

In this article, we have seen what is LVM snapshots, how to create new snapshot volumes and restore snapshot volumes. Finally, we have seen how to extend the snapshot volume in both manual and automatic ways.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More