HowTo use LVM to create filesystem snapshots for backups

If you have large amounts of data you’d like to backup, it can be cumbersome to use the normal backup tools such as tar. If data changes during the backup you might end up with an unusable backup.

If I want to backup my videos, recordings, music etc. of my MythTV box I cannot afford to take the filesystem offline for an entire night. Hence this solution.

I use XFS as my filesystem to store my stuff. It is a robust and modern filesystem designed for large files, precisely what I need for my recordings.

You need your volumes to be part of an LVM VG for this to work. You can find more information on LVM on IBM’s website: http://www.ibm.com/developerworks/linux/library/l-lvm/

The process to create a backup from a snapshot is quite easy:

# xfs_freeze -f /mountpoint/of/backup-fs

This will freeze the filesystem you want to backup essentially stalling all IO. For example if I have a LVM volume on /dev/myth/production mounted as /storage/mythtv I run

# xfs_freeze -f /storage/mythtv

Afterwards we can create a new logical volume with the same contents using LVM:

# lvcreate -l 500 -s -n snap /dev/myth/production

using the s argument tells LVM that I want a snapshot that is named „snap“ using the -n option.

Now I can safely mount the snapshot. I need the nouuid option because otherwise XFS would think it is mounting the same filesystem a twice. Basically that is the case though.

# mount -o nouuid,ro /dev/mapper/myth-snap /var/myth-snap

Now its time to unfreeze the „real“ filesystem and resume IO:

# xfs_freeze -u /storage/mythtv

At this point you can start the actual backup process using your favourite tool. Be it tar, rsync etc.

Once the backup run is complete you can safely unmount and destroy the snapshot.

# unmount /var/myth-snap

# lvremove -f /dev/myth/snap

The beauty of this is, that it can easily be put in a little script that runs every night. For a lot of applications it is also better to rely on a backup rather than a RAID which will only keep availability up but does in no way protect your data. Add a hotswap drive bay to your case and get a couple disk sleds and you have a fast, cheap and reliable backup solution up into multi terabyte territory…

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.