Back-up of /home of a running system using lv snapshot

By | October 21, 2017

Following is a simple and easy way to do a consistent back-up of the /home directory (usually mounted in a separate partition) on a running system.
We do not have the luxury of being able to stop the operations of a server and unmount the home partition to make sure no changes are done during the back-up. To be able to pull this off we use the wonderful feature of logical volume snapshot.

STEP 1: Check the logical volume and physical volume status

# lvscan
  ACTIVE            '/dev/fedora/swap' [2.09 GiB] inherit
  ACTIVE            '/dev/fedora/home' [179.80 GiB] inherit
  ACTIVE            '/dev/fedora/root' [50.00 GiB] inherit

# pvscan
  PV /dev/sda2   VG fedora          lvm2 [231.88 GiB / 0.0 GiB free]
  Total: 1 [231.88 GiB] / in use: 1 [231.88 GiB] / in no VG: 0 [0   ]

STEP 2: Free some space on the physical volume
The problem now is the whole physical volume is filled and there is no free space for the logical volume snapshot. We can try to shrink some logical volume but both /dev/fedora/home and /dev/fedora/root are mounted and we cannot shrink a mounted partition.
The solution is the swap logical volume.

STEP 3: Shrink the swap space

First disable the swap

# swapoff /dev/fedora/swap

Then reduce the swap logical volume

lvreduce -L 1G /dev/fedora/swap

Format the new smaller swap

mkswap /dev/fedora/swap

Activate the new smaller swap

swapon -v /dev/fedora/swap

Check the new status of the physical volume

[root@localhost ~]# pvscan 
  PV /dev/sda2   VG fedora          lvm2 [231.88 GiB / 1.09 GiB free]
  Total: 1 [231.88 GiB] / in use: 1 [231.88 GiB] / in no VG: 0 [0   ]

STEP 4: Create the logical volume snapshot

We see that we have now more than 1GB free on the physical volume. As our “/dev/fedora/home” is relatively small and by choosing a down time when not a lot of IO operations are going on we hope we will not have more than 1GB of data written to /home during the back-up.
It is important that the size of the logical volume snapshot is bigger than the potential additional data that will be added to the original logical volume. If you feel that you need more space make sure to make more free space on the volume.

# lvcreate -s -L 1G -n snap /dev/fedora/home
  Using default stripesize 64.00 KiB.
  Logical volume "snap" created.

Check that the snapshot is created:

# lvscan
  ACTIVE            '/dev/fedora/swap' [1.00 GiB] inherit
  ACTIVE   Original '/dev/fedora/home' [179.80 GiB] inherit
  ACTIVE            '/dev/fedora/root' [50.00 GiB] inherit
  ACTIVE   Snapshot '/dev/fedora/snap' [1.00 GiB] inherit

STEP 5: Do the back-up

At this step just mount the new snapshot logical volume under /mnt and proceed with the back-up.
The back-up can be a simple copy to another disk, tape or network resource.

#mount /dev/fedora/snap /mnt

STEP 6: Clean-up

After the back-up is done we have to execute the clean-up.
Unmount the snapshot logical volume

#umount /mnt

Remove the snapshot:

# lvremove /dev/fedora/snap 
Do you really want to remove active logical volume fedora/snap? [y/n]: y
  Logical volume "snap" successfully removed

Note that the changes that happened on the original logical volume during the back-up were recorded on the snapshot.
By removing the snapshot we are applying from the snapshot the changes to the original logical volume and free the space allocated by the snapshot.

# lvscan
  ACTIVE            '/dev/fedora/swap' [1.00 GiB] inherit
  ACTIVE            '/dev/fedora/home' [179.80 GiB] inherit
  ACTIVE            '/dev/fedora/root' [50.00 GiB] inherit

# pvscan
  PV /dev/sda2   VG fedora          lvm2 [231.88 GiB / 1.09 GiB free]
  Total: 1 [231.88 GiB] / in use: 1 [231.88 GiB] / in no VG: 0 [0   ]

All done: we did a consistent back-up of the /home while the system is running. At this point we can even revert the swap space to its old size.

Contribute to this site maintenance !

This is a self hosted site, on own hardware and Internet connection. The old, down to earth way 🙂. If you think that you found something useful here please contribute. Choose the form below (default 1 EUR) or donate using Bitcoin (default 0.0001 BTC) using the QR code. Thank you !

€1.00

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.