WBAdmin (Windows Server Backup) automation, with local drive rules...

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Merlin
Posts: 14
Joined: 11 May 2017 07:07
Location: Cape Town, ZA
Contact:

WBAdmin (Windows Server Backup) automation, with local drive rules...

#1 Post by Merlin » 11 May 2017 07:21

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: WBAdmin (Windows Server Backup) automation, with local drive rules...

#2 Post by aGerman » 11 May 2017 13:37

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

Merlin
Posts: 14
Joined: 11 May 2017 07:07
Location: Cape Town, ZA
Contact:

Re: WBAdmin (Windows Server Backup) automation, with local drive rules...

#3 Post by Merlin » 15 May 2017 02:01

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.
Last edited by Merlin on 16 May 2017 02:17, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: WBAdmin (Windows Server Backup) automation, with local drive rules...

#4 Post by aGerman » 15 May 2017 10:19

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

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: WBAdmin (Windows Server Backup) automation, with local drive rules...

#5 Post by elzooilogico » 15 May 2017 10:33

Did you name the script as wbadmin.bat or wbadmin.cmd?

Because it will be calling itself till the end of times!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: WBAdmin (Windows Server Backup) automation, with local drive rules...

#6 Post by aGerman » 15 May 2017 10:39

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

Merlin
Posts: 14
Joined: 11 May 2017 07:07
Location: Cape Town, ZA
Contact:

Re: WBAdmin (Windows Server Backup) automation, with local drive rules...

#7 Post by Merlin » 16 May 2017 02:33

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.
Attachments
Capture.JPG
Capture.JPG (59.16 KiB) Viewed 12814 times

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: WBAdmin (Windows Server Backup) automation, with local drive rules...

#8 Post by Compo » 16 May 2017 06:29

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:

Code: Select all

Set "_HDL=!_HDL!%%B,"
to:

Code: Select all

Call Set "_HDL=%%_HDL%%%%B,"
You would of course in these cases change the second line to:

Code: Select all

SetLocal EnableExtensions DisableDelayedExpansion
Last edited by Compo on 17 May 2017 05:05, edited 1 time in total.

Merlin
Posts: 14
Joined: 11 May 2017 07:07
Location: Cape Town, ZA
Contact:

Re: WBAdmin (Windows Server Backup) automation, with local drive rules...

#9 Post by Merlin » 16 May 2017 08:06

Compo,



That is fantastic! Thank you. :D

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.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: WBAdmin (Windows Server Backup) automation, with local drive rules...

#10 Post by Compo » 17 May 2017 05:06

@Merlin, I have updated the code in my previous post to hopefully fix the error message you were receiving.

Merlin
Posts: 14
Joined: 11 May 2017 07:07
Location: Cape Town, ZA
Contact:

Re: WBAdmin (Windows Server Backup) automation, with local drive rules...

#11 Post by Merlin » 17 May 2017 05:45

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. :D Thank you for your persist assistance with this. It is greatly appreciated.

It is, however, unfortunately including %SystemDrive% (read: C:) in the Backup list.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: WBAdmin (Windows Server Backup) automation, with local drive rules...

#12 Post by Compo » 17 May 2017 08:42

What happens if you use the first code example I provided? it was designed to specifically ignore the C: drive!

Merlin
Posts: 14
Joined: 11 May 2017 07:07
Location: Cape Town, ZA
Contact:

Re: WBAdmin (Windows Server Backup) automation, with local drive rules...

#13 Post by Merlin » 29 May 2017 05:54

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.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: WBAdmin (Windows Server Backup) automation, with local drive rules...

#14 Post by Compo » 29 May 2017 09:10

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.

Merlin
Posts: 14
Joined: 11 May 2017 07:07
Location: Cape Town, ZA
Contact:

Re: WBAdmin (Windows Server Backup) automation, with local drive rules...

#15 Post by Merlin » 30 May 2017 02:57

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.

Post Reply