How to Use LVM Snapshot to Backup Data in Linux

Are you looking for a guide on how to use LVM snapshot to backup data in Linux OS? Here’s everything you need to know. 

LVM stands for logical volume manager. It’s used in Linux to manage hard drives and storage devices. The advantage of LVM is that it works on all Linux platforms. So the steps discussed below apply to various distributions of Linux.  We will discuss the main steps to use LVM snapshot to backup data in Linux. 

Use LVM Snapshot to Backup Data – Installation

The first step is to install the LVM on the specified distribution. Depending upon the distribution, you may be required to perform different steps. The following paragraphs discuss the installation of major platforms. 

Installation on Ubuntu, Debian, and Linux Mint

For Ubuntu, Debian, or Linux Mint, use the following command to install the LVM:

$ sudo apt install lvm2


Installation on CentOS, Fedora, AlmaLinux, and Red Hat

For installation of LVM on CentOS, Fedora, AlmaLinux, and Red Hat, use the following command:

$ sudo dnf install lvm2


Installation on Arch Linux and Manjaro

For installation on Arch Linux and Manjaro, use the following command:

$ sudo pacman -S lvm2


Now we will discuss the actual steps of creating a snapshot and restoring it using LVM.

Creating partitions

The next step is to create partitions. Suppose we are working with /dev/sdb. You can see the details of the disk with the help of the following command:

# fdisk –l

Partition the disk

Now, partition the disk with the following command:

# cfdisk /dev/sdb

Using the interface as shown below, you will be able to create two partitions.

create two partitions

Verify the created partitions

Now, finalize the changes. Choose “Write” and “Exit” to exit the utility. Use the following command to see the disks/ created partitions:

$fdisk -l


Create physical volumes

Now, follow the steps below to create and verify physical volumes when you use LVM snapshot to backup data.

Creating physical volumes

We will create the physical volumes on a new partition by using the following command:

# pvcreate /dev/sdb1
# pvcreate /dev/sdb2


You will get the following messages for each of the above commands:

Physical volume “/dev/sdb1” successfully created.

Physical volume “/dev/sdb2” successfully created


Verify that the physical volumes are created

You can get information about physical volumes with the help of the following command:

# pvdisplay

Creating a volume group

We will create a volume group now to use LVM snapshot to backup data. This will serve as a container for the created physical volumes. 

Creating volume group

The following command will create a volume group “mynew_vg” that includes the /dev/sdb1 partition:

# vgcreate mynew_vg /dev/sdb1 

Instead of having one partition, you can consist of both the partition with the help of the following command:

# vgcreate mynew_vg /dev/sdb1 /dev/sdb2


Verifying volume group

Now you can see the volume group details with the help of the following command:

# vgdisplay


You can also extend the volume group with more physical volumes by using the following command:

# vgextend mynew_vg /dev/sdb2

Creating logical volume

Our volume group is like a big cake; now, we will cut different pieces (logical volume), which will be like partitions. 

Creating logical volume

To create a logical volume vol01 of size 400 MB, use the following command:

# lvcreate -L 400 -n vol01 mynew_vg


Let’s create another volume of size 1GB using the following command:

# lvcreate -L 400 -n vol01 mynew_vg


Verifying logical volume

To see the list of the logical volume, you can use the following command:

#Lvdisplay

Creating a file system on the logical volume

Now, before making the logical volume ready to use, create a filesystem using the following command:

# mkfs.ext4 -m 0 /dev/mynew_vg/vol01


Here, we have used –m to specify the percentage of volume to be used for the super user. 

Logical volume snapshot

Now, we will take a snapshot of the logical volume created earlier. Follow the steps below:

Create a mount point

First, create a mount point and mount the logical volume using the following command:

$ sudo mkdir -p /mnt/volume1
$ sudo mount /dev/mynew_vg/vol01 /mnt/volume1

Write some data on the logical volume

We will put some data in the logical volume. In this way, we can verify if the backup and restore processes have worked. The following command copies the /usr/bin directory to the volume:

$ sudo cp -av /usr/bin/* /mnt/volume1


Now, use the du command to check the size of the directory:

$ du -s /usr/bin

Create a snapshot of the volume

Now, create a snapshot of the logical volume “Volume 1”. The LVM will create a new separate logical volume. Use the command below:

$ sudo lvcreate -s -L 20M -n volume1_snapshot /dev/mynew_vg/vol01

Change the data in the logical volume

After the snapshot has been created, let’s remove some of the files from the logical volume using the following command:

$ sudo rm -rf /mnt/volume1/*

Recover the snapshot

At this point, we can recover the data from the snapshot using the following command:

$ sudo lvconvert --merge /dev/mynew_vg/vol01/volume1_snapshot


Once the above command is executed, the logical volume will roll back. 

Unmount the volume

We need to reactive the volume again. For this purpose, unmount the volume we are attempting to recover using the following command:

$ sudo umount /mnt/volume1


Deactivate and activate the volume

We can deactivate and activate the volume with the help of the following command:

$ sudo lvchange -a n /dev/mynew_vg/vol01
$ sudo lvchange -a y /dev/mynew_vg/vol01


Mount the volume again

Now mount the volume again and confirm that data has been restored with the help of the following commands:

$ sudo mount /dev/mynew_vg/vol01 /mnt/volume1
$ du -s /mnt/volume1


You can verify that the directory size is the same as reported earlier.

A word of caution

LVM snapshots are actually intended for backup. Instead, it captures the filesystem in a frozen state. However, they can be used for backup as the frozen images are not changed during the backup process. However, note the following things:

  • Snapshots don’t last forever.
  • A full snapshot is generally not recommended.
  • Snapshots need to be released at some point.
  • Use LVM wisely if you intend to use it for backup.

This article discusses the significant steps to use LVM snapshot to backup data in Linux. As an administrator, LVM snapshots are handy. However, certain limitations must be considered before using LVM snapshots for backup.

If this guide helped you, please share it. 

Related Posts