Interim Backup Solution for Hyper-V

The organization I belong to is part of the Hyper-V rapid deployment program which is a program run by Microsoft to get organizations to deploy production virtual servers on early RC and RTM code so that when the code is deployed to the rest of the world, they can note that they have X number of organizations already running it in production.  If you have been to any release event, these are the early adopter stories.  It also comes with great support from Microsoft and Partners who really want the early implementations succeed.  If you ever get the chance to be part of a TAP or RDP, I would recommend it.

So back to the backup script.  Being part of the rapid deployment program, and the desire to test out Hyper-V with production based virtual servers, we needed a way to protect the data that is the servers incase of problems.  Of course we could have just put a backup agent on the child servers and completed file based backups, but that can be a scheduling and resource nightmare once you get a large number of child servers on a particular parent server.  It also did not meet our standard of how we backup our servers in our Virtual Server 2005 R2 environments.  So, to mimic the success we had with backing up our Virtual Server 2005 R2 environment using the VShadow SDK, we looked for a similar solution. 

What we discovered right off the bat is, the VShadow SDK does not work on Windows 2008.  After a little reading of Windows 2008 documentation and a some Googling we came to realization that there was  a new and improved utility called DiskShadow.  What we came up with is a fairly simple compilation of a Diskshadow script and CMD files that takes live snapshots (using the VSWriter) of child servers, mounts those snapshots and then uses rob0copy to copy the files to an alternate location for backups to tape that happen during the daytime hours. 

Below are the scripts that consist of the backup procedures as it stands today.  This has already gone through many different derivatives, and I suspect this will evolve over time or maybe as others add their input.   Randy Pausch makes a good point about feedback from your peers and the inability to grow without it.  Please feel free to leave feedback.

 

Take note of line wraps.

VSBACKUP.CMD

*****************************************

REM Date Formatting
FOR /F "TOKENS=1* DELIMS= " %%A IN (‘echo %date%’) DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN (‘echo %date%’) DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN (‘echo %CDATE%’) DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ eol= " %%A IN (‘echo %CDATE%’) DO SET yyyy=%%B
SET dateStamp=%mm%%dd%%yyyy%
c:
cd vsbackup
Echo %date% %time% >>c:\vsbackup\logs\%dateStamp%_%computername%_VSBackup.log
REM Execute DiskShadow script
diskshadow /s c:\vsbackup\vsbackup_step.dsh >>c:\vsbackup\logs\%dateStamp%_%computername%_VSBackup.log
Echo %date% %time% >>c:\vsbackup\logs\%dateStamp%_%computername%_VSBackup.log

 

VSBACKUP_STEP.DSH (coordinates backup proceedure)

*****************************************

#Diskshadow script file

#Maintenance task to clear all previous Volume Shadow Copies
DELETE SHADOWS ALL

#Allow for shadow copies to be persist across program exit, reset or reboot.
SET CONTEXT PERSISTENT

#Cab location for process
SET METADATA c:\vsbackup\cab\Backup.cab

#Verbose on because knowing what is going on is good.
SET VERBOSE ON

#Start Backup process
BEGIN BACKUP

#Alias volumes for easier use in process. 
#Both Drive letters and volume GUIDs are used.
#Volume GUIDs are used for those virtual guests
# on volumes with out a drive letter.
ADD VOLUME D: ALIAS G1
ADD VOLUME \\?\Volume{ac9658ca-fa80-11dc-85fa-001b785788b0}\ ALIAS G2
ADD VOLUME H: ALIAS G3
ADD VOLUME J: ALIAS G4
ADD VOLUME \\?\Volume{6019c063-1540-11dd-b48d-001b785788b1}\ ALIAS G5

#Create Snaps
CREATE

#Maintenance script that deletes previous days file backup from
#secondary backup drives
EXEC c:\vsbackup\backupscript_maint.cmd

#Exposing of shadows and coping to secondary backup drives
#for each volume alias above.
#Using UNEXPOSE as a fallback proceedure incase Volume Shadow
#Copy malfunctions.
EXPOSE %G1% X:
EXEC c:\vsbackup\backupscript_step.cmd
UNEXPOSE X:
EXPOSE %G2% X:
EXEC c:\vsbackup\backupscript_step.cmd
UNEXPOSE X:
EXPOSE %G3% X:
EXEC c:\vsbackup\backupscript_step.cmd
UNEXPOSE X:
EXPOSE %G4% X:
EXEC c:\vsbackup\backupscript_step.cmd
UNEXPOSE X:
EXPOSE %G5% X:
EXEC c:\vsbackup\backupscript_step.cmd
UNEXPOSE X:

END BACKUP
#End of script

 

BACKUPSCRIPT_MAINT.CMD

*****************************************

FOR /F "TOKENS=1* DELIMS= " %%A IN (‘echo %date%’) DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN (‘echo %date%’) DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN (‘echo %CDATE%’) DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ eol= " %%A IN (‘echo %CDATE%’) DO SET yyyy=%%B
SET dateStamp=%mm%%dd%%yyyy%
Echo %date% %time% >> c:\vsbackup\logs\%dateStamp%_%computername%_maint.log
REM Delete directory with previous nights backup.
rd /s /q f:\%computername%
Echo %date% %time% >> c:\vsbackup\logs\%dateStamp%_%computername%_maint.log

 

BACKUPSCRIPT_STEP.CMD

******************************************

REM Reusable file copy proceedure for mounted shadows.

FOR /F "TOKENS=1* DELIMS= " %%A IN (‘echo %date%’) DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN (‘echo %date%’) DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN (‘echo %CDATE%’) DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ eol= " %%A IN (‘echo %CDATE%’) DO SET yyyy=%%B
SET dateStamp=%mm%%dd%%yyyy%
Echo %date% %time% >> c:\vsbackup\logs\%dateStamp%_%computername%_xcopy.log

REM Check to see if backup drive and directory exist.
if not exist f:\%computername% md f:\%computername%

REM Copy mounted volume data to secondary backup drives.
REM I used Robocopy for its logging and retry capabilities.
REM Any Copy proceedure would work
REM Note the exclusions that could cause problems with coping files.
robocopy X:\ f:\%computername%\ /E /V /NP /LOG+:"c:\vsbackup\logs\%dateStamp%_%computername%_xcopy.log" /ZB /R:2 /W:30 /XJD /XJF /XD $RECYCLE.BIN SYSTEM* MP* /XA:SHO
Echo %date% %time% >> c:\vsbackup\logs\%dateStamp%_%computername%_xcopy.log

So, here is the code we are using to backup the servers.  In the next post, I will go through the scripts in more detail.  Until then, send your feedback.

Rob

This entry was posted in Hyper-V. Bookmark the permalink.

11 Responses to Interim Backup Solution for Hyper-V

  1. Unknown says:

    Hello Rob,
     
    Nice article, however diskshadow.exe always gives me the error:
    COM call "CreateVssBackupComponents" failed.
    Access is denied.
     
    It gives this error on all lines, whether I want to list writers or flush all previous backups. Google\’s no help to me, perhaps you know what\’s going wrong?

  2. Dan says:

    Thanks for this… it saved me a good deal of time.  We used a similar solution using the vss sdk with our VS2005 VMs, and I was looking to do the same thing in Hyper-V.  This seems to do the trick.
     
    I did add a line to in the diskshadow script to verify the availability of the Hyper-V VSS writer.  It looks like this…
     
    # verify the "Microsoft Hyper-V VSS Writer" writer will be included in the snapshotwriter verify {66841cd4-6ded-4f4b-8f17-fd23f8ddc3de}create
     
    see http://blogs.technet.com/m2/archive/2008/04/17/invoking-diskshadow-to-back-up-a-virtual-machine-from-a-hyper-v-host.aspx
     
    Thanks again.

  3. Markus says:

    Hi,I\’d like to use the script, but just backup the files for the VMs. Having the .vhds and saved states how would I restore them?Markus

  4. Jonathan says:

    I have this script working very well. However, my question is how do we properly restore a VM with the saved state intact so that it can be started up from the saved state? The way I\’ve done it is I have a config.xml from an export and I have the following files:.\\config.xml.\\Snapshots\\.\\Snapshots\\9A9921A8-E4CB-4981-9B78-4C1D995927CB\\.\\Virtual Hard Disks\\.\\Virtual Hard Disks\\c_os.vhd.\\Virtual Hard Disks\\d_data.vhd.\\Virtual Machines\\.\\Virtual Machines\\9A9921A8-E4CB-4981-9B78-4C1D995927CB.exp.\\Virtual Machines\\9A9921A8-E4CB-4981-9B78-4C1D995927CB\\.\\Virtual Machines\\9A9921A8-E4CB-4981-9B78-4C1D995927CB\\9A9921A8-E4CB-4981-9B78-4C1D995927CB.bin.\\Virtual Machines\\9A9921A8-E4CB-4981-9B78-4C1D995927CB\\9A9921A8-E4CB-4981-9B78-4C1D995927CB.vsvWith the above files, I do an import on the folder to another Hyper-V server and it restores properly in a "Saved" state, but when I try to start the VM it gives an error when restoring the saved state:"An error occurred while attempting to change the state of virtual machine <name>. <name> failed to initialize".If I delete the saved state the VM will start up properly, but of course the OS was not properly shut down so we get the usual message indicating that on boot. Since this diskshadow method uses Hyper-V VSS\’s should we be able to restore from a saved state thanks to the .bin/.vsv files that were copied from the diskshadow snapshot?Any comments on this would be appreciated.

  5. Rolando says:

    Great post. I am currently running the script and its working like a charm. I am using this script to do backups of the vhds files from one server to another. But I do have just one issue and hope I get an answer. My virtual machine folder that contains the vhds are in my D: drive. I have two virtual machines in this server. The problem I have is that the first virtual machine goes into save mode and the files do copy, but the second virtual doesn’t go into save mode so when robocopy is trying to copy the files it says the files are being used by another program(Hyper V), Am I doing something wrong? I have the script:

    #Alias volumes for easier use in process.
    #Both Drive letters and volume GUIDs are used.
    #Volume GUIDs are used for those virtual guests
    # on volumes with out a drive letter.
    ADD VOLUME D:\ ALIAS G1
    ADD VOLUME C:\ ALIAS G2

    Then

    #Exposing of shadows and coping to secondary backup drives
    #for each volume alias above.
    #Using UNEXPOSE as a fallback proceedure incase Volume Shadow
    #Copy malfunctions.
    EXPOSE %G1% X:
    EXPOSE %G2% Y:
    EXEC c:\vsbackup\Sunday\backupscript_step.cmd
    UNEXPOSE X:
    UNEXPOSE Y:

    I have the backupscript_Step:

    REM Reusable file copy proceedure for mounted shadows.

    FOR /F “TOKENS=1* DELIMS= ” %%A IN (‘echo %date%’) DO SET CDATE=%%B
    FOR /F “TOKENS=1,2 eol=/ DELIMS=/ ” %%A IN (‘echo %date%’) DO SET mm=%%B
    FOR /F “TOKENS=1,2 DELIMS=/ eol=/” %%A IN (‘echo %CDATE%’) DO SET dd=%%B
    FOR /F “TOKENS=2,3 DELIMS=/ eol= ” %%A IN (‘echo %CDATE%’) DO SET yyyy=%%B
    SET dateStamp=%mm%%dd%%yyyy%
    Echo %date% %time% >> c:\vsbackup\Sunday\logs\%dateStamp%_%computername%_xcopy.log

    REM Check to see if backup drive and directory exist.
    if not exist \\SERVER2\Sunday\%computername% md \\server2\Sunday\%computername%

    REM Copy mounted volume data to secondary backup drives.
    REM I used Robocopy for its logging and retry capabilities.
    REM Any Copy proceedure would work
    REM Note the exclusions that could cause problems with copying files.
    robocopy “D:\Virtual Machines” \\server2\Sunday\%computername%\ /E /V /NP /LOG+:”c:\vsbackup\Sunday\logs\%dateStamp%_%computername%_xcopy.log” /ZB /R:2 /W:30 /XJD /XJF /XD $RECYCLE.BIN SYSTEM* MP* /XA:SHO
    Echo %date% %time% >> c:\vsbackup\Sunday\logs\%dateStamp%_%computername%_xcopy.log

    • A couple of things. First if you are seeing supported Hyper-V VMs going into saved state, it would seem that you do not have the correct integration components. Linux distros, and Windows NT/2000 even with the integration components will still utiilze a saved state back when queried by the Hyper-V writer on the host, but windows 2003 and above including Vista and Windows 7, should not save state when using the diskshadow script.

      As far as your copy, it looks like you are using a source of D:\ in your robocopy line. Looks like you should be using X:\ since that is the aliased mounted for the snapped D:\ drive. D:\ is live, X:\ is your point in time Hyper-V writter assisted diskshadow snapshot.

      • Rolando Montalvo says:

        Thanks for your quick reply. I was scared that since the last comment was 2009 I wasn’t going to get a reply. Thanks for taking your time I appreciate it. The first paragraph stated I don’t have the correct intregration components, can you elaborate a little bit more I don’t really understand. We are running two virtual machines of Server 2003 on Hyper V. The first one when the script runs goes into Saved mode and then the vhds gets copied over to the folder when finish it starts back up. The problem I am having is the second virtual is not going into saved mode so the vhds are not copying over.

        As far as my script in the robocopy I put the source D because the folder Virtual Machine which contains both vm’s vhd files is located in there. But I do see what you mean about the X:\ being my alias drive for the D drive. I will change it to X:\ and see how it works tonight. I thought I did it right since the first virtual machine did copy over just the second one is not. I’ll reply with the results.

      • The integration components are essentially group of software components that need to be on each VM running on Hyper-V. They assist with the VM working directly with the hypervisor and giving you much more performance and features. One feature being online backups (no saved state). The easiest way to install these is by starting up your VM and connecting to it with Hyper-V manager. Logon to the VM and then go the action menu at the top of the VM console. You will see “Insert Integration Services Setup Disk” at the bottom of the menu. Select that. It will attach the install ISO file and bring up a wizard to install the Integration Components. Say yes to all the prompts and it will ask for a reboot, sometimes 2 on certain operating systems.

  6. Rolando says:

    Thanks again, I did what you say about pressing Insert Integration Services and in the bottom it says Succeeded but nothing pops up.

    • Me says:

      You have auto-run disabled in the guest VM. With the integration services disk inserted open the computer, browse to the cd drive and launch the setup manually.

  7. alexpilotti says:

    We just released an open source Hyper-V backup solution supporting CSV and including a command line tool:

    http://hypervbackup.codeplex.com/

Leave a comment