Some times system admin mistakenly delete LVM partitions while working on production servers. By using vgcfgrestore command we can recover deleted LVM partitions.
How it works:
Linux server store the lvm configuration backup copies in the /etc/lvm/archive directory. Here I have created the new 2G logical volume and deleted the same for pratical example.
Step 1: Create the physical volume (PV), extend the volume group (VG) and create new logical volume (LVM)
[root@sclient ~]# fdisk -l | grep /dev/sd
Disk /dev/sda: 8589 MB, 8589934592 bytes
/dev/sda1 * 1 64 512000 83 Linux
/dev/sda2 64 1045 7875584 8e Linux LVM
[root@sclient ~]# echo "- - -" > /sys/class/scsi_host/host0/scan
[root@sclient ~]# echo "- - -" > /sys/class/scsi_host/host1/scan
[root@sclient ~]# echo "- - -" > /sys/class/scsi_host/host2/scan
[root@sclient ~]# fdisk -l | grep /dev/sd
Disk /dev/sda: 8589 MB, 8589934592 bytes
/dev/sda1 * 1 64 512000 83 Linux
/dev/sda2 64 1045 7875584 8e Linux LVM
Disk /dev/sdb: 5368 MB, 5368709120 bytes
[root@sclient ~]# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created
[root@sclient ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup 1 2 0 wz--n- 7.51g 0
[root@sclient ~]# vgextend VolGroup /dev/sdb
Volume group "VolGroup" successfully extended
[root@sclient ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup 2 2 0 wz--n- 12.50g 5.00g
[root@sclient ~]# lvcreate -L 2G -n lvtest VolGroup
Logical volume "lvtest" created.
[root@sclient ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_root VolGroup -wi-ao---- 6.71g
lv_swap VolGroup -wi-ao---- 816.00m
lvtest VolGroup -wi-a----- 2.00g
[root@sclient ~]# mkdir -p /data
[root@sclient ~]# mkfs.ext4 /dev/VolGroup/lvtest
mke2fs 1.43-WIP (20-Jun-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
[root@sclient ~]# mount -t ext4 /dev/VolGroup/lvtest /data
[root@sclient ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
6.5G 1.8G 4.5G 28% /
tmpfs 242M 0 242M 0% /dev/shm
/dev/sda1 477M 66M 382M 15% /boot
/dev/mapper/VolGroup-lvtest
2.0G 3.0M 1.8G 1% /data
[root@sclient ~]# cp /etc/passwd /etc/group /data
[root@sclient ~]# ls -l /data/
total 24
-rw-r--r-- 1 root root 495 Sep 11 22:57 group
drwx------ 2 root root 16384 Sep 11 22:57 lost+found
-rw-r--r-- 1 root root 1080 Sep 11 22:57 passwd
Step 2: Remove the newly created logical volume
Below command is not reccommended to exectue in Prodcution Linux Server, be careful while executing these commands.
[root@sclient ~]# umount /data
[root@sclient ~]# lvremove /dev/VolGroup/lvtest
Logical volume "lvtest" successfully removed
Now we have removed the newly created locical volume, at this moment the data’s in the volume has destroyed.
[root@sclient ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_root VolGroup -wi-ao---- 6.71g
lv_swap VolGroup -wi-ao---- 816.00m
Step 3: Recover the Logical Volume by using vgcfgrestore command
Whenever logical volume group get modified the linux server will take the backup of existing configuration and save it in /etc/lvm/archive directory.
[root@sclient ~]# vgcfgrestore --list VolGroup
File: /etc/lvm/archive/VolGroup_00000-304429941.vg
VG name: VolGroup
Description: Created *before* executing '/sbin/vgs --noheadings -o name --ignoreskippedcluster --config 'log{command_names=0 prefix=" "}''
Backup Time: Sun Jul 9 03:23:31 2017
File: /etc/lvm/archive/VolGroup_00001-1788852006.vg
VG name: VolGroup
Description: Created *before* executing 'lvcreate -L 2G -n lvtest VolGroup'
Backup Time: Mon Sep 11 22:55:26 2017
File: /etc/lvm/archive/VolGroup_00003-2103841493.vg
VG name: VolGroup
Description: Created *before* executing 'lvremove /dev/VolGroup/lvtest'
Backup Time: Mon Sep 11 23:04:52 2017
File: /etc/lvm/backup/VolGroup
VG name: VolGroup
Description: Created *after* executing 'lvremove /dev/VolGroup/lvtest'
Backup Time: Mon Sep 11 23:04:53 2017
In above example, linux server took the lvm configuration backup and save it in file /etc/lvm/archive/VolGroup_00003-2103841493.vg. This file is human readable in text format.
Below command will help us to recover the deleted logical volume
[root@sclient ~]# vgcfgrestore -f /etc/lvm/archive/VolGroup_00003-2103841493.vg VolGroup
Restored volume group VolGroup
where,
/etc/lvm/archive/VolGroup_00003-2103841493.vg – LVM configuration backup file name
VolGroup – Volume group
[root@sclient ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_root VolGroup -wi-ao---- 6.71g
lv_swap VolGroup -wi-ao---- 816.00m
lvtest VolGroup -wi------- 2.00g
Now it’s a time to mount the logical volume and see the exisiting data’s.
[root@sclient ~]# mount -t ext4 /dev/VolGroup/lvtest /data
mount: special device /dev/VolGroup/lvtest does not exist
Ohhhhhhh What happened?????? We are not able to mount the logical volume because recovered volume is not activated at this moment. We need to activate the volumes before mounting to system. Below command will help us to activate the volumes.
[root@sclient ~]# lvchange -ay /dev/VolGroup/lvtest
[root@sclient ~]# mount -t ext4 /dev/VolGroup/lvtest /data
[root@sclient ~]# ls -l /data
total 24
-rw-r--r-- 1 root root 495 Sep 11 22:57 group
drwx------ 2 root root 16384 Sep 11 22:57 lost+found
-rw-r--r-- 1 root root 1080 Sep 11 22:57 passwd