Using diskshadow to backup Hyper-V guests

EDIT: I wrote a Powershell script to leverage this.

Recently I required a simple backup method for our virtual servers, which our guests in our Hyper-V based office development environment.  Thanks to several knowledgeable bloggers I found information on using Windows Server 2008’s VSS writer: diskshadow.exe.  This allows an administrator to mount a shadow copy like a physical drive, and copy its contents elsewhere.  There are many other functions to diskshadow, but for this implementation I haven’t delved too far into those. All of my guests are Windows Server 2003 servers with at least Service Pack 2.

Here is the script I used, which is executed using diskshadow.exe /s scriptlocation.txt

set context persistent

<br /> #this sets up a shadow copy of the E: with the name VHDs<br /> add volume e: alias VHDs

set verbose on

<br /> #no work is done until the create command is issued<br /> create

<br /> #this uses the %VHDs% environment variable, which contains the shadow ID<br /> expose %VHDs% z:

<br /> #this batch file contains an xcopy command, and that's it<br /> exec c:hyperbakcopyvhds.bat

unexpose %VHDs%<br /> delete shadows all

The beauty of this batch file is in it’s simplicity. It doesn’t require third party backup software, just the commands executed above, along with an xcopy command. I execute this once weekly, and overwriting is fine in my case. Another layer of complexity could be added by renaming the folder when complete to name the files according to date to avoid the overwriting issue. You could also use a visual basic script to remove files older than two weeks. In that case you would need 2 times more storage capacity for backups than you need for primary storage.