Scripted XenServer VM Backup – Sicherung von Virtuellen Maschinen

Das Sichern von virtuellen Maschinen auf XenServern über das XenCenter mittels Snapshots und anschließendem Export ist eine praktische und komfortable Sache. Leider ist dies dennoch eine manuelle Sicherung und daher recht unpraktikabel auf Dauer.

Bei meiner Suche nach einem Skript welche diese Funktion automatisiert erledigt, bin ich auf Jeff Riechers und dessen VBS-Lösung gestoßen. Im Prinzip war es das, was ich suchte: Ein über den Taskplaner oder ähnliches startbarer Export einer oder mehrere VMs zu einer XVA-Datei. Das ganze läuft auf einem selbst nicht virtualisierten Windows System, das kann auch ein Desktop PC sein. Einzige Voraussetzung ist ein installiertes XenCenter und genug Speicherplatz lokal oder auf einem Netzlaufwerk.

Ich habe das Skript von Jeff Riechers etwas modifiziert und so „schluckt“ es nun auch VM Namen mit Leerzeichen und überschreibt nicht mehr das alte Backup sondern ergänzt beim Dateinamen das Erstellungsdatum. Des weiteren habe ich die Dokumentation etwas aufgebohrt, da mir hier manches fehlte was eventuell eine Stolperfalle sein könnte.

Das Skript funktioniert mit XenServer 5.5 sowie 5.6. Ob es auch mit 6.0 kompatibel ist, habe ich bisher nicht getestet. Sollte hier jemand Erfahrungen sammeln, so bitte ich um ein kurzes Kommentar. Danke vorab!

Das modifizierte Skript – nun in der Version 2.1:

'===============
' Info Section |
'===============

'Created by Jeff Riechers.  jeff.riechers@cliftoncpa.com
'Modified by Juergen Riederer.   juergen.riederer@concept-br.de
'Updated 12-13-2011 - Version 2.1


'Changelog
'=========
'Changes in Version 2.0
'Version 2.0 includes checking for Program Files(x86) or alternate install directory for XenCenter along with options for capturing info to log files.

'Changes in Version 2.1
'VMs can now include spaces
'Backups no longer overwrite the old one. The filename now include the date of creation.


'Instructions
'============
'> Install XenCenter 5.6 on your device you will be backing up to.
'> Modify the settings below to match your environment
'> Save this vbs file in your XenCenter folder (mostly:  C:\Progra~1\Citrix\XenCenter\)  as backupxen.vbs
'> Create a serverlist.txt file in the same directory with your VMs listed by name. Notice: 1 server per line, these are case-sensitive !!!
'> Schedule this vbs with task scheduler with the following syntax -"cscript.exe {Path to Script}\backupxen.vbs or use an batch file if you prefer
'> Each run of the backup will automatically create an new backup on the disk with creation date. 


'Notes
'=====
'Backup path must include trailing slash.  And the directory must exist.
'There must be sufficient space on your storage for snapshotting to work or the vm will not be backed up.
'The account running the script must have modify rights to the C:\Progra~1\Citrix\XenCenter\ folder.
'Set ErrorChecking to "On" to create a Xenbackup.log with info such as uuids returned and commands passed to the VM
'Modify the strDirectory path if you installed XenCenter to a different drive or directory.  The default listed directory is for 64 bit machines.  If the directory does not exist the system assumes it is installed into the default Program Files directory.
'Set BADSAN to On if you have an LSI based SAN that does not properly cleanup from snapshot removals, this will force the system to pause your VM, do an offline leaf coalesce, and then bring it back online.  This is a relatively quick process, so if you run this after hours it should not affect your users.  But as always test this before running it on your production server with live data.  Set the serveruuid to the host-uuid of your primary XenServer.
'Use DOS-Format for folders with spaces (e.g. C:\Progra~1)
'server and serveruuid settings equals the pool master in a cloud environment


'===================
' Settings Section |
'===================

'Modify these variables for your environment

user= "root"
pw= "password"
server="192.168.1.100"
backuppath = "W:\"
strDirectory = "C:\Progra~1\Citrix\XenCenter"
ErrorChecking = "On"
BADSAN = "Off"
serveruuid = ""




'======================================
'Do not change anything after this line.

vmuuid = ""
servername = ""
Set WshShell = CreateObject ("Wscript.shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strDirectory) Then
	XEPATH = strDirectory
Else
XEPATH = "C:\Progra~1\Citrix\XenCenter"
End If

strtemp= ""

	Set objSN = objFSO.OpenTextFile(XEPATH&"\serverlist.txt", 1)
If ErrorChecking = "On" Then
	If objFSO.FileExists(XEPATH&"\XenBackup.LOG") Then
		objFSO.DeleteFile XEPATH&"\XenBackup.LOG", True
	Else
		
	End If
Set objFile2 = objFSO.CreateTextFile(XEPATH&"\XenBackup.LOG", 2)
Else
End If	

Do Until objSN.AtEndOfStream

	servername = objSN.ReadLine
If ErrorChecking = "On" Then
	objFile2.WriteLine servername
Else
End If	
	BuildTemp (XEPATH&"\xe.exe -s "&server&" -u "&user&" -pw "&pw&" vm-list name-label="&Chr(34)&servername&Chr(34)&" --minimal")
If ErrorChecking = "On" Then
	objFile2.WriteLine strtemp
Else
End If	
If BADSAN = "On" Then
	vmuuid = strtemp
Else
End If
	BuildTemp (XEPATH&"\xe.exe -s "&server&" -u "&user&" -pw "&pw&" vm-snapshot new-name-label=backup uuid="&strtemp)
If ErrorChecking = "On" Then
	objFile2.WriteLine strtemp
Else
End If	
	strDayOfMonth = right("0" & day(date()),2)
	strMonth = right("0" & month(date()),2)
	strYear = right(year(date()),2)
	Backup (XEPATH&"\xe.exe -s "&server&" -u "&user&" -pw "&pw&" template-export template-uuid="&strtemp&" filename="&Chr(34)&backuppath&servername&"_"&strDayOfMonth&strMonth&strYear&".xva"&Chr(34))
If ErrorChecking = "On" Then
	objFile2.WriteLine strtemp
Else
End If	
	Backup (XEPATH&"\xe.exe -s "&server&" -u "&user&" -pw "&pw&" template-uninstall template-uuid="&strtemp&" --force")
If BADSAN = "On" Then
	Backup (XEPATH&"\xe.exe -s "&server&" -u "&user&" -pw "&pw&" host-call-plugin host-uuid="&serveruuid&" plugin=coalesce-leaf fn=leaf-coalesce args:vm_uuid="&vmuuid)
Else
End If
Loop
	objSN.Close

Function BuildTemp(runline)
	
	Set objFile = objFSO.CreateTextFile(XEPATH&"\Temp.bat", 2)
	
		objFile.WriteLine "@echo off"
		objFile.WriteLine runline
		objFile.Close
	return=WshShell.run (XEPATH&"\Temp.bat > temp.txt", 2, True)
	Set objFile = objFSO.OpenTextFile(XEPATH&"\temp.txt", 1)
		strtemp = objFile.ReadAll
		objFile.Close
	objFSO.DeleteFile XEPATH&"\temp.txt", True
	objFSO.DeleteFile XEPATH&"\Temp.bat", True
End Function

Function Backup(runline)
If ErrorChecking = "On" Then
	objFile2.WriteLine runline
Else
End If		
	return=WshShell.run (runline, 2, True)
End Function