Citrix XenServer 5.5 Automated Live VM Backup to Windows CIFS Share

Up until now reliably backing up virtual machines from any of the free virtualisation hypervisors has been either expensive or impossible.

With the launch of Citrix XenServer 5.5 you can now not only take VM snapshots of live servers without shutting them down and taking them offline, you can also export these snapshots including the hard drive disk data to a template file.


This script has been superseded with a more advanced version, fixing several bugs including the snapshot disk space issue, and with additional functionality.

If you wish to use this backup script instead please follow the link -

http://www.andy-burton.co.uk/blog/index.php/2009-11/updated-citrix-xenserver-5-5-automatic-vm-backup-scripts/


From the Citrix XenServer 5.5 Documentation:

Exporting and importing virtual machines

When you export a VM, a complete copy of the VM (including disk images) is stored as a single file on your local machine, with a .xva file extension. The VM export/import feature can be used in a number of different ways:

As a convenient backup facility for your VMs. An exported VM file can be used to recover an entire VM in the event of disaster.
As a way of quickly copying a VM, for example, a special-purpose server configuration that you use many times. You simply configure the VM the way you want it, export it, and then import it to create copies of your original VM.
As a simple method for moving a VM to another server.

However, the catch with these VM exports comes with step 1 – you must shut down the VM you wish to export. This is where the snapshots come in…

From the Citrix XenServer 5.5 Documentation:

Using VM snapshots

A virtual machine (VM) snapshot is a record of a running virtual machine at a point in time. When you take a snapshot of a VM, its storage information (the data on the hard drive) and metadata (configuration information) is also saved. Where necessary, I/O is temporarily halted while the snapshot is being taken to ensure that a self-consistent disk image can be captured. Unlike VM exports, snapshots can be created without first shutting down the VM. A snapshot is similar to a normal VM template but it contains all the storage and configuration information for the original VM, including networking information. Snapshots provide a fast way of creating templates that can be exported for backup purposes and then restored, or that can be used to quickly create new VMs.

If you combine snapshots and snapshot exporting you can see the functionality to take a live export exists as standard. You can easily take a VM snapshot and export it to a local disk using the XenCenter management tool, however you may wish to automatically backup your VMs…

Luckily Citrix XenServer 5.5 comes with a fantastic command line API which we can take advantage of.

Citrix XenServer itself is also essentially a customised linux system. This allows us to write a live VM backup bash script using the Citrix XenServer command line API (xe) which we can automate using the natively installed crontab (CRON).

With this in mind, it is just a case of merging it all together to write the batch script to backup the servers and automate it.

For my setup i wanted the hypervisors to backup to a seperate windows server 2008 storage server with a windows CIFS share (a typical windows shared folder).

The Citrix XenServer hyper-visor has the ability to mount a windows CIFS shared folder, acting as a local disk.

Mount Windows CIFS Shared Folder

Create the backup folder on the Windows server and make sure the user you want to backup as has write access. Then, assuming you know the backup folder address and login details, simply SSH into the hypervisor and run the following:


[root@vs-xs2 /]# mkdir /backup
[root@vs-xs2 /]# mount -t cifs "//192.168.0.20/VM Backup" -o username=username,password=password /backup

The above would mount the windows shared folder “VM Backup” on 192.168.0.20 to the /backup folder on the hypervisor, with the windows authentication details of username=username and password=password.

Reference: http://support.citrix.com/article/CTX121937

Live VM Backup/Export Script

SSH into the hypervisor and create the following script:

[root@vs-xs2 /]# nano /home/vm_backup

#!/bin/bash

# Settings

backup_dir="/backup/"
backup_ext=" VM.xva"
date=$(date +%Y-%m-%d_%H-%M-%S)

# VMs to backup

vm_backup_list=()
#vm_backup_list[0]="901d9d55-a61d-aff8-d0d1-7da25c83d827"
#vm_backup_list[1]="fe1f60cb-dcd6-46e0-8ed6-d0d8301baf04"

vm_backup_list_count=${#vm_backup_list[@]}

# Get VM list

vm_list_string=`xe vm-list is-control-domain=false`
IFS="
"
vm_list_array=($vm_list_string)
vm_list_count=${#vm_list_array[@]}

# Create arrays to use

vm_uuid_array=()
vm_label_array=()
vm_log=()

# Start Log

vm_log[${#vm_log[@]}]="Starting VM Backup: $date"
vm_log[${#vm_log[@]}]="-----------------------------"

# Get VMs to export

vm_log[${#vm_log[@]}]="Parsing VM list"

key=0
index=0

for line in ${vm_list_array[@]}; do

        if [ ${line:0:4} = "uuid" ]; then

                uuid=`expr "$line" : '.*: \(.*\)$'`
                label=`expr "${vm_list_array[key+1]}" : '.*: \(.*\)$'`

                vm_uuid_array[index]=$uuid
                vm_label_array[index]=$label

                vm_log[${#vm_log[@]}]="Added VM #$index: $uuid, $label"

                let "index = $index+1"

        fi

        let "key = $key+1"

done

vm_log[${#vm_log[@]}]="Done parsing VM list"

# Backup VMs

vm_log[${#vm_log[@]}]="Backup VMs"

key=0

for uuid in ${vm_uuid_array[@]}; do

        # Set VM backup state

        backup_vm=false

        # If the backup list is empty

        if [ $vm_backup_list_count = 0 ]; then

                # Backup all VMs

                backup_vm=true

        # Else check to see if the VM is to be backed up

        else

                for backup_uuid in ${vm_backup_list[@]}; do

                        if [ $uuid = $backup_uuid ]; then

                                backup_vm=true
                                break

                        fi

                done

        fi

        # If the VM is being backed up

        if [ $backup_vm = true ]; then

                # Log

                vm_log[${#vm_log[@]}]="VM: $uuid"

                # Label

                label=${vm_label_array[key]}

                # Create snapshot

                snapshot=`xe vm-snapshot vm=$uuid new-name-label=backup_$date`
                vm_log[${#vm_log[@]}]="Snapshot: $snapshot"

                # Set as VM not template

                snapshot_template=`xe template-param-set is-a-template=false uuid=$snapshot`
                vm_log[${#vm_log[@]}]="Set as VM"

                # Export

                snapshot_export=`xe vm-export vm=$snapshot filename="$backup_dir$label-$date$backup_ext"`
                vm_log[${#vm_log[@]}]="Export: $snapshot_export"

                # Delete snapshot

                snapshot_delete=`xe vm-uninstall uuid=$snapshot force=true`
                vm_log[${#vm_log[@]}]="Delete Snapshot: $snapshot_delete"

        # Else the VM isnt being backed up

        else

                # Log

                vm_log[${#vm_log[@]}]="VM: $uuid"
                vm_log[${#vm_log[@]}]="Ignoring Backup"

        fi

        # Increment Key

        let "key = $key+1"

done

vm_log[${#vm_log[@]}]="Export Complete"
vm_log[${#vm_log[@]}]="

"

# Logging

#echo ${vm_uuid_array[@]}
#echo ${vm_label_array[@]}

for log in ${vm_log[@]}; do
        echo $log
done

You can download the script directly here (instead of copy/pasting): http://www.andy-burton.co.uk/blog/wp-content/uploads/2009/10/xenserver-backup-02.txt

Press ctrl+x, y and then enter to save.

The above script will look up all of the virtual servers running on the hyper-visor, generate a snapshot for each, export the snapshot to the windows shared folder and then remove the snapshot.

It will also output a log of what happens, for confirmation and debugging purposes.

The saved filename will be in the format “VM Name-2009-08-24_02-00-00 VM.xva”. You can change this by editing the backup_ext=” VM.xva” and date=$(date +%Y-%m-%d_%H-%M-%S) lines at the top of the backup script.

If you wish for individual VMs to be backed up you can add their uuid (run xe vm-list on the hypervisor to see a list of all VM uuids) to the vm_backup_list array

e.g The following would only backup the 2 specified virtual machines, ignoring any others.

vm_backup_list[0]="901d9d55-a61d-aff8-d0d1-7da25c83d827"
vm_backup_list[1]="fe1f60cb-dcd6-46e0-8ed6-d0d8301baf04"

The 2 lines above are supplied as an example and are commented out using the # character in the code.

Reference: http://forums.citrix.com/message.jspa?messageID=1342058

Automate the VM Backup/Export Script

Now that you’ve got the script to backup all your VMs you might want this to happen automatically, say 2am in the morning each day when everything is quiet. Luckily the hypervisor, being linux based, comes with crontab pre-installed which allows us to schedule a script to run automatically whenever we choose.

Whilst logged into the hypervisor run the following:


[root@vs-xs2 /]# crontab -e
0 2 * * * /home/vm_backup >> /home/vm_backup.log 2>&1

The likelyhood is that the crontab file will be opened using vi, a more specialised editor than nano. To exit type :wq. If you need to exit without saving type :q!

You can tell the crontab to be opened using nano as default (which i prefer) by adding the following lines to /etc/bashrc


EDITOR=nano
export EDITOR

You will need to re-login for the change to be effective.

Back to the cronjob we have setup – this will automatically run the backup script we created at 2am every morning. It will also save the output of the script (the progress log) to /home/vm_backup.log

Just to explain a little about the cronjob timing for those that dont understand – the 5 stars indicate 5 different periods of time: hour, minute, day of the month, month, day of the week (0-6 sunday-monday).

Here are a few examples:


0 2 * * * - Will run at 2am every day
0 0 * * * - Will run at midnight every day
0 0 * * 0 - Will run at midnight every sunday
0 0 1 * * - Will run on the 1st day of each month
0 0,12 * * * - Will run every day at midnight and midday.

For more information on how to configure the cronjob to run at different times the following should help: http://www.adminschoice.com/docs/crontab.htm

Automatically Clearing Backups

And if you want to automatically delete backup files over a week old (7 days) on the windows server to stop your disks running out of space pretty quickly?…

… The following should help:


Forfiles -p "D:\VM Backup" -m *.* -d -7 -c "cmd /c del /q @path"

This will delete all files in the “D:\VM Backup” folder that are older than 7 days. You can change the folder path and age to delete files at by editing the command.

Just setup this command up as a sheduled task in windows.

To view which files would be deleted, rather than actually deleting them, just run the following from command line:


Forfiles -p "D:\VM Backup" -m *.* -d -14 -c "cmd /c Echo @path"

Reference: http://forums.devshed.com/showpost.php?p=2072796&postcount=17

All comments good/bad welcome.

  1. Very nice! Out of curiosity, when it exports, are they thin disks or fixed disks? If I have a VM with 40gb disk, but only 15gb used, how much will the VM backup use?

  2. Andy Burton says:

    Cheers! I have been exporting with thick provisioned disks. As for your example, you would end up with a 15gb xva backup file.

  3. Trident says:

    Hello
    nice script and clean coding ..
    I d add a section to skip some vm you dont want to backup
    beside that, thx for the work

  4. miku says:

    Wow this is just what i need. But i have some problems running it. I can mount cifs share on windows 2008 server and can write to it from xen but when running script it only takes snapshot but makes no export?

    Here’s Log:

    Parsing VM list
    Added VM #0: aaac471d-03fa-b16e-927e-6851d16d413d, Windows XP SP3 TESTIKONE
    Done parsing VM list
    Backup VMs
    VM: aaac471d-03fa-b16e-927e-6851d16d413d
    Snapshot: 6cb539fe-83ee-108d-5a86-1743e37b9c7f
    Set as VM
    Export:
    Delete Snapshot:
    Export Complete

  5. Andy Burton says:

    Hi,

    This would indicate a problem exporting the VM. You are using XenServer 5.5 right?

    Run this from the hypervisor console to generate a snapshot and get the snapshot uuid:

    xe vm-snapshot vm=aaac471d-03fa-b16e-927e-6851d16d413d new-name-label=winxp_backup

    Now use this uuid and change the backup path in the command below (assuming its different from the one in the article) which will attempt to export the snapshot:

    xe vm-export vm= filename=”/backup/winxp_backup.xva”

    Let us know whether this exports correctly or if you get any return.

    • miku says:

      Tried it again after reinstalling this xen because i added some disks to local raid and added emulex fc card for connecting to hp eva.

      Tried script when this xen was member of pool and it seems to try to take export from every vm that is in pool even when they are started on four different xen servers (but are on shared storage). I thought that that this script is “local” to xenserver but i guess that pool messes this up.

      Now i removed this xen from pool for easier testing and here are results. I have this one xp test machine running on local storage. First i thought that problem was with pasting script from windows 7/firefox 3.5.3 to ssh session opened through teraterm program but now script looks ok.

      [root@VXEN000 home]# /home/vm_backup
      Required parameter not found: uuid
      For usage run: ‘xe help’
      Required parameter not found: filename
      For usage run: ‘xe help’
      Error: No matching VMs found
      Starting VM Backup: 2009-09-15_15-19-08
      —————————–
      Parsing VM list
      Added VM #0: 45814ce7-04e7-3cf5-aa9d-64753001f7c7, Windows XP SP3 TESTIKONE
      Done parsing VM list
      Backup VMs
      VM: 45814ce7-04e7-3cf5-aa9d-64753001f7c7
      Snapshot: 04dff3d0-7c8f-994a-9f80-7b13776613ef
      Set as VM
      Export:
      Delete Snapshot:
      Export Complete

      Ok then i take snapshot “serial” and use it like this….

      [root@VXEN000 home]# xe vm-export vm=04dff3d0-7c8f-994a-9f80-7b13776613ef filena
      me=/backup/winxp_backup.xva
      Export succeeded

      And it works manually like this but sript doesnt?

      • miku says:

        Forget to say that i am using xenserver 5.5 fresh installation.

      • Andy Burton says:

        Hi,

        Try adding ha-always-run=false to the param command, so:

        snapshot_template=`xe template-param-set is-a-template=false uuid=$snapshot ha-always-run=false`

        I dont have a SAN to test this on unfortunatley!

        Andy

        • miku says:

          Hi

          Thanks for help. Now i got it working with your original code. Problem was with pasting code from windows machine so it was my mistake. Cant wait to try backing up whole pool to 16TB NAS with this script.

          • Andy Burton says:

            Brilliant! Glad you got it working. Ill add a download link to the script so anyone else doesnt have to use copy/paste.

            Sounds like you are using it on a pretty beefy system, do let me know how it runs.

            Andy

            • miku says:

              We are now having problems with not enough space for snapshots on our 1TB shared storage. Xen 5.5 loses disk space when using snapshot and you cant get space back even if you delete all snapshots. Now it shows 999,6 GB used of 1000 GB total (841,8 GB allocated), so like 160GB missing. I dont have any idea what would be enought space for our shared storage so that all snapshots would work. When i get this working on whole 1TB pool ill let you know how it works in long run.

              • Miro says:

                Hi Miku,

                I have the same problem as you reported. After running script I have lost space on my xen storage. This is what I can see from xenconsole: size 265,8 GB, virtual allocation 192,5 GB, usage 260,8 GB.So I have lost somewhere about 70 GB of free disk space. I think, vm-uninstall command doesn’t delete virtual disks.

  6. Adam says:

    Are you able to modify the script to work with XenServer 5.0?

  7. Andy Burton says:

    Hi,

    Ive not used XenServer 5,0 much, but looking at the documentation the only command that would need adjusting from the above code is the is-a-template, which isnt settable through the CLI.

    The simple answer is that you could create template exports rather than VM exports, but ill give it a go on a XenServer 5.0 setup when i get a chance unless anyone else can test?

    Andy

  8. Mitke says:

    in cronjob order is minute.hour… (not hour, minutes…).
    2 0 * * * – Will run at 2 minutes after midnight every day

  9. Andy Burton says:

    Very well spotted cheers!

    Article edited.

  10. ubay25 says:

    Thank you so much for this script, bless you!

    I have a problem though, my VM’s are on a iscsi SR, and normally I created the SR with the same disk size as the VM I will create. Now when I take a live snapshot, I get the error — The specified SR has insufficient space.

    I tried doing it on a VM created on local storage of my xenserver with a good amount of disk space and it was ok.

    It seems that to take a live snapshot, the disk where the VM lies should have a good amount of free space, the question is, how much? half the size of the VM? Double? Triple?

    Please advise. Thank you!

    • Andy Burton says:

      Hi,

      Are you creating a SR for each VM? Rather than having your SR and storing each VM in it?

      The snapshot size will be roughly equal to the amount of data the VM consumes, not its disk size. So if you have a 100GB disk and only use 15GB of it your snapshot will be ~15BG, which i guess is how much space you would require spare on the SR.

      Andy

      • ubay25 says:

        Thanks for the reply.

        Yes, for our busy servers (DC, SQL, Webserver) we normally dedicate an iscsi SR for each for disk I/O reasons. So for example, if my Windows DC with a 35GB virtual disk on a 40GB iscsi SR and only uses 15GB data, do you think that should work.
        Or it wouldn’t because the virtual disk is already 35GB and the available space (5GB) is not enough on the SR for the snapshot?

  11. Av says:

    Is there a way to send an email with the log file?

  12. Andy Burton says:

    Yes.

    You can email the cron output by adding MAILTO=your@emailaddress.com in the top of your crontab and removing the log command e.g.

    MAILTO=your@emailaddress.com
    0 2 * * * /home/vm_backup

    Or you could setup a seperate script with a cronjob to email you the recorded log file.

  13. Andy Turner says:

    Is it possible to modify the script to only back up specific VMs on the host?

    • Andy Burton says:

      Sure, how do you want to do it?

      I can modify it so you can set the VMs in the script using their name or uuid?

      Or i can set it so you can pass the script a VM name or uuid argument when called?

      • ubay25 says:

        I am really interested on how to do this. Can you please give us an example.
        Thank you very much!

      • Joe says:

        I’m also very interested to see if we can select images to backup instead of all by default.

        Thanks!

  14. Adam says:

    There is no wasted space with this method? I was using another script for snapshots but my lvm chains went over the limit. When you do lvscan on the hypervisor do you see a lot of bogus logical volumes?

    -Adam

  15. Ben says:

    Cheers! I will let this run tonight and report back on how it went. You will be a saint if this works cleanly.

    • Andy Burton says:

      Great – i hope it works as expected, do let me know.

      Andy

      • Ben says:

        hello again,

        I did a dry run (without cron) just to ensure all was in order with the script/log.

        the exported file shows up on my windows share as if all went well, but I get this oddess:

        [root@xenserver-backup home]# sh vm_backup
        Export failed, unknown error.
        Starting VM Backup: 2009-09-17_12-17-06
        —————————–
        Parsing VM list
        Added VM #0: f99f6fca-17a8-4efa-3b38-08dfc99d74ab, Windows Server 2003 – Test
        Added VM #1: b302d382-5d6c-7bb9-2d54-e0fb99e37323, Debian Lenny 5.0
        Done parsing VM list
        Backup VMs
        VM: f99f6fca-17a8-4efa-3b38-08dfc99d74ab
        Snapshot: c9d3e744-88db-b185-5e6c-119ebb264b3a
        Set as VM
        Export:
        Delete Snapshot: The following items are about to be destroyed
        VM : c9d3e744-88db-b185-5e6c-119ebb264b3a (backup_2009-09-17_12-17-06)
        VDI: ee93e050-4616-4de5-8af4-2d9f04532bac (0)
        All objects destroyed
        VM: b302d382-5d6c-7bb9-2d54-e0fb99e37323
        Snapshot: 1509273a-bf35-dcbc-e8d4-2743388a07c4
        Set as VM
        Export: Export succeeded
        Delete Snapshot: The following items are about to be destroyed
        VM : 1509273a-bf35-dcbc-e8d4-2743388a07c4 (backup_2009-09-17_12-17-06)
        VDI: ca7a44e6-1458-489f-8d1c-f1ab9cdfd4b9 (0)
        All objects destroyed
        Export Complete
        [root@xenserver-backup home]#

        also, when I try to view the log….

        [root@xenserver-backup home]# ls -la
        total 16
        drwxr-xr-x 2 root root 4096 Sep 17 02:00 .
        drwxr-xr-x 22 root root 4096 Sep 16 17:07 ..
        -rwxr-xr-x 1 root root 2052 Sep 16 17:14 vm_backup
        -rw-r–r– 1 root root 44 Sep 17 02:00 vm_backup.log
        [root@xenserver-backup home]# ls -la
        [root@xenserver-backup home]# cat vm_backup.log
        /bin/sh: /home/vm_backup: Permission denied
        [root@xenserver-backup home]#

        But… the images are showing up and being recognized by XenCenter….

        • Andy Burton says:

          Hi Ben,

          That does seem odd. Have you got any further with this?

          It has obviously failed to export the windows vm with no explanation.

          I imagine you will need execute permissions on the log file to run cat also.

          Cheers,

          Andy

          • Ben says:

            I goof’d, didn’t realize my remote backup source was still in fat32, so when it hit the 4gb mark, it blew up.

            Thanks for the help and great work with the script.

  16. Kieran says:

    It would be really good if you could have it refer to a file for the list of vm names to backup.

  17. Chris says:

    You could consider to use more parameters…

    xe vm-snapshot-with-quiesce, so that you can use the Windows VSS technology.

    To check if it is the right OS you could use: xe vm-param-get uuid=a60f471b-6623-6ed5-25dd-66eb873813d3 param-name=os-version

    Last you could also check if the right XenTools is installed with xe vm-list params=PV-drivers-version

  18. Kevin says:

    Is it possible to save the automated backups to an external hard drive connected to the host since Xenserver supports USB drives?

    • Andy Burton says:

      Hi Kevin,

      If the external drive is recognised in the hypervisor and you can mount it as a local disk then you should just be able to change the export path in the script to the mounted location.

      Cheers,

      Andy

  19. Adrian Rich says:

    Andy,

    Great script – thanks for the effort. I would like to be able to use the script to backup the C: drive (i.e. /DEV/HDA) ONLY for each VM since we backup the data drives separately. My initial thought was to detach every disk except HDA before snapshotting/running a backup, but from the posts I have found this doesn’t seem feasible from the command line. Is it possible with your script to snapshot a specific disk (rather than the complete VM) before exporting to a CIFS share? I suppose an alternative would be to shutdown the VM and simply copy/clone the relevant HDA to the CIFS backup share?

    • Andy Burton says:

      Hi Adrian,

      The script exports the entire VM im afraid, not a specific drive.

      Im not aware of command line functionality to export a specific disk, but that doesnt mean its not there.

      If you can shutdown the VM then as you say, copying the disk shouldnt be a problem.

      Cheers,

      Andy

  20. Chris says:

    One more thing, I will suggest to use xe vm-install based on the template, and not xe template-param-set is-a-template=false

    Not 100% that the param-set is the best way to go…

  21. Peter Carr says:

    Thanks Andy, just the script I was after although I can’t get it to run. when trying to run it from the /home/ folder using ./vmbackup I get a Permission Denied message, the same in the CRON job log.

    Is there any way to do the 7 day clean up the snapshot files stored on the target from the Xen CLI rather than the target is a simple NAS box?

    Thanks Peter

    • Andy Burton says:

      Hi Peter,

      You may need to CHMOD the script so that whichever user is calling it has execute permissions.

      I dont see why you couldn’t write a script, bash or otherwise, for the hypervisor which runs with CRON and removes old backup files providing the user account has permission.

      Andy

  22. Kevin says:

    Why do I get this when I try to run this manually:

    : command not found
    : command not found
    : command not found
    : command not found
    : command not found
    : command not found
    : command not found
    : command not found
    : command not found
    : command not found
    : command not found
    : command not found
    ‘m_backup: line 35: syntax error near unexpected token `do
    ‘m_backup: line 35: `for line in ${vm_list_array[@]}; do

    • Andy Burton says:

      Hi Kevin,

      Which version of xenserver are you running this on?

      It looks like you have an outdated version of the bash program or something.

      Are you sure that the code has been copied correctly also?

      Andy

  23. Carlton says:

    Andy,
    Great job on this script. I’m about to start a virtualization project and this script will help.
    I work for a non-profit so we have little funds for vm tools.

    Can you let me know when you have the script modified for selecting specific VMs for the backup process?
    thanks,

  24. Andy Burton says:

    To all that have asked for specific VM backups – I have updated the code and blog post with this feature.

    Please let me know of any problems regarding this as it is relatively untested.

    Many thanks.