How To Backup LVM Configuration On Linux (vgcfgbackup & vgcfgrestore)

Posted on : February 25, 2011 , Last Updated on : May 4, 2017 By
| Reply More

LVM BackupDue to several reason a volume group may become corrupted after unintended user intervention. LVM will not be aware of this situation unless the alternate is already on the volume group, and may allow to add the alternate path to a different volume group. Two of the useful commands provided as part of LVM are vgcfgbackup and vgcfgrestore. Theses commands allow you to back up the metadata of a volume group. This may not seem like much, but there are a few scenarios where having a backup of the volume group metadata can save some time and maybe sanity as well.

By default, the metadata backup is stored in the /etc/lvm/backup file and the metadata archives are stored in the /etc/lvm/archive file. At the top of each file it will tell you the time/data when the file was generated so chances are you'll have a copy of the older metadata as it was before the irreversible operations.

In many cases, lvm configuration gets corrupted. lvm could also be corrupted on some disk failures and on reboot OS fails to boot up. So it is important to have the lvm configuration backup.

See our lvm configuration below:

# pvs
 PV VG Fmt Attr PSize PFree 
 /dev/sda2 centos lvm2 a-- 19.51g 40.00m
 /dev/sdb vg-01 lvm2 a-- 15.00g 13.30g
 /dev/sdc vg-01 lvm2 a-- 20.00g 19.30g

Our volume group information:

root@centos7-srv admin]# vgs
VG #PV #LV #SN Attr VSize VFree 
centos 1 2 0 wz--n- 19.51g 40.00m
vg-01 2 3 0 wz--n- 34.99g 32.59g

Now, logical volume information:

# lvs
 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
 root centos -wi-ao---- 17.47g 
 swap centos -wi-ao---- 2.00g 
 lv_linear vg-01 -wi-a----- 1.00g 
 lv_mirror vg-01 rwi-a-r--- 200.00m 100.00 
 lv_stripe vg-01 -wi-a----- 1.00g

1. vgcfgbackup

Whenever you do any lvm activities like lvcreate, remove, disk add... should be updated in the respective volume group conf file under /etc/lvm/ directory. If disk fails we can restore the LVM configuration onto the disk using this backup configuration file. vgcfgbackup command takes the configuration metadata from the lvm header of the disk and save to a default file location /etc/lvm/backup/vg_name. By default, vgcfgbackup runs automatically each time, the lvm command changes the lvm configuration. It reads the LVM header details from system area of the disk and copies it to file. This file helps you to restore configuration on newly added disk in place of old disk which may have got corrupted or failed.

Note that a metadata backup does not back up the user and system data contained in the logical volumes. It is recommended to have this backup taken after every LVM level change. By default all LVM commands altering LVM details are designed to take this backup automatically hence manually running command is not necessary.

# vgcfgbackup [-f path_newfilename ] vg_name
  • vg_name: the path name of a volume group
  • -f path_newfilename:  save the configuration using the new filename specified at the path specified. If -f is omitted, the default file name is in the form of /etc/lvm/backup/vg_name where vg_name is the base name of vg_name. If you don't specified a new path, it will save the new file in the current directory.

Example:

# vgcfgbackup /dev/vg-01
 Volume group "vg-01" successfully backed up.

We can see the message our backup is a success. Let's see the backup folder

# ls -l /etc/lvm/backup/
total 8
-rw-------. 1 root root 1715 Apr 25 16:50 centos
-rw-------. 1 root root 4089 May 4 01:39 vg-01

The default filename is similar to our volume group name. Now, let's see what happen when we use -f option

# vgcfgbackup -f vg-01_bak /dev/vg-01
 Volume group "vg-01" successfully backed up.

It is a success. Let's check our file vg-01_bak in backup directory

# ls /etc/lvm/backup/
centos vg-01

Only the previous file exists. We don't see our file. Now let's check in the current directory

# ls -l
total 56392
-rwxrwxrwx. 1 admin admin 1975 Apr 25 17:01 manifest.txt
-rwxrwxrwx. 1 admin admin 2287 Apr 25 17:01 run_upgrader.sh
-rw-------. 1 root root 4074 May 4 02:05 vg-01_bak

You can see our file on the last line. Now let's retry the same command but we will specify the default backup folder.

# vgcfgbackup -f /etc/lvm/backup/vg-01_bak /dev/vg-01
 Volume group "vg-01" successfully backed up.

Now let's check.

# ls -l /etc/lvm/backup/
total 12
-rw-------. 1 root root 1715 Apr 25 16:50 centos
-rw-------. 1 root root 4089 May 4 01:39 vg-01
-rw-------. 1 root root 4090 May 4 02:26 vg-01_bak

2. vgcfgrestore

The vgcfgrestore command restores LVM volume group configuration from the archive to all the physical volumes in the volume groups. Restore cannot be performed if the volume group is activated in shared mode. Make sure you make a current copy of the existing working configuration using the vgcfgbackup command with the -f flag to specify a different file for the output so that you don't alter any files that are in the /etc/lvm/backup or /etc/lvm/archive folders. Make sure you diff the current configuration with the configuration you wish to restore to verify that the only changes you're about to apply are to revert the erroneous operations.

vgcfgrestore [-f|--file <filename>] [-t|--test] [-v] vg_name
  • vg_name: the name of the volume group corresponding
  • -f filename: indicates the name of LVM metadata backup file which specifies a metadata backup or archive file to be used for restoring the vg_name
  • --test: can be used to check the error of the command which can occur
  • -v: to have the detail of what currently happens

In the example below, we will simulate the deletion of a logical volume and we will try to restore it.

NB: The following example can be dangerous and destructive so be very careful and if possible have a full backup. Please make sure to try it on a test environment

step1:  Remove a logical volume

# lvremove /dev/vg-01/lv_linear 
Do you really want to remove active logical volume lv_linear? [y/n]: y
 Logical volume "lv_linear" successfully removed

Check

# lvs
 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
 root centos -wi-ao---- 17.47g 
 swap centos -wi-ao---- 2.00g 
 lv_mirror vg-01 rwi-a-r--- 200.00m 100.00 
 lv_stripe vg-01 -wi-a----- 1.00g

step 2: now look for the archive file at /etc/lvm/archive/vgname_(xxxxx).vg. Just looking the date that you have removed the logical volume. In our case it's the last file of the list

# ls -l /etc/lvm/archive/
total 28
-rw-------. 1 root root 1716 Apr 25 16:50 centos_00000-34432512.vg
-rw-------. 1 root root 1562 May 1 16:02 vg-01_00002-1413920221.vg
-rw-------. 1 root root 2009 May 1 16:07 vg-01_00003-718049637.vg
-rw-------. 1 root root 4097 May 4 03:51 vg-01_00004-306926489.vg

step 3:  verify the content of the file to check the time/data when the file was generated and the description which indicates action done before

# head /etc/lvm/archive/vg-01_00004-306926489.vg 
# Generated by LVM2 version 2.02.130(2)-RHEL7 (2015-10-14): Thu May 4 03:51:35 2017

contents = "Text Format Volume Group"
version = 1

description = "Created *before* executing 'lvremove /dev/vg-01/lv_linear'"

creation_host = "centos7-srv" # Linux centos7-srv 3.10.0-514.16.1.el7.x86_64 #1
creation_time = 1493866295 # Thu May 4 03:51:35 2017

You can see the description which indicates that the file was created before executing lvremove command.

step 4: Now recover it. You can test before recovering with --test option

# vgcfgrestore vg-01 -v -f /etc/lvm/archive/vg-01_00004-306926489.vg 
 Found same device /dev/sdb with same pvid 3vXbKwkq63cBiydWgQfkd2aaBeYo6b5I
 Found same device /dev/sdc with same pvid E9wih1232IjADxwkSwRC9kVV3VPJbD5O
 Restored volume group vg-01

step 5: Now we can check it

<# lvscan
 ACTIVE '/dev/centos/swap' [2.00 GiB] inherit
 ACTIVE '/dev/centos/root' [17.47 GiB] inherit
 inactive '/dev/vg-01/lv_linear' [1.00 GiB] inherit
 ACTIVE '/dev/vg-01/lv_stripe' [1.00 GiB] inherit
 ACTIVE '/dev/vg-01/lv_mirror' [200.00 MiB] inherit

You can see that our logical is not active in the third line. We will activate it and check again

# lvchange -ay /dev/vg-01/lv_linear
# lvscan | grep lv_linear
 ACTIVE '/dev/vg-01/lv_linear' [1.00 GiB] inherit

Conclusion

The commands vgcfgbackup and vgcfgrestore are useful commands to use when you need to restore volume group metadata. You can also use vgcfgrestore to restore an earlier version of the metadata.

Filed Under : HOWTOS, LINUX COMMANDS, LINUX HOWTO, LVM

Tagged With : ,

Free Linux Ebook to Download

Leave a Reply

All comments are subject to moderation.