cancel
Showing results for 
Search instead for 
Did you mean: 

Protection

Hi - hopefully some will find this useful.

It's an attempt at a tidier, and more readable backup_exit_notify script, and thus hopefully also more easily maintained.

FYI - NetBackup enmbedded 'scripts' such as this are overwritten by upgrades and patches, so... it is usually a good idea to keep a copy of your site specific scripts in another location - and to add steps to your upgrade/patch procedures/plans to have a task to put your site specific scripts back following an upgrade/patch.

N.B: It is likely, as with the default supplied script, that you will need to amend the usage of 'blat' to suit your implementation of blat, and/or usage of any other SMTP/email client.

HTH.

@echo off
setlocal enabledelayedexpansion

REM ************************************************************************************************
REM *    File:      backup_exit_notify.cmd
REM *    Purpose:   Called by NetBackup, when backup jobs complete.
REM *
REM *    Vers    Date           Who    Description
REM *    ----    ----           ---    -----------
REM *    v0.01   23-APR-2015    sdo    First draft.
REM ************************************************************************************************
REM *    Disclaimer
REM *    ----------
REM *    This script was developed and tested using:
REM *        - Windows 2008 R2 SP1
REM *        - NetBackup Server v7.6.0.4
REM *
REM *    This script is not endorsed, nor has it been tested, by Symantec.  Neither the author, nor
REM *    Symantec, shall accept any liability from any harm, loss, or damage caused by the use
REM *    and/or mis-use of this script, either in its original form, or in a modified form.  Use of
REM *    this script in its original, or modified, form is entirely at the end user's own risk.
REM *    This script is furnished on an example basis only, and may not be suitable for any given
REM *    environment.  
REM ************************************************************************************************
REM *    Sharing
REM *    -------
REM *    This script is free to share and modify, as long as this entire header section is kept with
REM *    the script, and maintained and updated appropriately as any changes are made.
REM ************************************************************************************************
REM *    Notes
REM *    -----
REM *    1) Mutiple instances of this script could be instantiated concurrently by NetBackup at any
REM *       given time.
REM *
REM *    2) Therefore this script has to somehow make its own working file names unique.
REM *
REM *    3) Hence we use a double random number in an attempt to make our working file names unique.
REM *
REM *    4) When this script is run by NetBackup, then it runs as root, which in Windows means that
REM *       this script runs a "NT_System_Authority".
REM *
REM *    5) Therefore, when this script runs, the value in `!temp!` will probably point towards:
REM *        C:\Windows\Temp
REM *
REM *    6) Which means that if this script has a bug, or the call to bperror fails, or the call to
REM *       the emailer fails, then small temporary files will begin to accumulate in the folder
REM *       pointed to by `!temp!`, i.e. files will begin to accumulate in:
REM *         C:\Windows\Temp
REM *
REM *    7) NetBackup does not wait for the completion of this script, and therefore does not check
REM *       for, nor take any action based upon, the final exist status of this script.
REM ************************************************************************************************
REM ************************************************************************************************


REM ************************************************************************************************
REM ************************************************************************************************
REM *** Build some file names used by this script...

set z_path=%~dp0
set z_name=%~n0

set z_random=!random!-!random!

set z_file_csv=!temp!\!z_name!.csv
set z_file_log=!temp!\!z_name!-!z_random!.log
set z_file_txt=!temp!\!z_name!-!z_random!.txt

if exist "!z_file_log!"  del "!z_file_log!"
if exist "!z_file_txt!"  del "!z_file_txt!"


REM ************************************************************************************************
REM ************************************************************************************************
REM *** Were we called by NetBackup?

if /i not "!IS_NETBACKUP_DAEMON!"=="yes" (
  echo !date! !time:~0,8!  Script can only be called by NetBackup...
  exit /b 1
)


REM ************************************************************************************************
REM ************************************************************************************************
REM *** Append to a log of all backup job statii...

(echo !date! !time:~0,8!,!computername!,%~1,%~2,%~3,%~4,%~5,%~6,%~7,%~8)>>"!z_file_csv!"


REM ************************************************************************************************
REM ************************************************************************************************
REM *** If not "done_trying" then exit out ?
REM *** N.B: Comment out this line if you want all 'attempts' to send an email...

if "%~7"=="0"  exit /b


REM ************************************************************************************************
REM ************************************************************************************************
REM *** If success, or partial, then do we need to email ?
REM *** N.B: Comment out these lines if you also want emails to be sent for backup status 0 or 1...

REM if "%~5"=="0"  exit /b

REM if "%~5"=="1"  exit /b


REM ************************************************************************************************
REM ************************************************************************************************
REM *** Build an email message body text file...

call :r_txt ""
call :r_txt "Backup job details:"
call :r_txt "  P1 - Client:         %~1"
call :r_txt "  P2 - Policy:         %~2"
call :r_txt "  P3 - Schedule:       %~3"
call :r_txt "  P4 - Schedule Type:  %~4"
call :r_txt "  P5 - Status:         %~5"
call :r_txt "  P6 - Stream:         %~6"
call :r_txt "  P7 - Done Trying:    %~7"
call :r_txt "  P8 - Primary JobID:  %~8"

if not "%~5"=="0" (
  if not "%~5"=="1" (
    call :r_txt ""
    call :r_txt ""
    call :r_txt "Backup status `%~5` error is described as:"

    echo+ >>"!z_file_txt!"

    bperror -S %~5 -r >>"!z_file_txt!" 2>&1

    set z_sts=!errorlevel!

    if not !z_sts!==0 (
      call :log "...call to bperror failed, status `!z_sts!`..."
      goto :end
    )

    echo+ >>"!z_file_txt!"
  )
)

call :r_txt ""
call :r_txt "[end]"


REM ************************************************************************************************
REM ************************************************************************************************
REM *** Send an email...

set z_recipient=someone@whocares.com

if "%~5"=="0" (
  set z_subject=Backup job success for client %~1 in policy %~2...
) else (
  if "%~5"=="1" (
    set z_subject=Backup job partial for client %~1 in policy %~2...
  ) else (
    set z_subject=Backup job failed for client %~1 in policy %~2, with status %~5...
  )
)

call :log ""
call :log "Calling emailer..."

blat "!z_file_txt!" -s "!z_subject!" -t "!z_recipient!" -p myprofile -debug >>"!z_file_log!" 2>&1

set z_sts=!errorlevel!

if not !z_sts!==0 (
  call :log "...call to email failed, status `!z_sts!`..."
  goto :end
)


REM ************************************************************************************************
REM ************************************************************************************************
REM *** If we get here, then the call to emailer appeared to be successful, so now we can remove...
REM *** ...the email body text file, and also remove the log file for the active running instance...
REM *** ...of this script...

if exist "!z_file_log!"  del "!z_file_log!"
if exist "!z_file_txt!"  del "!z_file_txt!"


REM ************************************************************************************************
REM ************************************************************************************************

:end

exit /b


REM ************************************************************************************************
REM ************************************************************************************************
REM *** Write some text to the email body file,...
REM *** ...but also capture it in the log file for the running instance of this script...

:r_txt
(echo !date! !time:~0,8!  %~1)>>"!z_file_log!"
(echo !date! !time:~0,8!  %~1)>>"!z_file_txt!"
goto :eof


REM ************************************************************************************************
REM ************************************************************************************************

:log
(echo !date! !time:~0,8!  %~1)>>"!z_file_log!"
goto :eof