cancel
Showing results for 
Search instead for 
Did you mean: 

How to run a batch in pre- and post-commands and return zero

alpo56
Level 3

Hello,

I need to run a batch file in the pre- and post-commands, but I need to be sure that the batch finishes before the backup starts.

The documentation tells this:

An exit code of zero returned to the operating system by the pre- or post-command is interpreted by Backup Exec to mean that the command completed successfully. A non-zero exit code is interpreted by Backup Exec to mean the command ended with an error

My question is, how can I finish my bat to return any code?

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions

rameshbabu_bupa
Level 3
Employee

@alpo56: I'm trying to share my thought. In general,  it is a good and safe practice to return proper error code from batch script. In the above batch script, there are four steps are being performed. For example, if the first step (i.e. the first osql command) had failed, the command processor would ignore the first osql command's failure and proceed to run the second step and so on. I assume that you wish the backup job to NOT TO START when any one of the osql command failed. If so, I think you have to write a conditional script something similar to the one shown below. The below sample will ensure to return FAILURE exit code (i.e. non-zero value) if any one osql command fails.

If you want to validate this behavior, just artificially introduce a syntax error by changing the second osql command to: osql -2 NonExistingServer -3 -45 " BACKUP DATABASE ADM1 TO DISK = 'E:\COPIADIARIA\ADM1.BAK' "   (the expectation is that the batch script to return "1" as exit code)

=================

osql -S server -E -Q " BACKUP DATABASE ADM TO DISK = 'E:\COPIADIARIA\ADM.BAK' "

IF ERRORLEVEL 1 goto QUITSCRIPT

osql -S server -E -Q " BACKUP DATABASE ADM1 TO DISK = 'E:\COPIADIARIA\ADM1.BAK' "

IF ERRORLEVEL 1 goto QUITSCRIPT

osql -S server -E -Q " BACKUP DATABASE ADM2 TO DISK = 'E:\COPIADIARIA\ADM2.BAK' "

IF ERRORLEVEL 1 goto QUITSCRIPT

osql -S server -E -Q " BACKUP DATABASE ADM3 TO DISK = 'E:\COPIADIARIA\ADM3.BAK' "

IF ERRORLEVEL 1 goto QUITSCRIPT

exit /b 0

:QUITSCRIPT
popd
REM Here the exit value 1 refers that an error encountered

exit /b 1

=================

Regards,

Ramesh Bupathy

 

 

View solution in original post

5 REPLIES 5

pkh
Moderator
Moderator
   VIP    Certified

The error code is dependent on the commands that you run in the batch file

NeerThadarai
Level 5
Employee Accredited Certified

In order for the pre command run successfully before it starts the backup job there is a check box "Run job only if pre-command is successful" check that.

For Post Command there are thre more options which you can specify as per your need.

Also you can run the pre-post command on the media server or the remote server, however, you need to make sure that the batch or command file is present on those server and the path is provided properly.

 

alpo56
Level 3

But is necessary to finish the batch file with any "errorlevel" or something else?

I have a batch file that runs various osql commands like this:

osql -S server -E -Q " BACKUP DATABASE ADM TO DISK = 'E:\COPIADIARIA\ADM.BAK' "

osql -S server -E -Q " BACKUP DATABASE ADM1 TO DISK = 'E:\COPIADIARIA\ADM1.BAK' "

osql -S server -E -Q " BACKUP DATABASE ADM2 TO DISK = 'E:\COPIADIARIA\ADM2.BAK' "

osql -S server -E -Q " BACKUP DATABASE ADM3 TO DISK = 'E:\COPIADIARIA\ADM3.BAK' "

 

Thanks!

rameshbabu_bupa
Level 3
Employee

@alpo56: I'm trying to share my thought. In general,  it is a good and safe practice to return proper error code from batch script. In the above batch script, there are four steps are being performed. For example, if the first step (i.e. the first osql command) had failed, the command processor would ignore the first osql command's failure and proceed to run the second step and so on. I assume that you wish the backup job to NOT TO START when any one of the osql command failed. If so, I think you have to write a conditional script something similar to the one shown below. The below sample will ensure to return FAILURE exit code (i.e. non-zero value) if any one osql command fails.

If you want to validate this behavior, just artificially introduce a syntax error by changing the second osql command to: osql -2 NonExistingServer -3 -45 " BACKUP DATABASE ADM1 TO DISK = 'E:\COPIADIARIA\ADM1.BAK' "   (the expectation is that the batch script to return "1" as exit code)

=================

osql -S server -E -Q " BACKUP DATABASE ADM TO DISK = 'E:\COPIADIARIA\ADM.BAK' "

IF ERRORLEVEL 1 goto QUITSCRIPT

osql -S server -E -Q " BACKUP DATABASE ADM1 TO DISK = 'E:\COPIADIARIA\ADM1.BAK' "

IF ERRORLEVEL 1 goto QUITSCRIPT

osql -S server -E -Q " BACKUP DATABASE ADM2 TO DISK = 'E:\COPIADIARIA\ADM2.BAK' "

IF ERRORLEVEL 1 goto QUITSCRIPT

osql -S server -E -Q " BACKUP DATABASE ADM3 TO DISK = 'E:\COPIADIARIA\ADM3.BAK' "

IF ERRORLEVEL 1 goto QUITSCRIPT

exit /b 0

:QUITSCRIPT
popd
REM Here the exit value 1 refers that an error encountered

exit /b 1

=================

Regards,

Ramesh Bupathy

 

 

alpo56
Level 3

@Ramesh That was what I was looking for!

Thanks!! 

Albert