in

Bunker Hollow

Matt Williamson's home on the web, welcome.

Matt Williamson’s Blog

Personal discoveries of an IT professional.

Hyper-V VM Backup Script Batch File

This will shutdown your virtual machines, copy the .vhd virtual hard disk files (not snapshots) to a backup location, and then power your virtual machines back on.

Install the Windows PowerShell Feature

  1. You may need to install Windows PowerShell on your Hyper-V host machine, Server Manager -> Add Features.

Learn Some PowerShell Basics

  1. A PowerShell script is a plain text file with the extension .ps1.
  2. To execute a PowerShell script, open Run and enter:
    powershell.exe c:\myscript.ps1
  3. The -noexit paramater will keep the command prompt open after the script has finished executing:
    powershell.exe -noexit c:\myscript.ps1
    * don't use -noexit if you're calling a script from a batch file, execution will stop at the end of the script and never return back to the batch file.

Set PowerShell Execution Policy to Allow Unsigned Local Scripts

  1. If you run a PowerShell script for the first time, you'll probably encounter this error:
    File C:\scripts\test.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-
    help about_signing" for more details.
  2. You can read the details here, or just execute the following command within PowerShell to allow your own scripts to run:
    Set-ExecutionPolicy RemoteSigned

Download the Backup Scripts, Customize, and Schedule

  1. If you unzip the files to c:\scripts\vmbackup, then most of the paths will be correct.
  2. It's just been brought to my attention that you need to Right-click VmBackup.bat -> Properties -> Unblock, or you'll receive an error about the script not being digitally signed.  Thanks Almoonir!
  3. Modify VmNamesStop.txt with the names of your virtual machines, your domain controller should be last.
  4. Modify VmNamesStart.txt with the names of your virtual machines, your domain controller should be first.
  5. Modify VmBackup.bat to copy your vhd files to your backup location.
  6. Create a scheduled task to run VmBackup.bat regularly.

Notes (UPDATED 9/25/2008)

  1. I used a PowerShell script written by the InfoTechGuyz, nice work!
  2. I ran a full backup last night and I made the classic mistake in forgetting to turn my domain controller off last, and then power it on first.  To accomplish this, I created 2 lists of VMNames.txt, one for stopping the VMs (DC last) and the other for starting them (DC first).
  3. Snagged the cool powershell image from Tore Lervik, who created some handy powershell scripts for HyperV.

Comments

 

Dino said:

Hi, your script is a good solution, but there's a procedure\windows tools for make a backup without powerdown the machines?? With windows server 2008 i need another volume that for the moment doesn't exist...

THX a lot

November 4, 2008 9:42 AM
 

Matt Williamson said:

I think you're asking if there is a way to backup VMs without shutting down.  Yes, you can use "Snapshots" but they aren't the same as full backups and there are some horror stories out there about people relying on snapshots and then losing their entire system!  If you're able to shutdown your machines during some regular interval, I've found this is the best way to backup your virtual machines so far.

November 4, 2008 11:03 AM
 

Robert Miles said:

You may also want to add as a note that your VmBackup.bat will fail for 64-bit Hyper-V installs. It's because by default PowerShell installs to "syswow64" instead of "system32". I'd say that many are running Hyper-V on x64 so this would be useful to know.

November 18, 2008 3:04 PM
 

Matt Williamson said:

Are you sure it will fail?  I'm definitely running 64bit and it turns out I have both paths... so it either installs both by default or at some point I installed the x86 version unnecessarily.

November 18, 2008 3:20 PM
 

Al said:

Hi,

A much better way of performing (online) backups of VM's, which will also include snapshots is to use the Server 2008 command DISKSHADOW.

DISKSHADOW will allow you to take a snapshot of the volume holding the VM's, which you can then mount as a drive letter and copy of the contents.

November 21, 2008 2:33 PM
 

Steve said:

Your backup script and process works great. Thanks! My Hyper-V server is a test and development enviroment thus I also depend on my snapshots. So what do I need to do to include my snapshots in my backup?  So that I might reconstruct a virtual machine completely including snapshots.

January 4, 2009 8:40 AM
 

Matt Williamson said:

The script copies virtual machine hard disk files, which are exact representations of the hard drive up to the point of shut down.  So snapshots are unnecesarry, but if you want to include them (or anything else), add what you want copied to VmBackup.bat.

January 4, 2009 2:39 PM

Leave a Comment

(required)  
(optional)
(required)  
Add
Powered by Community Server (Non-Commercial Edition), by Telligent Systems