<p>Below are sample scripts used to backup Hyper-V using VSS to create a snapshot. Guest operating systems that do not support VSS will be put into a saved state during the snapshot period.</p>
<p>This sample backs up images located on the <strong>E:\VS</strong> directory to a local directory at <strong>F:\VS Backups</strong>. These locations will need to be adjusted to fit your needs. For each source drive, an additional volume will need to be added to the VSS snapshot.</p>
<p>Documentation on the diskshadow.exe command is <a href="http://technet.microsoft.com/en-us/library/cc772172(WS.10).aspx">available on Technet</a>.</p>
<p>Copy each of these three files into a directory and run <strong>HyperVBackup.cmd</strong>.</p>
<p><strong>HyperVBackup.cmd:</strong></p>
<pre><code class="lang-auto">REM Use the diskshadow command to support "live" Hyper-V backup
REM though VSS
diskshadow /s diskshadow_script.txt > HyperVBackup_LOG.txt
REM Remove CAB files which are generated to support the exporting
REM of disk shadow copies (not required here)
del *.cab /Q
</code></pre>
<p><strong>diskshadow_script.txt:</strong></p>
<pre><code class="lang-auto"># Remove any existing shadow copies from crashed or errored scripts
WARNING: this will conflict with other backup software if running
at the same time.
delete shadows all
Use a persistent context so we can "map" a drive
set context persistent
Log everything
set verbose on
TODO Change this drive letter to match the location of your
VHD files
add volume E: alias HyperV1
Add additional volumes if needed
#add add volume H: alias HyperV2
Create the shadow copy
create
Expose each shadowed volume as a drive
TODO Make sure the exposed drive letter is available and used
in the backup script
expose %HyperV1% S:
Expose additional volumes as needed
#expose %HyperV2% T:
Execute the backup script (robocopy)
exec HyperVBAckup_exec.cmd
clean up the shadow copy images
delete shadows all
</code></pre>
<p><strong>HyperVBackup_exec.cmd:</strong></p>
<pre><code class="lang-auto">REM This is the script to perform the actual copy of the files
REM Use robocopy with the source set to the expose shadow drives
REM The drives are read-only, so don't try to reset the archive bit
REM TODO Set the destination location for the backups
robocopy S:\VS "F:\VS Backup" /MIR /NP /XF *.ISO /R:2 /W:5
REM Dummy command to clear the robocopy errorlevel
verify >nul
</code></pre>