Filippo Zanardo's Blog

to blog or not to blog

Xenserver Backup Updated 2.2

with 31 comments

Ok guys,
i have some time and i have updated my xen backup script, to improve it and to add some few features.
Basically i add the possibility to compress the backup and the possibility to have more than 1 removable storage, the line changed in the config.txt file are:

#the uudi of the removable storage, at the moment i don’t have an efficent script to localize since name can change
@removableuuid = (‘d7a8fb07-e7d1-9eb4-6d79-dca8680ace89′,’d7a8fb07-e7d1-9eb4-6d79-dca8680ace86’);

#compress option enable/disable compression
$compress = true;

#compress command
$compresscmd = “tar -czvf”;

#compress extension easiser for me to put correct extension
$compressext = “.tar.gz”;

Few other check and modification, the rest is the same.
You can downlaod the new script at:

Please comment for feature, bug and so on.

Enjoy! Cheers!


31 Responses

Subscribe to comments with RSS.

  1. Hi Filippo,
    thanks for your script. I am testing it on my xenfarm and I’ve noticed that sometimes xenserver does not delete the vm snapshot, leaving the vm disk on the storage.
    Have you got any idea on how to solve this? Is there anything I can do to troubleshoot this issue?


    April 7, 2011 at 16:24

    • i thik it’s related on xenserver itself btw look inside the /va/log/mylog file and we can try to diagnose the problem.


      April 7, 2011 at 18:07

  2. […] 2.2 is now avaiable i rewrite the check avaiable space to fix a minor […]

  3. Is there a way to modify the script so that the xva’s are written to local storage and then compressed to the backup location? It would save me time because I can have other backup jobs running against the NAS while I wait for Xen to export the VM’s and compress them.


    May 4, 2011 at 20:10

    • I have modified the script in order to do that. I can post it somewhere if it can be useful.



      May 9, 2011 at 15:14

      • send me by email so i can add your feature.


        May 10, 2011 at 10:04

      • Hi Stefano, wondering how did you get the script to write the backup to Local Storage? I thought the Local Storage is LVM and not be able to access from SSH command line?
        Please advise. Thanks.

        Xin Chen

        May 15, 2011 at 15:04

      • the script can use already local storage, it’s not the ideal solution, the article you post it’s only how can you handle storage repository with or without lvm.
        for local storage turn off automount and use backupdir as your local dir.


        May 16, 2011 at 09:20

  4. Thanks Pipposan. Sorry but I am still confused. When I SSH to my XenServer Host, I can only see 4G disk space (/ folder) where xensystem took around 2G. There is no enough space for backup script to store any backups. However, the Local Storage does have plenty space (400G in my case), but I just can’t figure how to access this 400GB. It is not a folder in the system. I wouldn’t know what to assign to backupdir variable in the script to use this 400G Local Storage. Help please.

    Xin Chen

    May 16, 2011 at 09:40

    • Hi, basically your local storage is 400G and local storage usually is where vm machine is stored, so i don’t think make backup on local storage is advisable, btw if you’re local storage is lvm based, you can’t access any part of the disk until you create a new lvm volume and format and mount that volume on a folder.
      try first to undestand what lvm is lvm than i suggest you to put the backup on a usb disk/nas or whatever, cause if your raid/disk… broke or simply if you reformat your local storage you loose all your backup.


      May 16, 2011 at 12:56

      • Good stuff! That clears everything.

        Xin Chen

        May 16, 2011 at 13:14

    • Hi Xin,
      basically you can mount the local storage repository to a XenServer default file system install local directory (Dom0) by issuing a couple of commands.
      First, find the uuid of the XenServer local storage you want to run the backup script:
      # xe sr-list host=xen01
      locate the ‘Local storage’ name-label, take note of the uuid paramenter.
      Go to /dev/ directory and look for something called XSLocalEXT-‘uuid noted before’/’uuid noted before’.
      Then create a mount directory, i.e. /media/LocalLV.
      You can now mount the XenServer local storage:
      #mount /dev/XSLocalEXT-‘uuid’/uuid /media/LocalLV
      Here you can backup the xva files then compress them over the network (we do it on a NFS share).
      We have a fiber channel shared storage and use the local storage as a high performance temporary local directory to create the xva, then transfer it over the NFS share already compressed to save time and disk space.


      May 17, 2011 at 17:55

      • Very nice!! Since I have no live vm stored in local storage, they all in a nfs share. I guess it would Be ok for me use local storage as a temp folder. Thanks.


        May 18, 2011 at 02:38

      • This is exactly why I asked my question above. My local storage on my Xen boxes is ext3 (I converted it from LVM). Since my backup takes like 8 hours, I was hoping to use the nice fast spindles I have sitting in each Xen box doing nothing to cache the backups, then I could compress them and copy over the network saving time in the backup procedure. I only have 1 NAS that’s storing the VHD’s and a 2nd NAS that’s for backup. Right now I have to have a complex backup scheme to make sure I have enough time during the night to complete all the backups. Where might I get your copy of the script Stefano?


        May 20, 2011 at 19:01

      • Hmmm… interestingly, I couldn’t find /dev/XSLocalEXT* in my xenserver (v5.6).

        Xin Chen

        May 29, 2011 at 13:56

  5. Hi!

    The script is great, but I see one issue:
    VMs that have more than one VDisk are backed up, but the snapshots of the second and third vdisk are not deleted after the backup.

    Is there any way to avoid that?



    May 17, 2011 at 10:34

    • I’d like to know this as well because I have alot of VM’s with multiple vdisks.


      May 21, 2011 at 01:55

  6. Hi Pipposan,

    Thanks for great script. How about option to make backup only of chosen VMs?


    July 19, 2011 at 15:50

  7. Hi, thanks for the script. It seems to work, but on my test run I got a defective backup. Two VMs were set to be backed up, rest were skipped (1: Windows 2008R2, 2: Centos).
    According to the logs everything went fine, but on the (NFS mounted) backup directory the VM1 .tar.gz is very small (260MB tgz vs. 17GB xva), and the archive is corrupted – truncated:

    #tar tvf VM1.tar.gz

    gzip: stdin: unexpected end of file
    tar: Unexpected EOF in archive
    tar: Error is not recoverable: exiting now

    Some kind of error checking logic would be great! Any thoughts?


    August 5, 2011 at 10:13

  8. Hi!

    I have modified the script. Now, all the VDIs of the snapshots are deleted. I needed to do that because “vm-uninstall” does just delete the first VHD of the VM:

    $vbdlist = `xe vbd-list vm-uuid=$snapshotUUID | grep vdi-uuid`;
    @vdilineList = split(/\n/,$vbdlist);
    @vdiuuid = ();

    foreach $vdiline(@vdilineList){
    push(@vdiuuid, substr($vdiline,25,38));

    foreach $vdientry(@vdiuuid){

    $destroy = `xe vdi-destroy uuid=$vdientry`;
    ### Edit End

    $status= `xe vm-uninstall uuid=$snapshotUUID force=true`;



    August 5, 2011 at 13:15

    • Can you send/post the complete script here ?

      It would be very helpful !



      October 5, 2011 at 17:07

  9. Dear Filippo,

    I would recommend to re-write/improve the script. I can volunteer. The perl is *really* hurting my eyes!!
    1- Use functions for logging and e-mail notification instead of if () {} million times
    2- strict, warnings; ftw!
    3- And more generally: Refactor, Refactor, Refactor
    I am sure by refactoring the code it can be reduced by huge amount of redundant lines!

    Plus I got a patch that introduces “locking” feature (I did not test it yet!!), this in case you backup to a NFS and suddenly your NFS is very slow and a new backup starts while old one is in progress..

    Please mail me if you want to work together on this. I think there is a lot of room for improvement.

    Patch (beware wordpress might mangle some special characters):

    — 2011-08-04 12:15:20.000000000 +0200
    +++ 2011-08-10 06:43:36.000000000 +0200
    @@ -1,6 +1,7 @@

    #XEN Server backup by Filippo Zanardo
    +#added “locking” by Ville Ojamo
    #follow me at

    use Switch;
    @@ -84,7 +85,19 @@

    +if (-e “$b/.lock”) {
    + my $lockdiemessage = “$backupdir.lock file exists, assuming backup ongoing, exiting.\n”;
    + if ($logging eq true) {
    + $logdate=localtime();
    + print MYLOG $lockdiemessage;
    + }
    + print MYFILE $lockdiemessage;
    + if ($mailNotification){
    + $send = `ssmtp $MailTo >$b/.lock”) or die “Lock file cannot be created, is $backupdir writable: $!”;

    $vmlist = `xe vm-list`; #Get the formatted list of guests
    @lineList = split(/\n/,$vmlist); #Split the list of guests into and array of lines
    @@ -577,3 +590,4 @@
    $send = `ssmtp $MailTo < /tmp/emailmessage`;

    +unlink "$b/.lock";

    Ville Ojamo

    August 10, 2011 at 06:45

    • Yep, wordpress mangled it pretty bad. Do not attempt to run this through `patch`.. Pls contact me.

      Ville Ojamo

      August 10, 2011 at 06:48

      • sure i want to develop and improve the script, any1 can partecipate and improve it, basically now i have no time to recode or modify the script, btw i start also develop a php version with interface of the script but ad i said before in this moment i have no time to quicly develop my new project, btw mail me your modification and i will include in the script or we can work togheter for improve the script.


        August 10, 2011 at 09:40

  10. Using this script with automount option, may cause to 100% usage of root partition, and server fault on reboot.
    If mounted resourse is not avalible, script is continue running, and backuping VM to local root partition(8 GB), causing it to overflow.
    Maybe need script fix to stop running script after unsusseful mounting.

    Sorry for my English.

    Nogaev Pavel

    August 15, 2011 at 10:57

    • Have same problem few month ago.


      September 12, 2011 at 15:08

  11. It seemed to have worked only for the first time. On subsequent tries I get:
    [root@xs-app6 backup]# perl $PWD/ $PWD/config.txt
    Error code: SR_BACKEND_FAILURE_82
    Error parameters: , Failed to snapshot VDI [opterr=failed to pause VDI 710a8f2e-dbc2-41ca-84b1-9c38458ea9a3],
    The uuid you supplied was invalid.
    type: VM
    Error: No matching VMs found
    mv: cannot stat `/mnt/backup/xen/SSH-Gateway/SSH-Gateway-110905-1756.xvatmp’: No such file or directory
    Error: No matching VMs found
    Error code: SR_BACKEND_FAILURE_82
    Error parameters: , Failed to snapshot VDI [opterr=failed to pause VDI c43953fa-2cfd-4cec-ae5d-2503726f7117],
    The uuid you supplied was invalid.
    type: VM
    Error: No matching VMs found
    mv: cannot stat `/mnt/backup/xen/pskiba-xen/pskiba-xen-110905-1756.xvatmp’: No such file or directory
    Error: No matching VMs found
    Any ideas how to debug it?

    Tomasz Jarzynka

    September 5, 2011 at 18:01

  12. Hi, it works like a treat, and I’ve been using it for some time. The only thing I haven’t been able to work out is the emailing. We have Exchange 2010 and I followed the brief notes but must be missing something. However once I’d made the emailing changes we get emails when our XEN hosts shutdown/restart so obviously its partly working. Any ideas on the emailing would be appreaciated.



    September 22, 2011 at 07:56

    • We’ve got the same problem here, Sendeing mail from the console works after the changes, but there is no mail send from the script. The first issue was that the ssmtp directive was not found so i changed that to

      $send = `/usr/sbin/ssmtp $MailTo < /tmp/emailmessage`;

      But i still get no e-mail



      October 5, 2011 at 17:05

      • Hi Dirk
        I changed the value from MailTo within the config.txt to instead of , it worked.

        Peter Speck

        October 6, 2011 at 09:21

  13. Hi Dirk
    I changed the value from MailTo within the config.txt to
    instead of,
    it worked.

    Peter Speck

    October 6, 2011 at 09:23

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: