
Automatic backup of virtual machines in XenServer
- Transfer
- Tutorial
Recently, I needed to automate the creation and saving of snapshots of virtual machines from XenServer. Having found nothing on this topic in the Russian-language segment, I went to the world Internet and found an article on the tecadmin.net resource that helped me a lot, so I decided to post its translation on Habré. Further, the author (with adaptation to our speech) translation of the original article:

I have been working with Citrix XenServer for many years, and manage all virtualization servers through the XenCenter application that is installed on my Windows computer. Until today, we regularly backed up virtual machines manually; we also had to stop the servers to copy them. Most server owners cannot afford to shut them down for a long time. Therefore, I found a way to copy virtual machines without shutting them down and, accordingly, downtime.
In this article, we will learn step by step to make backup copies of a running virtual machine, and we will also present a ready-made script that can make backup copies of all servers via cron.
Manual backup of a running machine
Script for automatic backup of running virtual machines
You can use the following bash script to back up all virtual machines running on xenserver. This script creates snapshots and exports them to an NFS drive. This script works fine for me, which may not be yours, so use it at your own peril and risk .
PS I don’t know why the author had problems with backup of running machines - XenCenter allows you to create snapshots and save them to a file without turning off the machine (of course, if XenTools is installed on it. But his automation script helped me a lot, except that you can change it under various conditions, for example, mount not NFS, but SMB or just an external drive

I have been working with Citrix XenServer for many years, and manage all virtualization servers through the XenCenter application that is installed on my Windows computer. Until today, we regularly backed up virtual machines manually; we also had to stop the servers to copy them. Most server owners cannot afford to shut them down for a long time. Therefore, I found a way to copy virtual machines without shutting them down and, accordingly, downtime.
In this article, we will learn step by step to make backup copies of a running virtual machine, and we will also present a ready-made script that can make backup copies of all servers via cron.
Manual backup of a running machine
- Search for a UUID of a virtual machine
xe vm-list is-control-domain=false is-a-snapshot=false
This team will show a list of virtual machines and their UUIDs that we need for the next step. - Creating a snapshot
The uuid parameter must be replaced with your own, obtained in the first step, make sure that it is correct.xe vm-snapshot uuid=8ac95696-94f3-83c1-bc89-8bb2603f832b new-name-label=testvmsnapshot
And this command will return uuid snapshot, by which it can be saved to a filexe template-param-set is-a-template=false ha-always-run=false uuid=b15c0531-88a5-98a4-e484-01bc89131561
- Saving a snapshot to a file
Now you can save the received snapshot to a .xva file, which can be used to restore the server from the command line or from XenCenterxe vm-export vm=b15c0531-88a5-98a4-e484-01bc89131561 filename=vm-backup.xva
- Deleting a Snapshot
After saving the .xva file, you can delete the Snapshot from XenServer itselfxe vm-uninstall uuid=b15c0531-88a5-98a4-e484-01bc89131561 force=true
Script for automatic backup of running virtual machines
You can use the following bash script to back up all virtual machines running on xenserver. This script creates snapshots and exports them to an NFS drive. This script works fine for me, which may not be yours, so use it at your own peril and risk .
#!/bin/bash
#
# Written By: Mr Rahul Kumar
# Created date: Jun 14, 2014
# Last Updated: Jan 22, 2016
# Version: 1.2
# Visit: http://tecadmin.net
#
DATE=`date +%d%b%Y`
XSNAME=`echo $HOSTNAME`
MOUNTPOINT=/xenmnt
UUIDFILE=/tmp/xen-uuids.txt
NFS_SERVER_IP="192.168.10.100"
### Create mount point
mkdir -p ${MOUNTPOINT}
### Mounting remote nfs share backup drive
[ ! -d ${MOUNTPOINT} ] && echo "No mount point found, kindly check"; exit 0
mount -F nfs ${NFS_SERVER_IP}:/backup/citrix/vms ${MOUNTPOINT}
BACKUPPATH=${MOUNTPOINT}/${XSNAME}/${DATE}
mkdir -p ${BACKUPPATH}
[ ! -d ${BACKUPPATH} ] && echo "No backup directory found"; exit 0
# Fetching list UUIDs of all VMs running on XenServer
xe vm-list is-control-domain=false is-a-snapshot=false | grep uuid | cut -d":" -f2 > ${UUIDFILE}
[ ! -f ${UUIDFILE} ] && echo "No UUID list file found"; exit 0
while read VMUUID
do
VMNAME=`xe vm-list uuid=$VMUUID | grep name-label | cut -d":" -f2 | sed 's/^ *//g'`
SNAPUUID=`xe vm-snapshot uuid=$VMUUID new-name-label="SNAPSHOT-$VMUUID-$DATE"`
xe template-param-set is-a-template=false ha-always-run=false uuid=${SNAPUUID}
xe vm-export vm=${SNAPUUID} filename="$BACKUPPATH/$VMNAME-$DATE.xva"
xe vm-uninstall uuid=${SNAPUUID} force=true
done < ${UUIDFILE}
umount ${MOUNTPOINT}
PS I don’t know why the author had problems with backup of running machines - XenCenter allows you to create snapshots and save them to a file without turning off the machine (of course, if XenTools is installed on it. But his automation script helped me a lot, except that you can change it under various conditions, for example, mount not NFS, but SMB or just an external drive
Only registered users can participate in the survey. Please come in.
How do you backup virtual machines?
- 22.9% Manual 11
- 31.2% Through the automation script 15
- 18.7% Through commercial software 9
- 27% I do not share backups 13