X
    Categories: LinuxLVM

How to take LVM snapshot backup in LINUX

With the help of LVM snapshot backup we can freeze current state of an LVM volume. Making LVM snapshot backup make us to keep current state of volume and we can revert this state back if any needs arises during activity. A volume consists of two parts:  file system metadata and the actual blocks containing data in a file.

While creating LVM snapshot backup volume the file system metadata is copied to the newly created snapshot volume. The file blocks stay on the original volume, however, and as long as nothing has changed in the snapshot metadata, all pointers to the blocks on the original volume remain correct. When a file changes on the original volume, the original blocks are copied to the snapshot volume before the change is committed to the file system.

Scenario: For the demo of LVM snapshot backup , we have created one logical volume “etc_dup” in the volume group “myvg” and it been mounted on the mount point “/etc_dup” ,where we have copied sample /etc files using below command. To know more about creating volume click here.

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

So let’s start with LVM snapshot backup activity:

Step 1: Check overview of all volume group within system using vgs command.For our demo 50mb space is more than sufficient.

[root@rhel1 ~]# 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 create snapshot volume with size 50MB  in order to store the snapshot of mount point “/etc_dup”

[root@rhel1 ~]# 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
[root@rhel1 ~]#

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 verify logical volume using below command. you can see that in the below output our newly created snapshot volume is listed.

[root@rhel1 ~]# 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 directory “/etc_dup_snap” for mounting the snapshot volume and mount the snapshot volume over it. Now if you go to “/etc_dup_snap” location and you can see same contents as that of the mount point “/etc_dup”.

[root@rhel1 ~]# mkdir /etc_dup_snap
[root@rhel1 ~]# mount /dev/myvg/etc_snap /etc_dup_snap
[root@rhel1 ~]# 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
[root@rhel1 ~]# cd /etc_dup_snap
[root@rhel1 etc_dup_snap]# ll
total 1428
-rw-r--r--. 1 root root 49 Jan 1 02:20 adjtime
-rw-r--r--. 1 root root 1512 Jan 1 02:20 aliases
-rw-r--r--. 1 root root 12288 Jan 1 02:20 aliases.db
-rw-r--r--. 1 root root 541 Jan 1 02:20 anacrontab
-rw-r--r--. 1 root root 148 Jan 1 02:20 asound.conf
-rw-r--r--. 1 root root 1 Jan 1 02:20 at.deny
-rw-------. 1 root root 232 Jan 1 02:20 autofs_ldap_auth.conf
-rw-r--r--. 1 root root 658 Jan 1 02:20 auto.master
-rw-r--r--. 1 root root 524 Jan 1 02:20 auto.misc
---- OUTPUT TRUNCATED--------

Step 5: Now goto “/etc_dup” directory and remove all files within it using rm -f * command, don’t worry all our data is still intact in snapshot volume mount point “/etc_dup_snap”.

[root@rhel1 /]# cd /etc_dup
[root@rhel1 etc_dup]# rm -f *
rm: cannot remove `lost+found': Is a directory
[root@rhel1 etc_dup]# ll
total 16
drwx------. 2 root root 16384 Jan 1 02:19 lost+found
[root@rhel1 etc_dup]#

Now perform below list of step for merging the snapshot back to original volume which will revert back all data.

Step 6:  Merge the snapshot volume using below command.

[root@rhel1 ~]# 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 error message in the above output.

Step 7: Now unmount “/etc_dup” and “/etc_dup_snap”.

[root@rhel1 ~]# umount /etc_dup_snap
[root@rhel1 ~]# umount /etc_dup

Step 8: Now deactivate and activate original volume. This step is necessary for merging the snapshot back into the original volume.

[root@rhel1 ~]# lvchange -a n /dev/myvg/etc_dup
[root@rhel1 ~]# lvchange -a y /dev/myvg/etc_dup

Step 9: Mount the “/etc_dup” mount point  and recheck its content using ll command as below:

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

You can see in the above output that all our original contents are back which means that we have successfully performed LVM snapshot backup activity.

NOTE: You don’t need to remove the LVM snapshot backup. By converting the snapshot back into the original volume, you have automatically removed the snapshot volume.

View Comments (0)

Related Post