This page looks best with JavaScript enabled

A Quick and Dirty Hyper-V Backup Script

 ·  ☕ 3 min read

I’ve replaced this script with a much more robust script that will backup each VM, including checkpoints/snapshots. You can find that post here.

Let’s celebrate the holiday season with a quick PowerShell script to backup a Hyper-V server…or maybe you just need a quick and dirty, belt and braces backup script for your Hyper-V based VMs. It’s a small, quick script leveraging Hyper-V’s PowerShell module and some traditional command line utilities, like robocopy.

1
2
3
4
5
6
Get-VM | Stop-VM
TIMEOUT /T 20
ROBOCOPY /MIR /R:0 /W:0 /LOG:F:\vs11.log /NP /NDL F:\Hyper-V \\nas2\VM_Backups\vs11\Hyper-V
Start-VM wsus01
TIMEOUT /T 30
Start-VM mdt01

Let’s walk through the script.

1
Get-VM | Stop-VM

The first command gets a list of all the VMs on the host and then gracefully shuts them down, one after the other.

1
TIMEOUT /T 20

The script then waits for 20 seconds. This is for safety before it begins copying the VM files, and may not be necessary for you.

1
ROBOCOPY /MIR /R:0 /W:0 /LOG:F:\vs11.log /NP /NDL F:\Hyper-V \\nas2\VM_Backups\vs11\Hyper-V

Now we use Robocopy to copy the contents of the Hyper-V folder to the backup location, which in this example is on a networked attached storage box called nas2. Robocopy logs it’s actions to a log file in the root of the F:\ drive on the Hyper-V host.

1
2
3
Start-VM wsus01
TIMEOUT /T 30
Start-VM mdt01

The final section of the script starts the VMs up in a specified order with a time out in between each one. We do this instead of

1
Get-VM | Start-VM

because the “Start-VM” command won’t wait for each VM to boot before booting the next one, and with all the VMs on the host starting simultaneously the storage array will become overworked. The disadvantage of starting the VMs this way is that you’ll have to edit this script when adding new VMs. Another way of starting the VMs would be to restart the Hyper-V host after the backup and in the settings for each VM, set them to start automatically with a gradually increasing wait time for each one. See screenshot for an example.

Incremental delayed start-up for each VM

As usual, I recommend scheduling this script via Task Scheduler and having it run once a week. When scheduling a PowerShell script in Task Scheduler remember to use the following command line as the action:

1
PowerShell.exe -ExecutionPolicy Bypass F:\scripts\hyperv-backup.ps1

The script is designed to be run on each Hyper-V host locally, so you’ll want to space out the schedules to prevent each host from copying all it’s Hyper-V data to the one location at the same time and saturating the network and/or storage bandwidth.

I wrote this script with my small collection of Hyper-V hosts in mind. They all have the Hyper-V VM configuration and VHD files in the location F:\Hyper-V and have the relevant data backed up daily via the network-wide backup solution. This script runs once a week and is a belt and braces, extra backup for the VMs themselves. It does mean that there is some downtime for each of the VMs however. The advantage of having a copy of both the VHDs as well as the configuration files is that the VMs can be easily imported to another Hyper-V host and up and running very quickly.

If you have any questions or comments please leave them below.

-Mike

Share on
Support the author with