Wouldn't it be great if in Hyper-V 2012, you could simply copy the virtual harddisk of the replicated machine to another location automatically? Simply making a copy of such a file is not supported, as it is in a state of being replicated, and information could be added while you are copying the (usually large) file. So we need to use some trickery…
What we really want is:
- Check if there's a replicated image of the VM
- Find all the disks associated with that VM
- Suspend the replication
- Copy the virtual disks to another location, preferably adding a date to the filename…
- And once we're done, simply turn the replication back on!
If we could do all that, it would be pretty easy to make on the fly backups of our VM's without a problem!
Thanks to PowerShell we can of course do this easily, and below you'll find the script needed. The nice thing is that a backup is created from the replicated image, so there is no (performance) impact on the 'production' Hyper-V host where the source VM is running. Keep in mind that you need to have administrative rights on source, target and Hyper-V for this to work.
The script takes two arguments:
- The name of the VM you want to backup (-VMName)
- The path of the location where you want the backup copied. (-BackupPath)
The script adds the date and time of the backup to the name of the file, so that it is easy to find your files later.
Here's what it looks like 'in action':
Please note that a function 'copy-file' is being used (which can be replaced with 'copy-item' if you don't care for the progress bar being displayed when the copy is in progress…) For this to work, you will need to make sure the copy-file.ps1 file can be found by the source. In order to do that you may need to edit the source a bit at line 34: (as you can see the file location is hard coded, you could also use '.\Copy-File.ps1' if you run the script from the current location)
# Uses function copy-file
You'll find the latest version of the script and the function in this downloadable zipfile.
(latest update version 1.2 as of aug 27th 2013)