Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Last week I posted a sample script that would save state a virtual machine, make a copy of it, and restore it (details here: https://blogs.msdn.com/virtual_pc_guy/archive/2005/02/25/380216.aspx). While this is an effective solution - it can result in a significant window of 'downtime' if the virtual machine is large - as you have to wait for the entire virtual machine to be copied before restoring it from the saved state.
So today I am going to show you how to backup a virtual machine of any size with the only downtime being how long it takes to shutdown and restart the virtual machine.
The script below will shutdown a virtual machine (specified via command line argument) and then go through each of the virtual hard disks in the virtual machine and change them to newly created differencing disks. Once this is done it then starts the virtual machine up again:
'Script Begins
'Connect to Virtual Server
Set virtualServer = CreateObject("VirtualServer.Application")
'Get virtual machine from command-line parameter
set vm = virtualServer.FindVirtualMachine(WScript.Arguments(0))
'shutdown the virtual machine
set shutdownTask = vm.GuestOS.Shutdown
'Loop waiting for task completion
while not shutdownTask.isComplete
WScript.Sleep 1000
wend
'Merge undo disks if they exist
if vm.Undoable then
set mergeTask = vm.MergeUndoDisks
'Loop waiting for task completion
while not mergeTask.isComplete
WScript.Sleep 1000
wend
end if
'Create and hookup differencing disks
for each vhd in vm.HardDiskConnections
newVhdName = Left(vhd.HardDisk.file, (len(vhd.HardDisk.file)-4)) & " diff.vhd"
set createTask = virtualServer.CreateDifferencingVirtualHardDisk(newVhdName , vhd.HardDisk.file)
while not createTask.isComplete
WScript.Sleep 1000
wend
vm.RemoveHardDiskConnection(vhd)
set newVhdConn = vm.AddHardDiskConnection(newVhdName, vhd.busType, vhd.busNumber, vhd.deviceNumber)
next
'Once everything is done - startup the virtual machine
vm.Startup
'Script ends
The advantage of this approach is that you can then copy away the base virtual hard disks at your leisure. The downsides to this approach are that it only works for guest operating systems that have Virtual Machine Additions installed, and if you run this script repeatedly you are going to end up with a very long chain of differencing drives which can only be merged when the virtual machine is offline.
Cheers,
Ben
Comments
- Anonymous
March 04, 2005
Can you merge in a paused state? - Anonymous
March 04, 2005
Hi Kevin,
No - but you can merge when the virtual machine is in a saved state.
Cheers,
Ben - Anonymous
March 15, 2005
How about
1. Save state of VM's
2. Use volume shadow copy to snapshot the disk
3. Start VM
4. At leisure copy the disk from the shadow.
Should be easy..... but not a script jock. - Anonymous
January 31, 2014
Thanks for a great script but what about if your vhdx are not so big and you want to back them up daily / in full so you do not have to merge differencing files in case of having to restore the virtual machine? Do you have a script to shutdown a vhdx, back it up to a local drive and restart the vhdx at a scheduled time weach day? Maybe even add a FILO based on the size so as to not use aup all available space of destination drive...would be nice