in

Bunker Hollow

Matt Williamson's home on the web, welcome.

Sponsored Link

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.

Added Allan Kjaer's Log File Update (2/10/2010)

  • Allan was kind enough to send me an update to the script which will keep a running log file.  I've added it as a separate PowerShell script, be sure to modify VmBackup.bat with the new script name if you want to use it.

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
 

Arnaud Bigot said:

great script but SOMETIMES, VM won't start after backup :(

Can you force VM to start even if a problem occurs in backup.

THX

January 9, 2009 4:51 AM
 

Matt Williamson said:

I've never had a VM fail to start so I don't think I can help, but if you figure out the problem or discover any tips let us know!

January 9, 2009 8:01 AM
 

wdj said:

The script don't stop the server (windows 2003) when I not logged in.

Who can help my to fix it?

January 13, 2009 11:21 AM
 

cgh said:

Great script Matt.  Exactly what i was looking for.  Had i not found this our only other option would have been to upgrade to backupexec 12.5 and buy the agent for hyper-v.  But now with this i just backup the VM files to a share on the backupexec server and then backup that share.  Works like a charm.

Thank you very much!!!

March 19, 2009 8:32 AM
 

Paolo said:

Great script Matt

March 22, 2009 2:20 PM
 

Paolo said:

Great script Matt. Is it possible in the script change the computer name and tcp/ip nic for make a test with the backup VM while original is running too.

Thank you very much !!!!!!

March 22, 2009 2:27 PM
 

Matt Williamson said:

Thanks.  If you want to backup while the VMs are running, this script won't do it.  You'll have to investigate more about Snapshots and the Diskshadow utility.

March 22, 2009 9:43 PM
 

Paolo said:

Hi Matt, sorry I made a mistake . I need to change computer's name and tcp/ip after made a backup ( no while VMs are running) . I mean that after backup I can't running both , for the duplicate name and duplicate tcp/ip. I know that I can change these setting manually .I was looking for change these setting with a scripit.

Thank you very much again.

Paolo

March 23, 2009 12:45 AM
 

dbauer said:

Hey I put this on our 2008 server. the problem I have is it runs the shut and start commands, but it does not copy the vhd files. It just stops the servers pauses for a few minutes maybe 5-10 then brings them back up and does not copy the vhd files. Any ideas why it is doing this?

April 10, 2009 10:30 AM
 

Matt Williamson said:

Check the copy lines in the VmBackup.bat file, something is wrong.  Check your paths and file names carefully.  Maybe it's permissions, when you create your Scheduled Task whichever user you run the script as will need write permission to where ever you are copying the files.

April 10, 2009 10:40 AM
 

Stuart said:

Can I use this script from a desktop to start and stop remote VMs? It seems I need to run it from the host server to have it work, correct?

May 21, 2009 3:24 PM
 

Matt Williamson said:

I would bet there's a way to modify the script to execute the commands against a different machine, but I really don't know.

May 22, 2009 8:17 AM
Powered by Community Server (Non-Commercial Edition), by Telligent Systems