Tuesday, January 6, 2009

Backup XEN guest with lvm snapshot and virt-clone

LVM2 has this wonderful feature called snapshot with COW (Copy on write). There are many good howtos online describe how LVM and snapshots works. Ryan Woodrum wrote a good one on his blog, with links to other howtos. Eg. backup mysql with LVM.

In this mini-howto, I will describe how to use LVM snapshot function together with virt-clone (included in python-virtinst) to create a full image(system) backup.

Scenario:
  1. Our xen guest called guestsrv1
  2. In /etc/xen/guestsrv1 config file: disk = [ "phy:/dev/VolGroup00/guestsrv1,xvda,w" ]
  3. To create a full backup of/dev/VolGroup00/guestsrv1 without shuting down guestsrv1
Steps:
  1. Create a lvm snapshot for /dev/VolGroup00/guestsrv1, snapshot size depends on disk activities during the backup time span. In our example, snapshot size is 1Gb
    [root@domU ~]# lvcreate -s -L 1G -n guestsrv-snapshot /dev/VolGroup00/guestsrv
    Use lvdisplay to check your new snapshot volume. Eg,
    ACTIVE Original '/dev/VolGroup00/guestsrv' [10.00 GB] inherit
    ACTIVE Snapshot '/dev/VolGroup00/guestsrv-snapshot' [1.00 GB] inherit
  2. Create a snapshot xen guest config in /etc/xen
    [root@domU ~]# cd /etc/xen; sed 's/guestsrv/guestsrv-snapshot/g' guestsrv > guestsrv-snapshot
  3. Create image with virt-clone by using guestsrv-snapshot volume
    [root@domU ~]# virt-clone -o guestsrv-snapshot -n guestsrv-backup -f guestsrv-backup.img
    Cloning from /dev/VolGroup00/guestsrv-snapshot to /root/guestsrv-backup.img
    Cloning domain... 1% | | 174 MB 04:12 ETA
  4. Previous step will create an image with a new xen guest config in /etc/xen/guestsrv-backup with disk pointed to file /root/guestsrv-backup.img. You can boot up or roll back your backup if the original guestsrv crash. Now remove the snapshot volymn:
    [root@domU ~]# lvremove /dev/VolGroup00/guestsrv-snapshot
    Remove /etc/xen/guestsrv-snapshot config file:
    [root@domU ~]# rm
    /etc/xen/guestsrv-snapshot
  5. Copy or move your /root/guestsrv-backup.img to somewhere safe!
Other thoughts:
Those steps are scriptable and do-able on today's cheap disk storage technology. Use binary diff such as cmp to create "binary-patch" between the images may "decrease" data-size for remote backup. On other hand, rsync is not a bad choice neither. Always test your recovery and crisis management. Otherwise are backups useless!

No comments: