Взято: http://blog.andyburton.co.uk/index.php/2009-08/citrix-xenserver-automated-live-vm-backup-to-windows-cifs-share/


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 -


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 "// Backup" -o username=username,password=password /backup

The above would mount the windows shared folder “VM Backup” on 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


# Settings

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

# VMs to backup



# Get VM list

vm_list_string=`xe vm-list is-control-domain=false`

# Create arrays to use


# Start Log

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

# Get VMs to export

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


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

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

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


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

                let "index = $index+1"


        let "key = $key+1"


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

# Backup VMs

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


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

        # Set VM backup state


        # If the backup list is empty

        if [ $vm_backup_list_count = 0 ]; then

                # Backup all VMs


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


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

                        if [ $uuid = $backup_uuid ]; then





        # If the VM is being backed up

        if [ $backup_vm = true ]; then

                # Log

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

                # Label


                # 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


                # Log

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


        # Increment Key

        let "key = $key+1"


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


# Logging

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

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

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.


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

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"