How to backup Xen with Logical Volume Mounts ; Works with HyperVM, SolusVM, FluidVM and More

Through our research and implementation of many Xen environments, it has become necessary to develop a reliable and secure method for backing up our Xen instances that are mounted on Logical Volumes (LVM).

The underlying problem is that the logical volume is usually a live file system that cannot be directly mounted / backed up or imaged safely.

We have written a script that processes all running Xen logical volumes, creates a snapshot of the volume and through that snapshot , uses dd to image the snapshot to another server over ssh.

You would be surprised at how well these dd images compress. Piping dd to bzip2 then to ssh to receive the image produces a very substantial compression ratio.

The initial trouble was writing the logic in the script to properly go through each Xen LV , create the snapshot, image and then remove the snapshot. Obviously extensive testing had to be completed to ensure reliability and proper error reporting.

This script should work with any 3rd party Xen control panel implementation (HyperVM, FluidVM, SolusVM to name a few). They all use the same underlying technology / framework. Since our script is a simple bash / shell script, it will run on any linux based system with little modification.

If you are using a LV for another purpose on the same box, it is probably a good idea to modify the script to ignore that so it doesn’t inadvertently get backed up.

Before implementing the script, it is probably a good idea to go through the motions manually just to see how it performs :
lvcreate -s -L 5G -n vm101_img_snapshot /dev/vps/vm101_img
dd if=/dev/vps/vm101_img_snapshot | bzip2 | ssh xenbackup@x.x.x.x "dd of=vm101_img.bz2"

One thing that you cant get around is space — you need to leave as much room as the largest Xen image on your logical volume — otherwise the script will fail at the snapshot creation process.

Find the script below. Hopefully it will help make your life easier (as well as being able to sleep at night) :
#!/bin/bash
# XEN Backup script
# Written by Star Dot Hosting

todaysdate=`date "+%Y-%m-%d"`

echo "XEN Backup Log: " $currentmonth > /var/log/backup.log
echo -e "------------------------------------" >> /var/log/backup.log
echo -e "" >> /var/log/backup.log

for obj0 in $(lvs --noheadings --separator ',' -o lv_name,lv_size | grep -v "swap" | awk -F "," '{printf "%s\n", $1}');
do

#grab the snapshot size
snapsize=`lvs --noheadings --separator ',' -o lv_name,lv_size | grep -v "swap" | grep $obj0 | awk -F "," '{printf "%s", $2}'`

#create the snapshot
lvcreate -s -L $snapsize -n $obj0_snapshot /dev/xenlvm/$obj0 >> /var/log/backup.log 2>&1

#dd piped to bzip2 to compress the stream before piping it over the network via ssh to the destination box
dd if=/dev/xenlvm/$obj0_snapshot | bzip2 | ssh xenbackup@0.0.0.0 "dd of=/home/xenbackup/xen-backups/$obj0.$todaysdate.bz" >> /var/log/backup.log 2>&1

if [ "$?" -eq 1 ]
then
echo -e "***SCRIPT FAILED, THERE WERE ERRORS***" >> /var/log/backup.log 2>&1
cat /var/log/backup.log | mail -s "XEN Backup Job failed" admin@yourdomain.com
lvremove -f /dev/xenlvm/$obj0_snapshot
exit 1
else
echo -e "Backup of $obj0 Completed Successfully!" >> /var/log/backup.log 2>&1
fi

# remove the snapshot
lvremove -f /dev/xenlvm/$obj0_snapshot

done

cat /var/log/backup.log | mail -s "XEN Backup Job Completed" admin@yourdomain.com

11 comments:

  • Gloria Maddox

    This is excellent! How did you learn this stuff?

  • Ellis Snider

    Please, keep up the awesome work and continue to post topics like this. I am really fan of your site.

  • Charles Henry

    Definitely, what a great site and instructive posts, I definitely will bookmark your blog.Best Regards!

  • Tracey Olveira

    Thank you! I consistently bare to address on my website something like that. Can I apparatus a allocation of your column to my blog?

  • Lana Kimble

    Thank you! I consistently bare to address on my website something like that. Can I apparatus a allocation of your column to my blog?

  • Metin2

    I typically dont write comments on posts, but your write-up urged me to commend your writings. Thanks for writing this, I am going to certainly common your web site and come back once in awhile. Happy blogging.

  • Easy Animation Software

    I’m impressed, I need to say. Really not often do I encounter a blog that’s both educative and entertaining, and let me let you know, you've gotten hit the nail on the head. Your thought is outstanding; the difficulty is one thing that not sufficient individuals are talking intelligently about. I am very glad that I stumbled throughout this in my search for one thing referring to this.

  • Elenora Munks

    Superb blog post, I acquire book credible this internet website so alluringly I’ll see abounding added on this answerable in the answerable future!

  • Particia Stepp

    Thank you! I consistently bare to address on my website something like that. Can I apparatus a allocation of your column to my blog?

  • Natisha Survis

    I saw this identical post of yours, located at facebook and figured that you might well want to know. You're making some progress!

  • Post a Comment