📜 ⬆️ ⬇️

Automatic backup of virtual machines in XenServer

Recently, it was necessary to automate the creation and saving of snapshots of virtual machines from XenServer. Having found nothing on this topic in the Russian-speaking segment, I went to the global Internet and found an article on the resource tecadmin.net, which helped me a lot, so I decided to post its translation on Habré. Further, the author (with adaptation for our speech) translation of the original article:

I have been working with Citrix XenServer for many years, and managing all the virtualization servers through the XenCenter application, which is installed on my Windows computer. Until today, we regularly backed up virtual machines manually, just 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, idle.

In this article, we will step by step learn how to make backup copies of a running virtual machine, as well as a ready script that can make backup copies of all servers via cron.

Manual backup running machine
')
  1. Search for UUID virtual machine

    xe vm-list is-control-domain=false is-a-snapshot=false 

    This command will show the list of virtual machines and their UUIDs that we need for the next step.

  2. Making snapshots

    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 the uuid snapshot, according to which it can be saved to a file.

     xe template-param-set is-a-template=false ha-always-run=false uuid=b15c0531-88a5-98a4-e484-01bc89131561 

  3. Saving snapshot to file

    Now you can save the snapshot to an .xva file, which can be used to restore the server from the command line as well as from XenCenter

     xe vm-export vm=b15c0531-88a5-98a4-e484-01bc89131561 filename=vm-backup.xva 

  4. Snapshot removal

    After saving the .xva file, you can remove the snapshot from the XenServer itself

     xe vm-uninstall uuid=b15c0531-88a5-98a4-e484-01bc89131561 force=true 

The script automatically backup running virtual machines

You can use the following bash script to back up all virtual machines running on the xenserver database. This script creates snapshots and exports them to an NFS disk. This script works fine for me, which you may not have, 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 shutting down the machine (of course, if XenTools is installed on it. But its automation script helped me a lot, but you can change it under various conditions, for example, to mount not NFS, but SMB or just an external drive

Source: https://habr.com/ru/post/317356/


All Articles