19 mrt 2009

Automatic Export of XenServers ... more advanced

in my other blog with the backup script for Vm's on xenservers, there you have to use the ip of the xenmaster. but if you play around with your xens then the "master- service" will be moved around between the available xenservers.
so the first script will work until the "master-service" is changed from xenserver.

to counter this problem i've wrote a detection mechanism in vbscript.

The Script can be sheduled with Sheduled tasks in windows. In this way you can make full image backups of your servers in Xen

'-------------------------------------------------------------------------------------
' XENSERVERS CONFIG
'-------------------------------------------------------------------------------------

XENHOST = "" 'IP ADDRESS OF ONE OF YOUR XEN SERVERS

XENUSER = "root"

XENPASS = "qsdqsdsdqsdqsdqs"

VMTOEXPORT = ""

LOGFILE = "C:\XENBACKUP\ExportVm_" + VMTOEXPORT + ".txt"



strCommand = "Xe -s " + XENHOST + " -u " + XENUSER + " -pw " + XENPASS + " pool-list"EXPORTPATH = "E:\xen\"sOutFile = "C:\XENBACKUP\Exceute.cmd"



emailaddress = "email@address.com"
SMTPSERVER = "MAILSERVER"





'-------------------------------------------------------------------------------------

'CHANGE ENVIRONMENT SETTINGS

'-------------------------------------------------------------------------------------

Set WshShell = WScript.CreateObject("WScript.Shell")

Set WshEnv = WshShell.Environment("SYSTEM")

WshEnv("Path") = WshEnv("Path") & ";C:\Program Files\Citrix\XenCenter"
VMTOEXPORT = UCase(VMTOEXPORT)


'-------------------------------------------------------------------------------------
' CHECK & CHANGE XENHOST IP TO CONNECT TO MASTER XEN
'-------------------------------------------------------------------------------------

Set objExecObject = WshShell.Exec(strCommand)

Do While Not objExecObject.StdOut.AtEndOfStream
trText = strText + objExecObject.StdOut.ReadLine()
Loop

If Instr(strText, "Master IP address") > 0 Then
XENHOSTs = split (StrText,":")
XENHOST = trim(XENHOSTs(1))
end if

Set objFSO = CreateObject("Scripting.FileSystemObject")

'-------------------------------------------------------------------------------------

' INITIALIZING COMMANDS WITH RIGHT XENHOST-MASTER
'-------------------------------------------------------------------------------------

commands1 = commands1 + "xe -s " + XENHOST + " -u " + XENUSER + " -pw " + XENPASS + " vm-shutdown vm=" + VMTOEXPORT + " >> " + LOGFILE + vbcrlf

commands1 = commands1 + "xe -s " + XENHOST + " -u " + XENUSER + " -pw " + XENPASS + " event-wait class=vm name-label=" + VMTOEXPORT + " power-state=halted >> " + LOGFILE + vbcrlf

commands1 = commands1 + "xe -s " + XENHOST + " -u " + XENUSER + " -pw " + XENPASS + " vm-export filename=" + EXPORTPATH + VMTOEXPORT + ".XVA >> " + LOGFILE + vbcrlf

commands1 = commands1 + "xe -s " + XENHOST + " -u " + XENUSER + " -pw " + XENPASS + " vm-start vm=" + VMTOEXPORT + " >> " + LOGFILE + vbcrlf

commands1 = commands1 + "bmail -s " + SMTPSERVER + " -p 25 -t " + emailaddress + " -f %COMPUTERNAME% -a " + chr(34) + VMTOEXPORT + " export Backup" + chr(34) + " -m " + LOGFILE + vbcrlf commands1 = commands1 + "pause"

If objFSO.FileExists (sOutFile) then

objFSO.DeleteFile (sOutFile)
end if

Append_File (commands1)
If objFSO.FileExists (sOutFile) then

RunApp(sOutFile)
end if

'-------------------------------------------------------------------------------------

' RUN ROUTINE
'-------------------------------------------------------------------------------------

Sub RunApp(ByVal sFile)

On Error Resume Next
objShell.Run sFile , 1, true
If Err.Number = 0 Then
Else
wscript.Echo "error" + Err.Number
End If
On Error GoTo 0
End Sub

'-------------------------------------------------------------------------------------'

LOGFILE FUNCTION
'-------------------------------------------------------------------------------------
Function Append_File (txt_info)
on Error Resume Next
Dim objFileSystem, objOutputFile
Const OPEN_FILE_FOR_APPENDING = 8
strOutputFile = sOutFile
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
If objFileSystem.FileExists(strOutputFile) Then

Set objOutputFile = objFileSystem.OpenTextFile(strOutputFile, OPEN_FILE_FOR_APPENDING)
else
Set objOutputFile = objFileSystem.CreateTextFile(strOutputFile , True)
End If
objOutputFile.WriteLine(txt_info)
objOutputFile.CloseSet

objFileSystem = Nothing
end Function