Skip to main content
 

Logo-Text

Search
Home
  
ServerCare home > Posts > Hyper-V 2012: backup replicated virtual harddisk using PowerShell  

 

 

March 20
Hyper-V 2012: backup replicated virtual harddisk using PowerShell

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:        

  1. Check if there's a replicated image of the VM
  2. Find all the disks associated with that VM
  3. Suspend the replication
  4. Copy the virtual disks to another location, preferably adding a date to the filename…
  5. 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:

  1. The name of the VM you want to backup (-VMName)
  2. 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

. "c:\scripts\Copy-File.ps1"

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)

   

Comments

Great script

I wish I had found this sooner would have saved a lot of money on backup software.
 on 5/6/2013 20:43

Add Comment

Spam Filter *


Please enter 4982 in this field. This will help prevent SPAM.

Title


Body *


Attachments