Hot backup for KVM / libvirt / Xen


We use Xen server on our development server and I decided, it’s time I start making full snapshots of my VM’s. So far we’ve done data backup only, mainly because of storage limitations. Since we got the new server, I have much more disk space now, so I would like to backup whole VM’s. Of course I don’t want to stop them when the backup is going on, or at least with minimum downtime (few seconds is acceptable).

The script, modifications and execution

Script for backuping is located at following URL: . It was written by Daniel Berteaud and it’s probably best script I have found so far. It does everything you need. It did need few changes when used on CentOS though:

# lvcreate path
- $opts{lvcreate} = '/usr/sbin/lvcreate -c 512';
+ $opts{lvcreate} = '/sbin/lvcreate -c 512';
# lvremove path
- $opts{lvremove} = '/usr/sbin/lvremove';
+$opts{lvremove} = '/sbin/lvremove';

I also installed pbzip2 on my CentOS, which is parallel bzip2 and makes use of all the cores in the server. To make sure our backups are created as fast as possible, I used it to compress my VM’s.

There was an additional change needed in, for virsh to be able to contact Xen socket:

$opts{connect} = "xen:///";

After those changes were made, it was time to try it out:

myserver ~ $ ./ --action=dump --vm=myvm01 --backupdir=/backup/ --compress=pbzip2 --debug

Connecting to libvirt daemon using xen:/// as URI

Checking myvm01 status

Running dump routine for myvm01

Locking myvm01

Saving XML description for myvm01 to /backup//myvm01/myvm01.xml
myvm01 is running, suspending
tmyvm01 now suspended

Analysing disk /dev/VG_RAID10/MYVM01_C connected on myvm01 as hda

Running: /sbin/lvcreate -c 512 -p r -s -n /dev/VG_RAID10/MYVM01_C_1384248972 -L 5G /dev/VG_RAID10/MYVM01_C > /dev/null 2>&1
/dev/VG_RAID10/MYVM01_C seems to be a valid logical volume (LVM), a snapshot has been taken as /dev/VG_RAID10/MYVM01_C_1384248972
Adding /dev/VG_RAID10/MYVM01_C_1384248972 to the list of disks to be backed up

The following disks will be dumped:

Source: /dev/VG_RAID10/MYVM01_C_1384248972  Dest: /backup//myvm01/myvm01_hda.img.bz2

We can run a live backup
myvm01 is suspended, resuming
myvm01 now resumed

Starting dump of /dev/VG_RAID10/MYVM01_C_1384248972 to /backup//myvm01/myvm01_hda.img.bz2

102400+0 records in
102400+0 records out
26843545600 bytes (27 GB) copied, 375.959 s, 71.4 MB/s
Removing snapshot /dev/VG_RAID10/MYVM_C_1384248972
myvm01 is not suspended, nothing to do
Removing lock file for myvm01

This VM had 27GB of logical volume size and after being compressed, backup file was about 8GB of size. It also took 3 minutes to back it up and total downtime less than a second. Good enough!


  1. By Maik


  2. Reply

  3. Reply

  4. By Eike


  5. Reply

    • By Eike


  6. Reply

Leave a Reply

Purpose of the commenting system is to share your experience. I encourage you to post feedback with your own suggestions, ideas or optimizations regarding the topic of a blog post. What commenting system isn't for, is asking questions about similar issues of yours and requesting support for it. Blog post is provided as is and I am not here to solve all your problems. Please bear that in mind and try to avoid posting such comments. I do take privilege to remove comment from my blog for any reason whatsoever. Usually I do it when I sense a comment was posted only for spam/seo reasons or is out of blog post's topic. Thank you for reading this, now you may continue :)

Your email address will not be published. Required fields are marked *