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.

1

cp/etc/*/etc_dup/

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

[root@rhel1~]# df -h /etc_dup

Filesystem Size Used Avail Use%Mounted on

/dev/mapper/myvg-etc_dup

504M18M461M4%/etc_dup

[root@rhel1~]# ll /etc_dup

total1428

-rw-r--r--.1root root49Jan102:20adjtime

-rw-r--r--.1root root1512Jan102:20aliases

-rw-r--r--.1root root12288Jan102:20aliases.db

-rw-r--r--.1root root541Jan102:20anacrontab

-rw-r--r--.1root root148Jan102:20asound.conf

-rw-r--r--.1root root1Jan102:20at.deny

-rw-------.1root root232Jan102:20autofs_ldap_auth.conf

-rw-r--r--.1root root658Jan102:20auto.master

-rw-r--r--.1root root524Jan102:20auto.misc

-rwxr-xr-x.1root root1237Jan102:20auto.net

-rwxr-xr-x.1root root687Jan102:20auto.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.

1

2

3

4

[root@rhel1~]# vgs

VG#PV #LV #SN Attr VSize VFree

myvg210wz--n-2.99g1020.00m

vg_rhel1120wz--n-19.51g0

Step 2: Now create snapshot volume with size 50MB in order to store the snapshot of mount point “/etc_dup”

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.

1

2

3

4

5

6

7

[root@rhel1~]# lvs

LV VG Attr LSize Origin Snap%Move Log Copy%Convert

etc_dup myvg owi-ao512.00m

etc_snap myvg swi-a-52.00metc_dup0.02

lvol0 myvg-wi-a-1.99g

lv_root vg_rhel1-wi-ao15.57g

lv_swap vg_rhel1-wi-ao3.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”.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

[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

16G4.9G9.7G34%/

tmpfs1002M4.1M998M1%/dev/shm

/dev/sda1485M32M428M7%/boot

/dev/mapper/myvg-etc_dup

504M18M461M4%/etc_dup

/dev/mapper/myvg-etc_snap

504M18M461M4%/etc_dup_snap

[root@rhel1~]# cd /etc_dup_snap

[root@rhel1 etc_dup_snap]# ll

total1428

-rw-r--r--.1root root49Jan102:20adjtime

-rw-r--r--.1root root1512Jan102:20aliases

-rw-r--r--.1root root12288Jan102:20aliases.db

-rw-r--r--.1root root541Jan102:20anacrontab

-rw-r--r--.1root root148Jan102:20asound.conf

-rw-r--r--.1root root1Jan102:20at.deny

-rw-------.1root root232Jan102:20autofs_ldap_auth.conf

-rw-r--r--.1root root658Jan102:20auto.master

-rw-r--r--.1root root524Jan102:20auto.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”.

1

2

3

4

5

6

7

[root@rhel1/]# cd /etc_dup

[root@rhel1 etc_dup]# rm -f *

rm:cannot remove`lost+found':Isadirectory

[root@rhel1 etc_dup]# ll

total16

drwx------.2root root16384Jan102:19lost+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.

1

2

3

4

[root@rhel1~]# lvconvert --merge /dev/myvg/etc_snap

Can't merge over open origin volume

Can'tmerge when snapshot isopen

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”.

1

2

[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.

1

2

[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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

[root@rhel1~]# mount /dev/myvg/etc_dup /etc_dup

[root@rhel1~]# cd /etc_dup

[root@rhel1 etc_dup]# ll

total1428

-rw-r--r--.1root root49Jan102:20adjtime

-rw-r--r--.1root root1512Jan102:20aliases

-rw-r--r--.1root root12288Jan102:20aliases.db

-rw-r--r--.1root root541Jan102:20anacrontab

-rw-r--r--.1root root148Jan102:20asound.conf

-rw-r--r--.1root root1Jan102:20at.deny

-rw-------.1root root232Jan102:20autofs_ldap_auth.conf

-rw-r--r--.1root root658Jan102:20auto.master

-rw-r--r--.1root root524Jan102:20auto.misc

-rwxr-xr-x.1root root1237Jan102:20auto.net

-rwxr-xr-x.1root root687Jan102:20auto.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.

If this post helped you by any means, then Please
Support Us! by making Payment Payment Link.
Subscribe For RSS Feed::Here.
Like my facebook page FaceBook PageFollow Us on Twitter::TwitterFollow Us on YouTube::YouTube