Saturday, May 20, 2023

LVM - Restoring Files and Directories

If you run an AWS EC2 instance you can set up a policy that takes regular snapshots. For disaster recovery, it is pretty simple to convert a snapshot to a volume and attach it to your EC2 instance in place of the previously failed volume. It gets a little more complicated if you are trying to restore individual files or directories that are contained within logical volumes.

The first step is to convert your most recent snapshot into a volume and attach it to your EC2 instance. In general you should do this while your EC2 instance is running in order to avoid any confusion at startup. You should see a reference to the new volume in your kernel ring buffer shortly after it is attached. 

Because the volume group on your new volume has the same name and UUID as the volume group that is currently in use, you will not be able to restore files just yet. You will need to use the vgimportclone command to update the volume group name and UUID.

# vgimportclone <devicename>

Use the vgdisplay command to list your volume groups. You should now see a new volume group in the output which will need to be activated with the vgchange command. This should cause device nodes to show up in the /dev/mapper directory.

If you have used the xfs filesystem format on your logical volumes, your filesystems will have identical UUIDs to the ones currently mounted. There are ways of updating the xfs UUID, or you can pass the -o nouuid argument to the mount command.

Once you are done restoring files, unmount the volumes, deactivate the volume group with the vgchange command, detach the volume from your EC2 instance, and delete it.