Buenas,
Después de armar un entorno de desarrollo con Microsoft Virtual Server 2005 que tiene varias maquinas de prueba, a saber:
– Domain Controller
– Microsoft SQL Server 2005
– Exchange 2007
– Team Foundation Server
– Live Communication Server
– MOSS 2007
– Etc.
Una de las cosas que menos esperas es un problema de hardware o algún otro error que te obligue a instalar todo y reconfigurarlo nuevamente (el problema no es instalar, sino configurar, hacer que todos se vean entre sí, certificados, seguridad, etc.) yo tampoco espero ganarme la lotería, pero también puede pasar. Es por eso que mi amigo Vicente, me pidió que veamos una forma de backupear resguardar en idioma natural todas estas virtual machines por si las moscas.
Pues bien, el primer lugar donde busqué fue en el blog Virtual PC Guy’s WebLog, donde encontré muchísima información relacionada con VM. Dentro del mismo hay varios scripts que permiten tareas parecidas, y luego de modificar un poco algunos llegue a la solución final que permite
1. Salvar el estado de una VM, solo si esta en ejecución.
2. Copiar los archivos relacionados con la VM a un directorio de BackUp.
3. Restaurar la VM.
El script es bastante descriptivo por sí mismo, y ayudado por una pequeña tarea programada nocturnamente, ya tenemos listo nuestro esquema de resguardo para las maquinas virtuales.
‘Script Begins
On Error Resume Next
Dim objShell
Dim virtualServer
Dim vm
Dim saveTask
Dim vhd
Dim line
‘define BackUp folders
VM_Disc_E = «F:Backup disco E»
VM_Disc_F = «E:Backup disco F»
‘Create Shell Object
Set objShell = CreateObject («WScript.Shell»)
‘Connect to Virtual Server
Set virtualServer = CreateObject(«VirtualServer.Application»)
‘1ro stop all VM
call StopAndSaveVM(«SQL01» , VM_Disc_E)
call StopAndSaveVM(«SPS01» , VM_Disc_E)
call StopAndSaveVM(«TFS01» , VM_Disc_F)
call StopAndSaveVM(«LCS01» , VM_Disc_F)
call StopAndSaveVM(«EXCH01», VM_Disc_F)
call StopAndSaveVM(«DC01» , VM_Disc_E)
‘2do start vm
call StartVM(«DC01»)
call StartVM(«SQL01»)
call StartVM(«SPS01»)
call StartVM(«TFS01»)
call StartVM(«LCS01»)
call StartVM(«EXCH01»)
‘ Garbage Collector
Set objShell = nothing
Set virtualServer = nothing
Set vm = nothing
Set saveTask = nothing
Set vhd = nothing
‘Script ends
Sub StopAndSaveVM(VirtualMachineName, BackUpFolder)
‘Get virtual machine from command-line parameter
set vm = virtualServer.FindVirtualMachine(VirtualMachineName)
‘ validate valid VM and VM is running
‘ vmVMState_Running: The virtual machine is restored and turned on, not suspended. = 5,
if vm is nothing then exit sub
if vm.state <> 5 then exit sub
‘Save state the virtual machine
set saveTask = vm.Save
‘Loop waiting for task completion – and display status
while not saveTask.isComplete
WScript.Sleep 1000
wend
‘Copy virtual hard disks and undo disks
for each vhd in vm.HardDiskConnections
line = «%comspec% /c copy « & chr(34) & vhd.HardDisk.file & chr(34) & » « & chr(34) & BackUpFolder & chr(34)
objShell.Run line,1 , True
next
‘Copy .VMC and .VSV files
line = «%comspec% /c copy « & chr(34) & vm.File & chr(34) & » « & chr(34) & BackUpFolder & chr(34)
objShell.Run line, 1 , True
line = «%comspec% /c copy « & chr(34) & vm.SavedStateFilePath & chr(34) & » « & chr(34) & BackUpFolder & chr(34)
objShell.Run line ,1 , True
‘try with %comspec% /c
End Sub
Sub StartVM(VirtualMachineName)
‘Get virtual machine from command-line parameter
set vm = virtualServer.FindVirtualMachine(VirtualMachineName)
‘ validate valid VM and VM is saved
‘ vmVMState_SavedThe virtual machine is off but the guest is saved. = 2,
if vm is nothing then exit sub
if vm.state <> 2 then exit sub
‘Once everything is done – startup the virtual machine
vm.Startup
End Sub
Saludos
El Bruno
Referencias
Microsoft Virtual Server 2005 COM Interface Reference
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msvs/msvs/ivmvirtualmachine_state.asp
VMVMState
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msvs/msvs/ivmvirtualmachine_state.asp
Virtual PC Guy’s WebLog
http://blogs.msdn.com/Virtual_PC_Guy/