Logical Volume Manager (LVM) is used on Linux to manage hard drives and other storage devices. As the name implies, it can sort raw storage into logical volumes, making it easy to configure and use.
In this tutorial, you will learn how to create LVM backup and restore in Linux system. LVM works the same on any Linux distribution, so you can use any of the commands below on your own system.
Follow along with us as we use LVM to create partitions, physical volumes, a volume group, logical volumes, and filesystems on a hard disk. We’ll also show how to mount, extend, and remove our newly created logical volumes. At the end of this tutorial, we will take a snapshot of the volume, and then show how to restore the backup.
In this tutorial you will learn:
- How to create a logical volume in LVM
- How to create a snapshot of a logical volume
- How logical volume snapshots work
- How to restore a backup of a logical volume
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Software||Logical Volume Manager (LVM)|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
LVM backup and restore in Linux step by step
Install LVM on major Linux distros
Your Linux system may already have LVM installed, but it doesn’t come installed by default on every distro. Use the appropriate command below to install LVM with your system’s package manager.
$ sudo apt install lvm2
$ sudo dnf install lvm2
$ sudo pacman -S lvm2
The first thing we will do is create partitions on our disk. This is to facilitate the creation of physical volumes in the next section, which can either be created on raw, unpartitioned block devices or single partitions. For the sake of this tutorial, we will work on the latter.
For this example, the disk we’ll be working with is
/dev/sdb, which is a 5GB (and currently unpartitioned) hard disk.
We can see our
/dev/sdb disk and its pertinent details with the following command.
# fdisk -l
Next, let’s partition the disk with
# cfdisk /dev/sdb
An interface will open in your console, which is quite intuitive to use. We’ve created the following two partitions as an example.
Finalize your changes by choosing “write,” then exit the utility when done. We can now see our partition listed when we execute
fdisk -l again.
Create physical volumes
We can now create physical volumes on our new partitions by using the
# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created. # pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created.
pvdisplay command to see information about all the physical volumes on your system, or specify a particular volume you wish to view details about.
# pvdisplay OR # pvdisplay /dev/sdb1
Create a volume group
At this stage we need to create a volume group which will serve as a container for our physical volumes. In this example, we’ll call our volume group “mynew_vg” which will include the
/dev/sdb1partition, with the following Linux command:
# vgcreate mynew_vg /dev/sdb1
Or, to include both partitions at once:
# vgcreate mynew_vg /dev/sdb1 /dev/sdb2
Use the following command to display information about the volume group(s).
We can add more physical volumes to the group by using the
# vgextend mynew_vg /dev/sdb2 Volume group "mynew_vg" successfully extended
Create logical volumes
Now we can move on to creating logical volumes. It may help to think of our volume group as a “big cake,” from which we can cut “pieces” (logical volumes) that will get treated as partitions on our Linux system.
The following command will create a logical volume named
vol01 with a size of 400MB.
# lvcreate -L 400 -n vol01 mynew_vg
Then, we’ll create another volume named
vol02 with a size of 1GB.
# lvcreate -L 1000 -n vol02 mynew_vg
Finally, we can use the
lvdisplay command to see the logical volumes we’ve just created.
Create a filesystem on logical volumes
The logical volume is almost ready to use. All we need to do is to create a filesystem on it with the
# mkfs.ext4 -m 0 /dev/mynew_vg/vol01
-m option specifies the percentage reserved for the super-user, we can set this to 0 to use all the available space (the default is 5%).
Logical Volume Snapshot
Finally, we have come to the point where we can take a snapshot of our logical volume created in the previous sections. For this we will also need some sample data on our Logical Volume “volume1” so once we revert from the snapshot we can confirm the entire process by comparing original data with data recovered from the snapshot.
In order to understand how snapshots work, we first need to understand what logical volume consists of and how data is stored. This concept is similar to well known symbolic links. When you create a symbolic link to a file, you are not creating a copy of the actual file but instead you simply create only a reference to it. Logical volume stores data in a similar fashion and it consists of two essential parts:
- metadata pointers
- data block
When a snapshot is created, Logical Volume Manager simply creates a copy of all Metadata pointers to a separate logical volume. Metadata does not consume much space and therefore you are able to create a snapshot of, let’s say 2GB logical volume to 5MB snapshot volume.
The snapshot volume only starts to grow once you start altering data of the original logical volume. Which means, that every time you remove or edit a file on the original logical volume, a copy of that file (data) is created on the snapshot volume. For a simple change, you may need to create a snapshot volume of around 5-10% of the logical volume original size. If you are prepared to make many changes on your original logical volume then you will need a lot more than 10%.
LVM backup and restore in Linux from Logical Volume Snapshot
Now we are ready to get started. Follow the step by step instructions below to create LVM backup and restore in Linux from the logical volume snapshot.
- First, we will create a new mount point for our logical volume, and then mount the volume.
$ sudo mkdir -p /mnt/volume1 $ sudo mount /dev/mynew_vg/vol01 /mnt/volume1
- As mentioned previously, we will need to put some sample data on our logical volume. That way, we can verify that a backup and restore was successful later on. In this step we will simply copy the
/usr/bindirectory to the volume.
$ sudo cp -av /usr/bin/* /mnt/volume1 $ du -s /usr/bin 162528
As seen in the output of the
ducommand above, the size of your
/usr/bindirectory is 162528 bytes in total size. We will use this information for verification that our backup and restore was successfull later.
- Now we are going to create a snapshot of logical volume “volume1”. In the process, Logical Volume Manager will create a new separate logical volume. This new logical volume will have size of 20MB and will be called “volume1_snapshot”:
$ sudo lvcreate -s -L 20M -n volume1_snapshot /dev/mynew_vg/vol01 Logical volume "volume1_snapshot" created
- Now that the snapshot has been created, we can start altering data on “volume1” for example by removing the entire content we copied earlier:
$ sudo rm -rf /mnt/volume1/*
- Next, we use the following
lvconvertcommand to recover our snapshot.
$ sudo lvconvert --merge /dev/mynew_vg/vol01/volume1_snapshot Can't merge over open origin volume Merging of snapshot volume1_snapshot will start next activation.
- After execution of the above command, the logical volume “volume1” will rollback once it is activated. Therefore, what needs to be done next is to re-activate “volume1”. First, make sure that you unmount your the volume we are trying to recover.
$ sudo umount /mnt/volume1
- Now we can deactivate and activate the volume:
$ sudo lvchange -a n /dev/mynew_vg/vol01 $ sudo lvchange -a y /dev/mynew_vg/vol01
- For the last step, mount again your logical volume “volume1” and confirm that all of the deleted data has been recovered.
$ sudo mount /dev/mynew_vg/vol01 /mnt/volume1 $ du -s /mnt/volume1 162528
As evidenced in this output, our logical volume was completely restored to its state at the time of the snapshot.
The above was a basic example of snapshot manipulation using Logical Volume Manager and how to perform LVM backup and restore in Linux. The usefulness of logical volume snapshots is enormous and it will sure help you with your tasks whether you are system administrator or a developer. Although you can use the setup above to create multiple snapshots for a backup recovery, you also need to know that your backup will find its limits within your Logical Volume Group. Therefore, any low level physical volume problems may render your snapshot useless.