One of the reasons I decided to switch from ESXi to XenServer for my home lab is the ability to perform backups using the Xen API. This functionality is completely free in XenServer, while you need at least an Essentials license (which is not exactly cheap) in order to do the same in ESXi.
There are a number of scripts out there that allow you to backup your VMs, but they might not work out of the box for you if your configuration is different and you need to customize something. In my case, I found this script which looked very good for my needs, with three exceptions:
- I don’t use NFS for my backup destinations; I use CIFS instead, so this script would not work with a simple copy-paste;
- The script does not compress backups by default;
- The script did not successfully unmount the backup folder at the end of the backup (fixed by simply adding the
-loption to the
All changes to the script are very easy but I thought I would share the updated code for future reference.
The updated script
#!/usr/bin/python import commands, time def get_backup_vms(): result =  cmd = "xe vm-list is-control-domain=false is-a-snapshot=false" output = commands.getoutput(cmd) for vm in output.split("\n\n\n"): lines = vm.splitlines() uuid = lines.split(":")[1:] name = lines.split(":")[1:] result += [(uuid, name)] return result def backup_vm(uuid, filename, timestamp): cmd = "xe vm-snapshot uuid=" + uuid + " new-name-label=" + timestamp snapshot_uuid = commands.getoutput(cmd) cmd = "xe template-param-set is-a-template=false ha-always-run=false uuid=" + snapshot_uuid commands.getoutput(cmd) cmd = "xe vm-export vm=" + snapshot_uuid + " compress=true filename=" + filename commands.getoutput(cmd) cmd = "xe vm-uninstall uuid=" + snapshot_uuid + " force=true" commands.getoutput(cmd) cmd = "mount -t cifs //ip_address/XenServer\ Backup -o username=username,password=password /mnt/backup" commands.getoutput(cmd) for (uuid, name) in get_backup_vms(): timestamp = time.strftime("%Y%m%d-%H%M", time.gmtime()) print timestamp, uuid, name filename = "\"/mnt/backup/" + timestamp + " " + name + ".xva\"" backup_vm(uuid, filename, timestamp) cmd = "umount -l /mnt/backup" commands.getoutput(cmd)
password in the code above are the credentials used to access the shared folder on your backup storage (in my case, with a Synology, I created a new user specifically for XenServer backups, and I have given this user read and write permissions to the backup destination). The script will then user this user and this password to connect to the share.
Including the password in the code allows you to schedule the backup using
cron, however it obviously exposes the password. Therefore, I would recommend you only allow the root user to read, write and execute the file (
Schedule the backup to run automatically
Of course, having a backup script in place and not using it is useless, so let’s make use of
cron to schedule it. Let’s say you want to run the backup every Sunday night at 3 AM. Simply run
crontab -e and add the following line before saving the file:
0 3 * * 7 /usr/bin/python backup_vms.sh