Page 1 of 1
WBAdmin (Windows Server Backup) automation, with local drive rules...
Posted: 11 May 2017 07:21
by Merlin
Hi everyone,
I've been tasked with a massive automation project, of which WBAdmin is a small part of it. I've been trawling Google for two days now, finding snippets to assist with this. I am an amateur batch programmer and often rely on bits & pieces to help compile my own, tailored scripts.
The batch file needs to be able to backup all of the local drives (not mapped drives, optical drives, flash drives, etc.), with the exception of C:, in an Amazon Web Services Windows Server 2008 R2/2012 R2 environment.
Bonus points if it can backup each drive to its own .VHD file.
Code: Select all
@echo Off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
:Title
title WBAdmin
:Colour
color 0A
:Message.
@echo Checking for existing drives...
@echo.
set remotebackup=\\backupserver\path
for /F "usebackq eol=: skip=1 tokens=1" %%a in (
'wmic logicaldisk where "drivetype=3" get deviceid'
) do (
set "_drive=%%a:"
set "_HDL=!_HDL!!_drive:~0,2!,"
)
if "%_HDL:~-1%"=="," (set _HDL=%_HDL:~0,-1%)
if "%_HDL:~-2%"==",:" (set _HDL=%_HDL:~0,-2%)
echo Found: %_HDL%
WBADMIN START BACKUP -backuptarget:%remotebackup% -include:!_HDL! -allCritical -vssCopy -quiet -systemState
Through selective commenting, I've narrowed down the first issue to these two lines...
Code: Select all
:if "%_HDL:~-1%"=="," (set _HDL=%_HDL:~0,-1%)
:if "%_HDL:~-2%"==",:" (set _HDL=%_HDL:~0,-2%)
Any assistance rendered would be greatly appreciated.
Thank you.
Kind regards,
Nic
Re: WBAdmin (Windows Server Backup) automation, with local drive rules...
Posted: 11 May 2017 13:37
by aGerman
I assume you're looking for something like that
Code: Select all
for /F "tokens=2 delims==" %%a in (
'wmic logicaldisk where "drivetype = 3 and deviceid <> 'C:'" get deviceid /value'
) do for /F %%b in ("%%a") do set "_HDL=!_HDL!%%b,"
if defined _HDL (
set "_HDL=!_HDL:~,-1!"
echo Found: !_HDL!
REM your WBADMIN command here
)
Steffen
Re: WBAdmin (Windows Server Backup) automation, with local drive rules...
Posted: 15 May 2017 02:01
by Merlin
Hi Steffen,
Many thanks for your assistance.
When I try to run the batch file on the server, it loops through an error...
Found D:
Maximum setlocal recursion level reached.
Code: Select all
@echo Off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
REM Title
title My title goes here.
REM Colour
color 0A
REM Message.
@echo Checking for existing drives...
@echo.
set remotebackup=D:
for /F "tokens=2 delims==" %%a in (
'wmic logicaldisk where "drivetype = 3 and deviceid <> 'C:'" get deviceid /value'
) do for /F %%b in ("%%a") do set "_HDL=!_HDL!%%b,"
if defined _HDL (
set "_HDL=!_HDL:~,-1!"
echo Found: !_HDL!
WBADMIN START BACKUP -backuptarget:%remotebackup% -include:!_HDL! -allCritical -vssCopy -quiet -systemState
)
I've encountered this error with several sample scripts. Do you perhaps have any suggestions as to why this may be? Google hasn't provided much in the way of help to date.
Kind regards.
Re: WBAdmin (Windows Server Backup) automation, with local drive rules...
Posted: 15 May 2017 10:19
by aGerman
This error message doesn't make much sense for me
Do you run the script via double click or rather from within another script in a loop.
Try
Code: Select all
@echo Off &SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
:: your code here...
ENDLOCAL
Re: WBAdmin (Windows Server Backup) automation, with local drive rules...
Posted: 15 May 2017 10:33
by elzooilogico
Did you name the script as wbadmin.bat or wbadmin.cmd?
Because it will be calling itself till the end of times!
Re: WBAdmin (Windows Server Backup) automation, with local drive rules...
Posted: 15 May 2017 10:39
by aGerman
Good point elzooilogico! That's most likely the reason.
@Merlin Never ever name a batch file the same as a command that you want to use in the batch code (or in any other batch file in the same environment).
Steffen
Re: WBAdmin (Windows Server Backup) automation, with local drive rules...
Posted: 16 May 2017 02:33
by Merlin
Hi everyone,
Thanks for the input.
At present, the file is named TEST. I have tried running it by double-clicking it and by running it through a CMD window, using both .bat and .cmd extensions, on a virtualised Windows 2008 R2 Datacenter Server installation. All present the same issue.
I have amended the code to use aGerman's edit.
I have attached a screenshot showing the error.
Kind regards.
Re: WBAdmin (Windows Server Backup) automation, with local drive rules...
Posted: 16 May 2017 06:29
by Compo
The first major issue I can see with your 'code' when looking at the output is that you are setting your remotebackup to the same drive letter you are backing up!
This version will not try to back up your remotebackup or the C: drive:
Code: Select all
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set "RemoteBackUp=D:"
Echo=Checking for existing drives...
Echo=
Set "_HDL="
For /F "UseBackQ EOL=C" %%A In (`
WMIC LogicalDisk Where "DriveType = '3' And DeviceID <> '%RemoteBackUp%'" Get Caption
`) Do For %%B In (%%A) Do Set "_HDL=!_HDL!%%B,"
If Defined _HDL (Echo Found: %_HDL:~,-1%
WBAdmin Start BackUp -backupTarget:%RemoteBackUp% -include:%_HDL:~,-1% -allCritical -vssCopy -quiet -systemState
)
This version will not backup the D: drive:
Code: Select all
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set "RemoteBackUp=D:"
Echo=Checking for existing drives...
Echo=
Set "_HDL="
For /F "EOL=D" %%A In ('WMIC LogicalDisk Where "DriveType='3'" Get DeviceID'
) Do For %%B In (%%A) Do Set "_HDL=!_HDL!%%B,"
If Defined _HDL (Echo Found: %_HDL:~,-1%
WBAdmin Start BackUp -backupTarget:%RemoteBackUp% -include:%_HDL:~,-1% -allCritical -vssCopy -quiet -systemState
)
This version will not backup the remote backup drive regardless of it's letter:
Code: Select all
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set "RemoteBackUp=D:"
Echo=Checking for existing drives...
Echo=
Set "_HDL="
For /F "UseBackQ Skip=1" %%A In (`
WMIC LogicalDisk Where "DriveType = '3' And DeviceID <> '%RemoteBackUp%'" Get DeviceID
`) Do For %%B In (%%A) Do Set "_HDL=!_HDL!%%B,"
If Defined _HDL (Echo Found: %_HDL:~,-1%
WBAdmin Start BackUp -backupTarget:%RemoteBackUp% -include:%_HDL:~,-1% -allCritical -vssCopy -quiet -systemState
)
Finally this version will not backup the remote backup drive or system drive regardless of their letters:
Code: Select all
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set "RemoteBackUp=D:"
Echo=Checking for existing drives...
Echo=
Set "_HDL="
For /F "UseBackQ Skip=1" %%A In (`
WMIC LogicalDisk Where "DriveType = '3' And DeviceID <> '%RemoteBackUp%' And DeviceID <> '%SystemDrive%'" Get DeviceID
`) Do For %%B In (%%A) Do Set "_HDL=!_HDL!%%B,"
If Defined _HDL (Echo Found: %_HDL:~,-1%
Echo=WBAdmin Start BackUp -backupTarget:%RemoteBackUp% -include:%_HDL:~,-1% -allCritical -vssCopy -quiet -systemState
)
None of the above code actually needs delayed expansion if you change:
to:
You would of course in these cases change the second line to:
Code: Select all
SetLocal EnableExtensions DisableDelayedExpansion
Re: WBAdmin (Windows Server Backup) automation, with local drive rules...
Posted: 16 May 2017 08:06
by Merlin
Compo,
That is fantastic! Thank you.

Using the last of your examples, I have made the recommended changes, to avoid using Delayed Expansion.
Code: Select all
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set "RemoteBackUp=D:"
Echo=Checking for existing drives...
Echo=
Set "_HDL="
For /F "Skip=1" %%A In ('
"WMIC LogicalDisk Where (DriveType = '3' And DeviceID <> '%RemoteBackUp%' And DeviceID <> '%SystemDrive%') Get DeviceID"
') Do For %%B In (%%A) Do Set "_HDL=!_HDL!%%B,"
If Defined _HDL (Echo Found: %_HDL:~,-1%
WBAdmin Start BackUp -backupTarget:%RemoteBackUp% -include:%_HDL:~,-1% -allCritical -vssCopy -quiet -systemState
)
I am still getting an error though...
> was unexpected at this time.
...which stems from somewhere in this line:
Code: Select all
"WMIC LogicalDisk Where (DriveType = '3' And DeviceID <> '%RemoteBackUp%' And DeviceID <> '%SystemDrive%') Get DeviceID"
I'm fairly certain that it's just a case of ' or " needing to be replaced somewhere.
Kind regards.
Re: WBAdmin (Windows Server Backup) automation, with local drive rules...
Posted: 17 May 2017 05:06
by Compo
@Merlin, I have updated the code in my previous post to hopefully fix the error message you were receiving.
Re: WBAdmin (Windows Server Backup) automation, with local drive rules...
Posted: 17 May 2017 05:45
by Merlin
Compo wrote:@Merlin, I have updated the code in my previous post to hopefully fix the error message you were receiving.
Thanks Compo. It's working, and I am delighted.

Thank you for your persist assistance with this. It is greatly appreciated.
It is, however, unfortunately including %SystemDrive% (read: C:) in the Backup list.
Re: WBAdmin (Windows Server Backup) automation, with local drive rules...
Posted: 17 May 2017 08:42
by Compo
What happens if you use the first code example I provided? it was designed to specifically ignore the C: drive!
Re: WBAdmin (Windows Server Backup) automation, with local drive rules...
Posted: 29 May 2017 05:54
by Merlin
Compo wrote:What happens if you use the first code example I provided? it was designed to specifically ignore the C: drive!
Hi Compo,
My apologies for the delay in responding to this. I've been in hospital and then on rest leave.
I've tried the first code example both with and without delayed expansion. No matter what I try, C: is still included in the BAK, unfortunately.
Kind regards.
Re: WBAdmin (Windows Server Backup) automation, with local drive rules...
Posted: 29 May 2017 09:10
by Compo
I don't believe that the C: drive is being included due to an error in any of my coding, more because of the options you have included with your backup command.
Have you considered removing one or both of the -allCritical and -systemState options as they both have relationship to the system state and you're ignoring the system drive.
Re: WBAdmin (Windows Server Backup) automation, with local drive rules...
Posted: 30 May 2017 02:57
by Merlin
Compo wrote:I don't believe that the C: drive is being included due to an error in any of my coding, more because of the options you have included with your backup command.
Have you considered removing one or both of the -allCritical and -systemState options as they both have relationship to the system state and you're ignoring the system drive.
Hi Compo,
I shall wear the Dunce Cap for today. You are correct.
Thank you for all of your help. The script is working and it will help us tremendously.
Kind regards.