How to backup your Linux system with LVM snapshots

Posted by Luke on Fri, 21 Feb 2020 10:11:00

Since I received my Pinebook Pro it happened to me a few times that, after I upgraded my Manjaro ARM Linux on it, I was left with a crippled system. So I had to come up with some backup strategy before I upgrade.

Since I have the system (root filesystem) on luks partition with LVM, it dawned to me that I can use LVM snaphots for this.

So let’s see if we have enough place in volume group for such an operation:

[root@pbp ~]# vgdisplay
— Volume group —
VG Name vg0
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 12
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 4
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size <893.75 GiB
PE Size 4.00 MiB
Total PE 228799
Alloc PE / Size 134081 / 523.75 GiB
Free PE / Size 94718 / 369.99 GiB

There is almost 370GB of free space in a volume group vg0. So let’s see how big root volume is:

[root@pbp ~]# lvdisplay
— Logical volume —
LV Path /dev/vg0/rootlv
LV Name rootlv
VG Name vg0LV UUID xyZeBw-Pltj-3wXG-DyfS-fc7o-lxrc-qQyue
LV Write Access read/write
LV Creation host, time pbp-sd, 2020-02-14 13:48:33 +0100
LV snapshot status source of
rootsnap [active]
LV Status available
# open 1
LV Size 30.00 GiB
Current LE 7680
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:2

Root volume is 30GB so there is more than enough space in volume group to create a snapshot:

[root@pbp ~]# lvcreate -s -n rootsnapshot -L 30G /dev/mapper/vg0-rootlv
Logical volume “rootsnapshot” created.

Check if everything went ok. Notice “Original” and “Snapshot” labels:

[root@pbp ~]# lvscan
ACTIVE Original ‘/dev/vg0/rootlv’ [30.00 GiB] inherit
ACTIVE ‘/dev/vg0/swaplv’ [4.50 GiB] inherit
ACTIVE ‘/dev/vg0/homelv’ [459.25 GiB] inherit
ACTIVE Snapshot ‘/dev/vg0/rootsnapshot’ [30.00 GiB] inherit

You can also see how much changes have been written to original volume since snapshot was created (0.34%):

[root@pbp ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
homelv vg0 -wi-ao—- 459.25g
rootlv vg0 owi-aos— 30.00g
rootsnapshot vg0 swi-a-s— 30.00g rootlv 0.34
swaplv vg0 -wi-ao—- 4.50g

The snapshot volume can be mounted, as any other device, to access original files and optionally make a tar backup and transfer it outside of your computer:

[root@pbp ~]# mount /dev/mapper/vg0-rootsnapshot /mnt/rootsnapshot/

[root@pbp ~]# ls -l /mnt/rootsnapshot/
total 76
lrwxrwxrwx 1 root root 7 Apr 16 2019 bin -> usr/bin
drwxr-xr-x 2 root root 4096 Feb 14 14:07 boot
drwxr-xr-x 2 root root 4096 Feb 14 13:53 dev
drwxr-xr-x 70 root root 4096 Feb 21 16:34 etc
drwxr-xr-x 3 root root 4096 Dec 4 20:30 home
lrwxrwxrwx 1 root root 7 Apr 16 2019 lib -> usr/lib
drwx—— 2 root root 16384 Dec 9 17:45 lost+found
drwxr-xr-x 4 root root 4096 Feb 19 14:48 mnt
drwxr-xr-x 2 root root 4096 Apr 16 2019 opt
-rw-r–r– 1 root root 44 Dec 9 13:46 overlay.txt
drwxr-xr-x 2 root root 4096 Feb 14 13:53 proc
drwxr-x— 18 root root 4096 Feb 19 10:22 root
drwxr-xr-x 24 root root 4096 Feb 14 07:57 run
lrwxrwxrwx 1 root root 7 Apr 16 2019 sbin -> usr/bin
drwxr-xr-x 4 root root 4096 Oct 30 12:08 srv
drwxr-xr-x 2 root root 4096 Feb 14 13:53 sys
drwxrwxrwt 30 root root 4096 Feb 9 14:01 tmp
drwxr-xr-x 9 root root 4096 Feb 20 13:01 usr
drwxr-xr-x 12 root root 4096 Feb 20 13:06 var

Now, how to recover in case your upgrade (or whatever else) goes wrong:

[root@pbp ~]# lvconvert -v –merge vg0/rootsnapshot

Logical volume vg0/rootlv contains a filesystem in use.
Can’t merge over open origin volume.

Merging of snapshot vg0/rootsnapshot will occur on next activation of vg0/rootlv

Because you are trying to recover your root volume, and it is mounted, the operation is not going to succeed. But it will be recovered after next reboot, just after LVM vary on process. After this, snaphot volume will be deleted and you will boot into the system before the upgrade/whatever else.