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://blog.andyburton.co.uk/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 scheduled 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.

109 thoughts on “Citrix XenServer 5.5 Automated Live VM Backup to Windows CIFS Share

  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. 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

  3. 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

  4. 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.

    • 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?

      • 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

        • 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.

          • 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

            • 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.

              • 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.

  5. 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

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

  7. 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!

    • 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

      • 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?

  8. 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.

    • 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?

  9. 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

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

      • 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….

        • 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

          • 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.

  11. 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

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

    • 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

  13. 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?

    • 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

  14. 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…

  15. 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

    • 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

  16. 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

    • 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

  17. 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,

  18. 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.

  19. I am having an issue running the script. I removed all the ^M’s but still won run

    error

    ./vm_backup
    -bash: ./vm_backup: /bin/bash^M: bad interpreter: No such file or directory

    any ideas?

  20. What a fantastic script. Just what I was looking for. One or two tweaks and now I’m backing up my vm estate as we speak. Many thanks for sharing this Andy B.

  21. Great script, changed to mount NFS rather than CIFS and added to `xe vm-list is-control-domain=false power-state=running name-description=backupdaily` to only backup running VMs and with a description of backupdaily ( as distinct from backupweekly)

    I can export halted at any time.

    thanks for the great job

  22. I have the script working but it’s exporting the VM’s as full size. I have one VM that is 40GB but only 2GB in use. It’s exporting the full 40GB file.

    How can I correct this?

  23. Thanks for all your work on this! I really wish that the various VM platform vendors didn’t leave the little guy with no budget in the lurch for getting backups of their systems… How great would it be to have a ‘right click’ ‘schedule backup’ ‘schedule retention’ ‘set destination’ option right there in the XenCenter that would do snapshot backups for each of the VMs? XenCenter doesn’t, ESXi doesn’t. I’m not sure about of any of the other guys…

    I noticed that there are not any new posts / replies to this work since May. Is this something that you are still actively working on? I’d like to use this script for a single XenServer I’m building, but fear that this script may have been abandoned if I were to run into problems…

    Thanks again,
    Xanderphillips

  24. Just wondering what kind of backup rates others are getting using this solution? The quick math I did against the test I’m currently running looks like about 40G/Hr. (Gigabit switch between servers).

    I realize this is a function of how fast the drives are on both ends and the network congestion, but wondering if my numbers are anywhere near where others are.

    -Xanderphillips

  25. Hi Andy, thanks for the great script!

    Given the current state of XenServer snapshots and disk space issues (being partially addressed soon in Update 1), would it be practical to modify your script to do a shutdown of the VM, then a cold export, then power the VM back on? Looks like I could just replace all of the snapshot commands with shutdown and start commands. What about timing, wondering if I might need to throw in a sleep command after the shutdown, just for good measure, to give the VM time to fully shutdown.

    In case you’re wondering, this is just for a home/lab setup, so while I would prefer not to power the VMs down, it would be ok temporarily until the snapshot issue is worked out. I have limited disk space, and can’t afford to lose any to the snapshot behavior.

    Your thoughts are appreciated!

    • Hi Tom,
      I was able to achieve what you are doing and pasted the commands in this thread. You were right about sleep. Sleep duration would seem to depend on what the VM is. If it is an SBS server, you may want to bump it up. I also found that if detaching iSCSI, you may want to call xe sr-scan uuid=id of the iscsi SR before using the xe vbd-create command.

      Regards,

  26. Pingback: Andy Burton » Blog Archive » Updated Citrix XenServer 5.5 Automatic VM Backup Scripts - PHP & MySQL Development, IT Infrastructure and Car Detailing

  27. This script works great. Andy has done absolutely amazing work on this. The only comments I have would be a short wish list for the script.
    -Wishlist:
    -Automount USB Drives to a VM, or Automount eSATA backup attached to XenServer.
    -Rotate Log or Timestamp it because it gets overwritten currently.
    -Auto-detach USB drives From VM for Snapshot Process
    -Auto-atttach USB drives back to VM post snapshot.
    -email notification.

  28. Awesome script Andy! I too would like to know the command(s)/(script) to backup a specific VM to the mounted CIFS share. Could you post or email?

    Thanks!

  29. Can anyone tell me if it is possible to take a snapshot but have it stored on a different storage repository from where the VM is on?

    We are maxed out on our storage repository space for one set of VMs but I have a new, empty storage repository that I can bring on line.

    Kindest regards,

    Peter

  30. Assuming that I have no free space for snapshot, the only way to backup is to suspend/halt VM, then vm-export it, then start VM again – am I right?

    Does it make sense to add such feature to backup script?

  31. Hi Andy,
    Thanks again for the great script. I have a question I’d like to toss up the masses though. We have 3 Xenservers, each running 3 VM’s and each VM running 2 drives(Volumes – OS, Data). The second drive is an iscsi DATA LUN that we don’t want to back up because it is being handled by something else. Is there a way to exclude or detach the iscsi storage from the VM prior to export? The only means I have found is manually turn off the VM, manually detach the iscsi, Export the OS image, re-attach the iscsi, and then start the VM.

    We have found that VM Performance over iSCSI is best handled by running the OS’s on Local raid controllers, and Data on iSCSI.

    I’d like to be able to automate this, but have not had success thus far trying to call xe vdi-forget or pdb-create. I figured that like the type=”CD” there would be a way to exclude “lvmoiscsi” in the script, but have been unsuccessful as of yet.

    Thanks again.

  32. UPDATE: I was able to resolve my issue. Since uptime isn’t a huge issue for us I basically used the following to achieve what we need. We only wanted weekly backups of the OS partitions. I hope the following can help out someone else.

    (I also used sendEmail to create monitoring notice via wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz)

    I know it’s not pretty, I will use sed/awk and grep/cut to make a more elegant script, but this works well for us. It also works on USB drives attached to VM’s so that you don’t export the entire contents of the drive in the .XVA. Currently, I have three sections in our production script, one for each VM.

    xe vm-shutdown name-label=”VM Name” (where this is name of vm you use)
    sleep 30
    xe vdi-forget uuid=(uuid of the iSCSI or USB device)
    sleep 30
    mv -fv /mnt/esata/VM-Server-Week-3.xva /mnt/esata/VM-Server-Week-4.xva
    sleep 10
    mv -fv /mnt/esata/VM-Server-Week-2.xva /mnt/esata/VM-Server-Week-3.xva
    sleep 10
    mv -fv /mnt/esata/VM-Server-Week-1.xva /mnt/esata/VM-Server-Week-2.xva
    sleep 10
    xe vm-export name-label=VM-Server filename=/mnt/esata/VM-Server-Week-1.xva
    xe vbd-create vm-uuid=(your vm uuid) vdi-uuid= (your vdi uuid) device=hdb
    sleep 30
    xe vdi-param-set uuid=(your vdi uuid) name-label=”iSCSI Storage” name-description=”Unmounted/Detached during OS-only backups”
    sleep 30
    xe vm-start name-label=VM-Server

    Cheers!

    • UPDATE2:
      Silly me, on Some USB Drives, when using xe vdi-forget, after running xe sr-scan on the SR UUID of removable storage, the VDI-UUID would change, so remounting using the same VDI-UUID would fail. It would also cause the backup script to back up the entire contents of the attached USB drive in addition to the VM.

      I resolved the issue with the following hack to retrieve the the VDI-UUID after running SR-SCAN. I Added these lines after running vm-export, and before vm-start.

      xe sr-scan uuid=(UUID of your Removable Storage Repository)
      VDI_UUID2=`xe vdi-list sr-uuid=(SR-UUID) | grep -i “^uuid ( RO)” | cut -f2 -d’:’ | sed ‘s/^\s//’`
      xe vbd-create vm-uuid=(VM UUID) vdi-uuid=$VDI_UUID2 device=1

  33. when running the script I get these errors, how can I fixed them? :

    [root@NYXS01 home]# /home/vm_backup
    /home/vm_backup: line 4: +%Y-%m-%d_%H-%M-%S: command not found
    /home/vm_backup: line 19: for: command not found
    /home/vm_backup: line 20: syntax error near unexpected token `do’
    /home/vm_backup: line 20: `${vm_list_array[@]}; do’
    [root@NYXS01 home]#

    Did everything to the T when copying the script

  34. Pingback: Citrix XenServer 5.5 – Back that chassis up « John Lee Nelson

  35. Hi,

    This is a GREAT script, congrats!

    I changed the date and also added gzip compression of the XVA file, here are the changes:

    1. replace # Settings with the following:
    # Settings
    backup_dir=”/backups/”
    backup_ext=”_VM.xva”
    date=$(date +%Y%m%dT%H%M%S)
    pack=true
    pack_cmd=”`which gzip`”
    pack_ext=”.gz”

    2. Replace from # Export to # Delete snapshot with the following:
    # Export

    snapshot_export=`xe vm-export vm=$snapshot filename=”$backup_dir$label-$date$backup_ext”`
    if [ $pack = true ]; then
    $pack_cmd “$backup_dir$label-$date$backup_ext” && vm_log[${#vm_log[@]}]=”Exported and packed: $backup_dir$label-$date$backup_ext$pack_ext” || vm_log[${#vm_log[@]}]=”Exported, but packing failed: $backup_dir$label-$date$backup_ext”;
    else
    vm_log[${#vm_log[@]}]=”Exported, packing was set to false: $backup_dir$label-$date$backup_ext”;
    fi

    # Delete snapshot

    Adjust the packer settings if you want different packer or other compression level, as long as pack=true packing is enabled, anything else there disables the packing mechanism (like pack=nottrue or =false or =x disables it).
    Right now the end result file will be like:
    vm-real-name-20100529T064130_VM.xva.gz

    Exporting and packing with gzip set on default compression level of a 2.8GB VM resulted in a file that is only 833MB and took 4mins on a single quad core cpu.


    Marius

  36. hi!

    i’m very new to xenserver and linux.

    any kind person who can teach me the commands on how to run this manually and check the log files manually.

    thanks!

  37. Pingback: How to backup Xen Server VMs | Steve Garriques's Professional Blog

  38. Hi

    Had anyone the same problem with CIFS as I?
    The Problem is, that if i write the snapshot to a cifs share
    on an Windows 2003 Server i got corrupt files. Tested everything
    but it seems to be definetly a problem of the CIFS implementation
    in the XenServer. The files (sometimes) get random zero-bytes…
    In the log there are the folowing messages:
    Mar 26 09:31:51 xensnapshottestsrv kernel: CIFS VFS: Write2 ret -11, wrote 4096
    Mar 26 09:31:52 xensnapshottestsrv kernel: CIFS VFS: No response to cmd 47 mid 1995
    Mar 26 09:31:52 xensnapshottestsrv kernel: CIFS VFS: Write2 ret -11, wrote 0
    Mar 26 09:31:52 xensnapshottestsrv kernel: CIFS VFS: Write2 ret -11, wrote 4096
    Mar 26 09:32:22 xensnapshottestsrv kernel: CIFS VFS: server not responding
    Mar 26 09:32:22 xensnapshottestsrv kernel: CIFS VFS: No response for cmd 117 mid 2036

    Has any one an idea?

    Best regards
    afries

Leave a Reply to Marius Cancel reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>