How to back up an LVM snapshot on Linux

With an LVM snapshot backup, we can freeze the current state of the LVM volume. We will create a snapshot of the LVM backup to save the current state of the volume, and we can return to this state back if any need arises during the activity. The procedure consists of two parts: the file system metadata and the actual blocks containing the data in the file.

When you create an LVM snapshot, the file system metadata backup volume is copied to the volume snapshot you just created. File blocks remain on the original volume, however, as long as nothing is changed in the snapshot metadata, all pointers in the original size blocks remain correct. When a file is modified on the original volume, the original blocks are copied to a snapshot of the volume before being modified in the file system.

Scenario: For demonstration backup LVM snapshot, we created one logical volume “etc_dup” in the volume group “myvg” and it was mounted on the mount point “/ etc_dup” where we copied the image of the files ‘/ etc’ using the following command.

cp /etc/* /etc_dup/
[[email protected] ~]# df -h /etc_dup
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/myvg-etc_dup
504M 18M 461M 4% /etc_dup
[[email protected] ~]# ll /etc_dup
total 1428
-rw-r--r--. 1 root root 49 Mar 4 12:11 adjtime
-rw-r--r--. 1 root root 1512 Mar 4 12:11 aliases
-rw-r--r--. 1 root root 12288 Mar 4 12:11 aliases.db
-rw-r--r--. 1 root root 541 Mar 4 12:11 anacrontab
-rw-r--r--. 1 root root 148 Mar 4 12:11 asound.conf
-rw-r--r--. 1 root root 1 Mar 4 12:11 at.deny
-rw-------. 1 root root 232 Mar 4 12:11 autofs_ldap_auth.conf
-rw-r--r--. 1 root root 658 Mar 4 12:11 auto.master
-rw-r--r--. 1 root root 524 Mar 4 12:11 auto.misc
-rwxr-xr-x. 1 root root 1237 Mar 4 12:11 auto.net
-rwxr-xr-x. 1 root root 687 Mar 4 12:11 auto.smb
------ OUTPUT TRUNCATED-------------------

So let’s start with an LVM snapshot backup operation:

Step 1: Check an overview of the entire volume group on the system using the vgs command. For our demo 50mb space is more than enough.

[[email protected] ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 2 1 0 wz--n- 2.99g 1020.00m
vg_rhel1 1 2 0 wz--n- 19.51g 0

Step 2: Now take a snapshot of the volume with a size of 50MB to save a snapshot of the mount point “/ etc_dup”

[[email protected] ~]# lvcreate -s -L 50M -n etc_snap /dev/myvg/etc_dup
Rounding up size to full physical extent 52.00 MiB
Logical volume "etc_snap" created
[[email protected] ~]#

In the above command:

-s --> creates snapshot logical volume
 
-L --> creates 50 Mb sized LV.
 
/dev/myvg/etc_dup is the logical volume which is mounted on "/etc_dup" for which we are taking LVM snapshot backup.

Step 3: You can check the logical volume with the command below, you can see that the example below shows our newly created volume snapshot.

[[email protected] ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
etc_dup myvg owi-ao 512.00m
etc_snap myvg swi-a- 52.00m etc_dup 0.02
lvol0 myvg -wi-a- 1.99g
lv_root vg_rhel1 -wi-ao 15.57g
lv_swap vg_rhel1 -wi-ao 3.94g

Step 4: Create a directory “/ etc_dup_snap” to mount the snapshot volume and set the snapshot volume above it. Now, if you go to the location of / etc_dup_snap, you can see the same content as in the mount points of / etc_dup.

[[email protected] ~]# mkdir /etc_dup_snap
[[email protected] ~]# mount /dev/myvg/etc_snap /etc_dup_snap
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_rhel1-lv_root
16G 4.9G 9.7G 34% /
tmpfs 1002M 4.1M 998M 1% /dev/shm
/dev/sda1 485M 32M 428M 7% /boot
/dev/mapper/myvg-etc_dup
504M 18M 461M 4% /etc_dup
/dev/mapper/myvg-etc_snap
504M 18M 461M 4% /etc_dup_snap
[[email protected] ~]# cd /etc_dup_snap
[[email protected] etc_dup_snap]# ll
total 1428
-rw-r--r--. 1 root root 49 Mar 4 12:11 adjtime
-rw-r--r--. 1 root root 1512 Mar 4 12:11 aliases
-rw-r--r--. 1 root root 12288 Mar 4 12:11 aliases.db
-rw-r--r--. 1 root root 541 Mar 4 12:11 anacrontab
-rw-r--r--. 1 root root 148 Mar 4 12:11 asound.conf
-rw-r--r--. 1 root root 1 Mar 4 12:11 at.deny
-rw-------. 1 root root 232 Mar 4 12:11 autofs_ldap_auth.conf
-rw-r--r--. 1 root root 658 Mar 4 12:11 auto.master
-rw-r--r--. 1 root root 524 Mar 4 12:11 auto.misc
---- OUTPUT TRUNCATED--------

Step 5: Now go to the “/ etc_dup” directory and delete all files in it with the rm -f * command, don’t worry, all our data will still be intact in the snapshot of the “/ etc_dup_snap” mount point.

[[email protected] /]# cd /etc_dup
[[email protected] etc_dup]# rm -f *
rm: cannot remove `lost+found': Is a directory
[[email protected] etc_dup]# ll
total 16
drwx------. 2 root root 16384 Mar 4 12:12 lost+found
[[email protected] etc_dup]#

Now follow the step to combine the snapshots back to the original volume, which will bring all the data back.

Step 6: Merge the volume snapshot using the following command.

[[email protected] ~]# lvconvert --merge /dev/myvg/etc_snap
Can't merge over open origin volume
Can't merge when snapshot is open
Merging of snapshot etc_snap will start next activation.

You can safely ignore the error message in the output above.

Step 7: Now unmount / etc_dup and / etc_dup_snap.

[[email protected] ~]# umount /etc_dup_snap
[[email protected] ~]# umount /etc_dup

Step 8: Now deactivate and activate the original volume. This step is necessary to merge the images back to their original volume.

[[email protected] ~]# lvchange -a n /dev/myvg/etc_dup
[[email protected] ~]# lvchange -a y /dev/myvg/etc_dup

Step 9: Set the mount point / etc_dup and recheck its contents with the ll command as shown below:

[[email protected] ~]# mount /dev/myvg/etc_dup /etc_dup
[[email protected] ~]# cd /etc_dup
[[email protected] etc_dup]# ll
total 1428
-rw-r--r--. 1 root root 49 Mar 4 12:11 adjtime
-rw-r--r--. 1 root root 1512 Mar 4 12:11 aliases
-rw-r--r--. 1 root root 12288 Mar 4 12:11 aliases.db
-rw-r--r--. 1 root root 541 Mar 4 12:11 anacrontab
-rw-r--r--. 1 root root 148 Mar 4 12:11 asound.conf
-rw-r--r--. 1 root root 1 Mar 4 12:11 at.deny
-rw-------. 1 root root 232 Mar 4 12:11 autofs_ldap_auth.conf
-rw-r--r--. 1 root root 658 Mar 4 12:11 auto.master
-rw-r--r--. 1 root root 524 Mar 4 12:11 auto.misc
-rwxr-xr-x. 1 root root 1237 Mar 4 12:11 auto.net
-rwxr-xr-x. 1 root root 687 Mar 4 12:11 auto.smb
---- OUTPUT TRUNCATED--------

You can see in the above output that all of our original content is in place, which means that we have successfully performed an LVM snapshot operation.

Note: You do not need to delete the LVM snapshot backup. By converting the snapshot back to the original volume, you automatically delete the snapshot volume.

Sidebar