Have you ever deleted a logical volume by accident? Can you recover it looking into the backups? Well, the answer is YES. For those who are not familiar with Logical Volume Management (LVM) is a device mapper target that provides logical volume management for the Linux kernel.- Wikipedia. It is an abstraction layer or software that has been placed on top of your hard drive for flexible manipulation of the disk space. Some of the articles published in the past on LVM are:
- Add and Extend disk on Virtual Box through LVM
- Move your /home to a new LVM partition
- Part 1 on Managing LVM with PVMOVE
- Part2 on Managing LVM with PVMOVE
- Ansible playbook to configure LVM
All test carried out on this blog post have been tested on a CentOS machine. Please don't make a live test on a production server.
1. So, as you can see below I have an lv called lvopt which is from a vg called centos.
2. Same is mounted on the /opt
3. There are some data in that partition as well:
4. I created a directory inside the /opt directory
5. Now, let's pretend to remove the lvm lvopt. Or say, someone did it by accident because it was unmounted. The command lvremove will be used here to remove the lv. Note: that the lv need to be unmounted.
6. If you make an lvs, lvdisplay or vgs or even mount again the partition, you cannot do it. The data is lost. But you can still recover it. This is because the lvm contains the archive of your lv inside the folder /etc/lvm/archive. But, you cannot read the files directly.
7. But you can still, interpret part of the files. Since we deleted the volume group called "centos", we knew that it is referenced in the file centos_... The question that arises here is which file is relevant for you. Right? So to understand which archive you want to restore, you need to use the command vgcfgrestore --list <name of volume group>. Here is an example:
8. If you observe carefully, each archive has been backup at a certain time. In my case, I deleted the LV on 18-Apr-2019 at 11:17:17 2019:
9. So, I want to restore from that last archive. You will need to copy the full patch of the vg file. In my case it is /etc/lvm/archive/centos_00004-1870674349.vg. The goal here is to restore the lv before this specific time, or simply restore back the lv before the command lvremove was fired. Here is the command:
10. If you launch the command lvs, you will notice the presence of the lv.
11. But, mounting back the lv won't result in anything. This is because the lv is inactive. You can see it with the command lvscan. Please take note below that the lvopt is inactive.
12. To activate it you simply need to use the command lvchange.
13. Mount it back and you are done.
I believe this can be very useful especially when you have encountered a situation where someone deleted an lv. I hope you enjoy this blog post. Please share and comment below if you like it.