@echo off setlocal enabledelayedexpansion REM ************************************************************************************************ REM * File: bpstart_notify.POLICY_NAME.bat REM * Location: ?:\Program Files\Veritas\NetBackup\bin REM * Purpose: To perform an OpsCenter Database backup (once) at the start of a backup job. REM * Author: sdo REM * REM * Vers Date Who Description REM * ---- ---- --- ----------- REM * v0.01 02-APR-2015 sdo First draft. REM * v0.02 03-APR-2015 sdo Handle being run by multiple streams. REM * v0.03 03-APR-2015 sdo Check registry keys for installation path and product name. REM * v0.04 03-APR-2015 sdo Check for existence of database backup target folder. REM * v0.05 03-APR-2015 sdo Use a random number on temporary file. REM * v0.06 03-APR-2015 sdo Handle if running from a non-multi-streamed policy. REM ************************************************************************************************ REM * Disclaimer REM * ---------- REM * This script was developed and tested using: REM * - Windows 2012 (not the R2 variant) REM * - NetBackup Client v7.6.1.1 REM * - NetBackup OpsCenter Server v7.6.1.1 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. This REM * script is furnished on an example basis only, and it is expected that end user's will have REM * to modify at least one portion of this script, notably the OpsCenter database backup target REM * folder variable named 'z_target'. REM ************************************************************************************************ REM * Sharing REM * ------- REM * This script is free to share and modify, as long as this entire header section is maintained REM * and updated appropriately, as you make changes. REM ************************************************************************************************ REM * Notes REM * ----- REM * 1) The backup policy/job related to this script needs to be single streamed, i.e. the backup REM * policy needs to be configured as: REM * - either a non-multi-streamed policy (i.e. 'Allow multiple data streams' is disabled) REM * - or a multi-streamed policy with the policy job limit set to 1... REM * - or a multi-streamed policy where the backup client's 'Client Attributes' option REM * 'Maximum data streams' is set to 1. REM * REM * N.B: Very strange things will happen if the policy is multi-streamed with no stream limit REM * set on either the backup policy or the backup client. REM * REM * 2) Years ago (and I'm not sure if this is still true), there was no guarantee that any REM * particular stream would run in any particular order. For example, if a backup policy REM * had two streams, one for C:, and one for D:... then there was no guarantee that, for any REM * given run of the backup policy, that stream 1 would always be C:, and that stream 2 REM * would always be D:. This is why the OpsCenter Database backup is actually performed REM * during stream 1, because we need to be sure that the database is backed-up before any REM * stream actually starts moving data - because stream 1 might be the 'D:' drive, which is REM * where (in this example scenario) the backup target folder could be located. REM ************************************************************************************************ set z_path=%~dp0 set z_name=%~n0 set z_file_log=!z_path!!z_name!.log set z_file_tmp=!z_path!!z_name!.tmp.!random! if exist "!z_file_tmp!" del "!z_file_tmp!" REM ************************************************************************************************ REM ************************************************************************************************ REM *** Modify this next line to point to your OpsCenter database backup target folder... set z_target=F:\NBU-OPSC-DB-BACKUP REM ************************************************************************************************ REM ************************************************************************************************ REM ************************************************************************************************ REM *** If we have any issues, this is the status code to abort with... set z_abort=2 REM ************************************************************************************************ REM *** Check backup job/stream environment variables... if "!is_netbackup_daemon!"=="" ( call :log "Envrionment variable `IS_NETBACKUP_DAEMON` is empty or not found, script aborting..." if not "%~6"=="" echo !z_abort! >>"%~6" exit /b !z_abort! ) if /i not "!is_netbackup_daemon!"=="yes" ( call :log "Script is not being run by a NetBackup daemon, script aborting..." if not "%~6"=="" echo !z_abort! >>"%~6" exit /b !z_abort! ) if not "!stream_number!"=="" ( if not "!stream_number!"=="1" ( call :log "Script instance `!stream_number!`, process PID `!stream_pid!`, is not the first stream, script exiting..." REM *** Here we need to exit cleanly, so as not to disrupt other streams... if not "%~6"=="" echo 0 >>"%~6" exit /b 0 ) ) REM ************************************************************************************************ REM *** Check to see whether a component of NetBackup OpsCenter is installed... reg query HKLM\SOFTWARE\Symantec\OpsCenter\Server /v "InstallDir" >"!z_file_tmp!" 2>&1 set z_sts=!errorlevel! if not "!z_sts!"=="0" ( call :log "Unable to query registry key `InstallDir`, script aborting..." if exist "!z_file_tmp!" del "!z_file_tmp!" if not "%~6"=="" echo !z_abort! >>"%~6" exit /b !z_abort! ) set z_installdir= for /f "tokens=1,2,* skip=1" %%a in ('type "!z_file_tmp!"') do ( if "%%a %%b"=="InstallDir REG_SZ" ( set z_installdir=%%c ) ) if exist "!z_file_tmp!" del "!z_file_tmp!" if "!z_installdir!"=="" ( call :log "A component of NetBackup OpsCenter Server is not installed on this server, script aborting..." if not "%~6"=="" echo !z_abort! >>"%~6" exit /b !z_abort! ) if not exist "!z_installdir!" ( call :log "Unable to locate `InstallDir` path of `!z_installdir!`, script aborting..." if not "%~6"=="" echo !z_abort! >>"%~6" exit /b !z_abort! ) REM ************************************************************************************************ REM *** Check to see whether Symantec NetBackup OpsCenter Server itself is installed... reg query HKLM\SOFTWARE\Symantec\OpsCenter\Server /v "ProductName" >"!z_file_tmp!" 2>&1 set z_sts=!errorlevel! if not "!z_sts!"=="0" ( call :log "Unable to query registry key `ProductName`, script aborting..." if exist "!z_file_tmp!" del "!z_file_tmp!" if not "%~6"=="" echo !z_abort! >>"%~6" exit /b !z_abort! ) set z_productname= for /f "tokens=1,2,3,4,5,6 skip=1" %%a in ('type "!z_file_tmp!"') do ( if "%%a %%b"=="ProductName REG_SZ" ( set z_productname=%%c %%d %%e %%f ) ) if exist "!z_file_tmp!" del "!z_file_tmp!" if not "!z_productname!"=="Symantec NetBackup OpsCenter Server" ( call :log "Found `ProductName` of `!z_productname!`, an instance of `Symantec NetBackup OpsCenter Server` does not appear to be installed on this server, script aborting..." if not "%~6"=="" echo !z_abort! >>"%~6" exit /b !z_abort! ) REM ************************************************************************************************ REM *** Check to see whether the builtin `dbbackup` script exists... if "!z_installdir:~-1!"=="\" ( set z_dbbackup=!z_installdir!bin\dbbackup.bat ) else ( set z_dbbackup=!z_installdir!\bin\dbbackup.bat ) if not exist "!z_dbbackup!" ( call :log "Cannot find database backup script `!z_dbbackup!`, script aborting..." if not "%~6"=="" echo !z_abort! >>"%~6" exit /b !z_abort! ) REM ************************************************************************************************ REM *** Check whether database backup target folder exists... if not exist "!z_target!" ( call :log "OpsCenter database backup target folder `!z_target!` does not exist, script aborting..." if not "%~6"=="" echo !z_abort! >>"%~6" exit /b !z_abort! ) REM ************************************************************************************************ REM *** Finally we should be able to do something now... call :log "" call :log "" call :log "***************************************" call :log "Script started..." call :log "" call :log "Computername: !computername!" call :log "Username: !username!" call :log "Domain: !userdomain!" call :log "" call :log "Parameters received:" call :log " P1 - Client Name: `%~1`" call :log " P2 - Policy Name: `%~2`" call :log " P3 - Schedule_Name: `%~3`" call :log " P4 - Schedule Type: `%~4`" call :log " P5 - Status: `%~5`" call :log " P6 - Result File: `%~6`" call :log "...done..." call :log "" call :log "NetBackup stream/job environment variables:" call :log " Stream Number: `!stream_number!`" call :log " Stream Count: `!stream_count!`" call :log " Stream PID: `!stream_pid!` call :log "...done..." call :log "" call :log "Script determined values:" call :log " InstallDir: `!z_installdir!`" call :log " ProductName: `!z_productname!`" call :log " dbbackup script: `!z_dbbackup!`" call :log " Backup Target: `!z_target!`" call :log "....done..." call :log "" call :log "Checking parameter P4 `Schedule Type` is not empty..." if not "%~4"=="" ( call :log "...parameter P4 is not empty, continuing..." ) else ( call :log "...parameter P4 is empty, cannot continue, aborting with status `1`..." echo 1 >>"%~6" exit /b 2 ) call :log "" call :log "Calling OpsCenter `dbbackup` script..." call "!z_dbbackup!" "!z_target!" >>"!z_file_log!" 2>&1 set z_sts=!errorlevel! if "!z_sts!"=="0" ( call :log "...call to dbbackup success, status `!z_sts!`..." ) else ( call :log "...call to dbbackup failed, status `!z_sts!`..." echo !z_sts! >>"%~6" exit /b !z_sts! ) call :log "" call :log "Script completed ok, writing `0` to results file..." echo 0 >>"%~6" exit /b 0 :log (echo !date! !time:~0,8! %~1)>>"!z_file_log!" goto :eof