Tuesday 3 December 2013

Hyper-V 2012 R2 Export / Backup Script

Our backup tool of choice for our virtual infrastructure is Acronis Backup and Recovery Virtual Edition 11.5. However recently we had issues with it on our new Hyper-V 2012 R2 Servers.

We needed some backups while we fixed the issues with Acronis so I created this powershell script to export all the vms on each of our Hyper-V servers.

The script loops through all virtual machines on the local Hyper-V server and exports the virtual machine to the backup location specified in $path. The exported files are stored in a folder named with todays date inside a folder with the name of the virtual machine.

Example \\[path backup location]\testVM\2013-12-03\

--Start of script--

$path = ""
$vms = Get-VM
foreach ($vm in $vms) {
    $date = Get-Date -format “yyyy-MM-dd”
    $vmname = $vm.Name
    New-Item -ItemType Directory -Path $path\$vmname\$date -Force
    Export-VM -Name $vmname -Path $path\$vmname\$date\
}


--End of script--

Notes:

This script has only been used on Hyper-V 2012 R2 servers as this version allows virtual machines to be exported while the virtual machine is running.

I have had to run the script on the local Hyper-V server. It doesn't work when ran from a remote console.

As with any script please test it before using in production as there is no warranty with this script.

Hope it helps some one.