WBADMIN Demo

Earlier this year I wrote an article for REDMOND Magazine about the new backup features in Windows Server 2008 R2. I’m not going to re-hash the article here except to say it includes some sample scripts on using the WBADMIN command line tool. One of the scripts is an old-school batch file.

The batch file included code to create a directory that included a time stamp, like \\mycompany-dc01\backup\RESEARCHDC\12152009_132532 where the last portion is the month, day, year, hour, minute and second. My original code parsed out these values from the %TIME% variable.

set m=%date:~4,2%
set d=%date:~7,2%
set y=%date:~10,4%
set h=%time:~0,2%
set min=%time:~3,2%
set sec=%time:~6,2%

Unfortunately, I forgot to take into account situations where the time mght not have two digits like 1:00AM. In those situations the code tries to create a folder like \\mycompany-dc01\backup\RESEARCHDC\12152009_ 10000 which fails because of the space. To correct this I needed to add a line to check for the space in the variable, %h% and if found, define a new value with a leading 0.

rem handle blanks in hour
if /i %h:~0,1%$==$ set h=0%h:~1,1%

This sort of thing is much, much easier in Windows PowerShell, by the way. But regardless, I now have an updated batch file.

@echo off
::Demo-Backup.bat
::demonstration script using WBADMIN.EXE on a Windows Server 2008 R2 Server
::http://redmondmag.com/Articles/2010/04/01/Backup-Basics-in-Windows-Server-2008-R2.aspx?Page=1
::Revised 9:58 AM 7/26/2010

set backupshare=\\File01\backups

rem files and folders to include

set include=c:\scripts,c:\work

set m=%date:~4,2%
set d=%date:~7,2%
set y=%date:~10,4%
set h=%time:~0,2%
set min=%time:~3,2%
set sec=%time:~6,2%

rem handle blanks in hour

if /i %h:~0,1%$==$ set h=0%h:~1,1%

rem defining a new folder like \\mycompany-dc01\backup\RESEARCHDC\12152009_132532


set newfolder=%backupshare%\%computername%\%m%%d%%y%_%h%%min%%sec%

echo Creating %newfolder%

mkdir %newfolder%

rem run the backup

echo Backing up %include% to %newfolder%

wbadmin start backup -backuptarget:%newfolder% -include:%include% -quiet

rem Clear variables

set backupshare=
set include=
set m=
set d=
set y=
set h=
set min=
set sec=
set newfolder=

::EOF

You can download the batch file here. Rename it to .bat.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to FriendFeed Post to Google Buzz Post to Ping.fm Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati

This entry was posted in CommandLine, Miscellaneous, Scripting, Windows Server and tagged , , , , , . Bookmark the permalink.