I’ve used UFS2 snapshots on FreeBSD by means of the dump command to make stateful filesystem backups in the past, so I recently wanted to take a similar approach to backup some Linux servers. Most GNU/Linux distributions use ext3 as their root filesystem by default these days which does not natively support snapshots, so I needed to find another way to do it. As it turns out, I used LVM on my main Linux servers to add a layer of virtualization to the storage subsystem. LVM does natively support snapshots, and here’s how to do it.

1. Make room for the snapshot

I ran into a major problem right off the bat – the server I was testing LVM snapshots on had one hard drive with 2 partitions, 1 for /boot and one for LVM. The LVM partition contained one Volume Group called “TERA-VIRTUAL” and this VG had 2 Logical Volumes – “root” (mounted on /) and swap_1 (swap). The root LV had all the PE allocated to it and the filesystem beneath it was using all of the available space. The problem is that since there were no available Physical Extents in the VG (or in my PVs), I couldn’t create a snapshot volume. This is important: when you create a snapshot on LVM you are really creating another Logical Volume that is used to hold the changes to the volume. This volume needs to me in the same Volume Group as the volume you are taking a snapshot of – so you need some extra space in that VG! In my case, I needed to resize the root fs, which was on a Logical Volume – this to me an hour plus to achieve since most LiveCDs either have no support for LVM (gparted-live) or the support is broken (backtrack 3 final usb). I booted ubuntu server 7.10 64bit into rescue mode and started a shell in the installer environment and once at a prompt typed vgmknodes to make the /dev/ entries for my LVs, then used resize2fs to shrink my FS and lvreduce to shrink my LV, leaving me with 23G of available Physical Extents in my VG (seriously, if you don’t know what I’m talking about, don’t try it – you will trash your hard drive!).

2. Create the snapshot and run backup

To create a snapshot you first need to figure out how much space you need – it is critical that you have enough space to hold all the changes to your volume until the snapshot is removed!. For me 10% of my 200G is fine – 20G.
The process is to create a snapshot LV, mount it, do your backup, unmount it, and remove the snapshot:

root@TERA-VIRTUAL:~# lvcreate --size 20G --snapshot --name snap /dev/TERA-VIRTUAL/root
Logical volume "snap" created
root@TERA-VIRTUAL:~# lvs
LV     VG           Attr   LSize   Origin Snap%  Move Log Copy%
root   TERA-VIRTUAL owi-ao 200.00G
snap   TERA-VIRTUAL swi-a-  20.00G root     0.00
swap_1 TERA-VIRTUAL -wi-ao   9.46G
root@TERA-VIRTUAL:~# lvdisplay /dev/TERA-VIRTUAL/snap
--- Logical volume ---
LV Name                /dev/TERA-VIRTUAL/snap
VG Name                TERA-VIRTUAL
LV UUID                fFbaH4-22Hq-s7a2-e1mo-DPov-8wff-dvuAiI
LV Write Access        read/write
LV snapshot status     active destination for /dev/TERA-VIRTUAL/root
LV Status              available
# open                 0
LV Size                200.00 GB
Current LE             51200
COW-table size         20.00 GB
COW-table LE           5120
Allocated to snapshot  0.01%
Snapshot chunk size    8.00 KB
Segments               1
Allocation             inherit
Read ahead sectors     0
Block device           254:2
root@TERA-VIRTUAL:~# mount /dev/TERA-VIRTUAL/snap /backup_snapshot/
root@TERA-VIRTUAL:~# cp -Rf /backup_snapshot/VirtualMachines/TERA-WEBSERVER /backup/
root@TERA-VIRTUAL:~# lvdisplay /dev/TERA-VIRTUAL/snap
--- Logical volume ---
LV Name                /dev/TERA-VIRTUAL/snap
VG Name                TERA-VIRTUAL
LV UUID                fFbaH4-22Hq-s7a2-e1mo-DPov-8wff-dvuAiI
LV Write Access        read/write
LV snapshot status     active destination for /dev/TERA-VIRTUAL/root
LV Status              available
# open                 1
LV Size                200.00 GB
Current LE             51200
COW-table size         20.00 GB
COW-table LE           5120
Allocated to snapshot  1.96%
Snapshot chunk size    8.00 KB
Segments               1
Allocation             inherit
Read ahead sectors     0
Block device           254:2
root@TERA-VIRTUAL:~# umount /backup_snapshot/
root@TERA-VIRTUAL:~# lvremove /dev/TERA-VIRTUAL/snap
Do you really want to remove active logical volume "snap"? [y/n]: y
Logical volume "snap" successfully removed

As you can see from the transcript above, you can keep an eye on your snapshot size by using lvdisplay and looking for “Allocated to snapshot”. As seen above, before the backup the snapshot volume was 0.01% full, but by the time my backup finished, it was up to 1.96%.

Now I’ve got a sane copy of my data without having to dismount my filesystem!