search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
An easy way to backup your Hyper-v vhd file with Powershell!
Posted in Virtual Machine Manager, Windows Powershell | 1 Comment | 7,257 views | 29/03/2009 23:27

You don’t need to use Data Protection Manager to backup your vhd files. You can use Windows Backup (with a little hack) or my method. It’s not a good method maybe, but it is easy to use and free. Just you need SCVMM Library to use this ps1 file. Now let’s see my codes:

1
2
3
4
5
6
7
8
9
10
11
12
13
Add-PSSnapin -name # Microsoft.SystemCenter.VirtualMachineManager
Get-Vmmserver localhost
$VMName = $args[0]
SaveState-vm $VMName
$strVMHost = Get-VM $VMName | Get-VirtualHardDisk | Select-Object -Property VMHost
$strVMDisk = Get-VM $VMName | Get-VirtualHardDisk | Select-Object -Property Location
$VMHost = $strVMHost.VMHost
$VMDisk = $strVMDisk.Location
$VMLocation = $VMDisk | foreach {$_ -replace ":", "$"}
$VMPath = "\\$VMHost\$VMLocation"
$VMMPath = "\\scvmm.contoso.fabrikam.com\MSSCVMMLibrary\VHDs"
xcopy $VMPath $VMMPath
Start-vm $VMName

As you can see, it’s really simple. First, I do “savestate-vm” then find vhd location and copy it to vmm library and finally do “start-vm”. $VMMPath is your SCVMM Library Path. That is just an example. Also you can add date to your vhd files name like win2008-01052009.vhd. But if you want to do that, you should use -replace for a second time. If you want to use this script, you just need to type “backup.ps1 VDS02”. Backup ps1 will save VDS02 and after backing up, will start it again.

You can add a cronjab for this script. It’s a good solution. You may need to change backup.ps1 file to backup all vm’s at the same time.


Comments (1)

Brachus

April 28th, 2011
20:14:09

Does not work when the VM being backed up has multiple VHDs attached to it.



Leave a Reply