Backup delle VM su XenServer

Il mondo della virtualizzazione porta molti benefici, tra i quali la possibilità di semplificare le procedure di backup dei wordload. Facendo una rapida ricerca non sono riuscito a trovare uno script per effettuare il backup delle macchine virtuali su Citrix XenServer.
In ambiente XenServer esiste la possibilità di fare l’export di una macchina virtuale in un singolo file .xva (Xen Virtual Appliance) e tramite il comando xe è possibile creare uno script per effettuare il backup delle macchine virtuali.

Partendo da quest’idea ho provato a creare uno script shell da utilizzare direttamente sul Domain0 del master di un Resource Pool XenServer. Premetto che non sono un guru della programmazione in bash su sistemi Unix e che questo script può facilmente essere migliorato ed ampliato. Ho deciso di pubblicarlo solo a scopo dimostrativo.

Lo script effettua il mount di un filesystem nfs nella cartella /media/backup, controlla l’elenco delle macchine virtuali, e una ad una procede con l’export, eventualmente spegnendo e riavviando la VM. Lo script effettua basilari check sugli errori e non prevede la gestione di filtri da applicare all’elenco delle macchine virtuali delle quali effettuare il backup. Non è inoltre previsto il backup di template generati dall’utente.

Attenzione: lo script è fornito così com’è, senza nessuna garanzia di funzionamento per puro scopo dimostratico. Utilizzatelo a vostro rischio e pericolo, ma non in ambienti di produzione! Lo script può essere ulteriormente sviluppato e arricchito aggiungendo funzioni di filtro, parametrizzando ulteriori valori, logging, ecc.

 

#!/bin/bash
#
# VM Backup Script 0.1 for XenServer
# Author: Francesco Dipietromaria
# Web Site: http://www.dpmworld.net
#
#
# This is the path of the filesystem where we will perform the export of the VM
# If you are running on a single host, this may also be configured to point to a USB disc (like /dev/sdb1)
#
BACKUPNFS="<fileserver FQDN>:/nethdd/XenBackup/"
#
# Let's check if there is a mount point for the filesystem where we want to backup VM
#
if [ -d /media/backup ]; then
echo Directory esistente
else
echo Creating backup mount point
mkdir /media/backup
if [ "$?" -ne "0" ]; then
echo "Cannot create directory. Exiting..."
exit 1
fi
fi
#
# Ok, now we can mount the remote filesystem
#
echo Mounting backup directory
mount $BACKUPNFS /media/backup/
if [ "$?" -ne "0" ]; then
echo "Cannot mount backup filesystem. Exiting..."
exit 1
fi
#
#
# We are now generating a list of names of the VMs
# Select the command that best suite your needs (select only one)
#
# The following line returns a complete list of VM configure on the system
xe vm-list | grep -v "Control domain" | grep name-label | cut -c 24-100 | sort > tmp.txt
#
# The following line returns a list of halted (not running) VM on the system
#xe vm-list power-state=halted | grep -v "Control domain" | grep name-label | cut -c 24-100 | sort > tmp.txt
#
# The following line returns a list of running VM on the system
#xe vm-list power-state=running | grep -v "Control domain" | grep name-label | cut -c 24-100 | sort > tmp.txt
#
#
# Let's atart cycling throu the VMs and export them
#
cat tmp.txt | while read VM
do
RUNNING=0
COUNT=`xe vm-list name-label=$VM params=power-state | grep running | wc -l`
if [ $COUNT -eq 0 ] ; then
echo The VM $VM is already halted
else
echo The VM $VM is running. Stopping now.
RUNNING=1
xe vm-shutdown vm=$VM
fi
echo Export of VM $VM started at `date | cut -d " " -f5`
xe vm-export vm=$VM filename=/media/backup/$VM.xva
echo Export of VM $VM finished at `date | cut -d " " -f5`
if [ $RUNNING -eq 1 ]; then
echo Restarting VM $VM...
xe vm-start vm=$VM
echo Done.
fi
done
echo Unmounting backup directory
umount /media/backup
rm tmp.txt
#
# Finish

One thought on “Backup delle VM su XenServer

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *