Forum Discussion

ElenaC's avatar
ElenaC
Level 3
10 years ago

Configuring or getting dbclient log name

Hi,  I'm trying to automate  SqlServer Backups by executing from control-m, which is a 3rd party schedule software.  To do this, I have created a .bat file that call the Netbackup exec file (db...
  • sdo's avatar
    10 years ago

    Ok - I've just had a go at the script below.

    I will attempt to take in to consideration your most recent post above, later today... to see if the script can access the logs that you have indicated - but it's not easy for me - as I don't have an SQL client to test with.

    Anyway, try the below for now... just curious to see if it works for you - and I will make it use the logs you have highlighted.

    @echo off
    setlocal enabledelayedexpansion
    
    set z_path=%~dp0
    set z_name=%~n0
    
    set z_file_lis=!z_path!!z_name!.lis
    set z_file_log=!z_path!!z_name!.log
    set z_file_tmp=!z_path!!z_name!.tmp
    
    if exist "!z_file_lis!"  del "!z_file_lis!"
    if exist "!z_file_tmp!"  del "!z_file_tmp!"
    
    set z_final_sts=999
    
    call :log "**************************************************"
    call :log "Script started..."
    
    
    REM *********************************************************************************
    REM *********************************************************************************
    
    call :log ""
    call :log "Determining NetBackup installation path..."
    
    reg query "HKLM\Software\Veritas\NetBackup\CurrentVersion" /v "InstallDir" >"!z_file_tmp!" 2>&1
    set z_sts=!errorlevel!
    if not !z_sts!==0 (
      call :log "...failed to determine install directory for NetBackup, status `!z_sts!`, script aborting..."
      goto :end
    )
    for /f "tokens=1,2,* skip=1" %%a in ('type "!z_file_tmp!"') do (set z_installdir=%%c)
    
    call :log "...NetBackup installation path is:  !z_installdir!"
    call :log "...done..."
    
    
    REM *********************************************************************************
    REM *********************************************************************************
    
    call :log ""
    call :log "Checking for `dbbackex` CLI tool..."
    
    set z_dbbackex=!z_installdir!NetBackup\bin\dbbackex.exe
    
    if not exist "!z_dbbackex!" (
      call :log "...unable to locate `!z_dbbackex!` command, script aborting..."
      goto :end
    )
    
    call :log "...found dbbackex at `!z_dbbackex!`..."
    call :log "...done..."
    
    call :log ""
    call :log "Locating SQL `batch` file..."
    
    set z_file_bch=!z_path!!z_name!-!computername!.bch
    
    if not exist "!z_file_bch!" (
      call :log "...unable to locate `!z_file_bch!`, script aborting..."
      goto :end
    )
    
    call :log "...found `!z_file_bch!`..."
    call :log "...done..."
    
    
    REM *********************************************************************************
    REM *********************************************************************************
    
    call :log ""
    call :log "Calling dbbackex to start MS SQL Server backup..."
    
    "!z_dbbackex!" -f "!z_file_bch!" -p TEST001 -s back -np >>"!z_file_log!" 2>&1
    set z_sts=!errorlevel!
    set z_final_sts=!z_sts!
    
    if !z_sts!==0 (
      call :log "...call to dbbackex completed SUCCESSFULLY..."
    ) else (
      call :log "...call to dbbackex FAILED, status `!z_sts!`..."
    )
    
    call :log "...done..."
    
    
    REM *********************************************************************************
    REM *********************************************************************************
    
    call :log ""
    call :log "Locating most recent dbclient log..."
    
    set z_dbclient_path=!z_installdir!NetBackup\logs\dbclient\
    
    if not exist "!z_dbclient_path!" (
      call :log "...unable to locate `!z_dbclient_path!`, script aborting..."
      goto :end
    ) else (
      dir /o:d /t:w /b "!z_dbclient_path!" >"!z_file_tmp!" 2>&1
      set z_sts=!errorlevel!
      if not !z_sts!==0 (
        call :log "...unable to list dbclient log files, status `!z_sts!`, script aborting..."
        goto :end
      )
    )
    
    for /f "tokens=*" %%a in ('type "!z_file_tmp!"') do (set z_dbclient_file=%%a)
    
    set z_dbclient_spec=!z_dbclient_path!!z_dbclient_file!
    
    if not exist "!z_dbclient_spec!" (
      call :log "...unexpected unable to locate `!z_dbclient_spec!`, script aborting..."
      goto :end
    )
    
    call :log "...most recent dbclient log is:  !z_dbclient_spec!"
    call :log "...done..."
    
    
    REM *********************************************************************************
    REM *********************************************************************************
    
    call :log ""
    call :log "Acquiring trailing PID pair from dbclient log..."
    
    set z_pids=
    
    for /f "tokens=1,2" %%a in ('type "!z_dbclient_spec!"') do (set z_pids=%%b)
    
    if "!z_pids!"=="" (
      call :log "...failed to acquire a trailing PID pair from dbclient log, script aborting..."
      goto :end
    )
    
    call :log "...found trailing pid pair of:  !z_pids!"
    call :log "...done..."
    
    
    REM *********************************************************************************
    REM *********************************************************************************
    
    call :log ""
    call :log "Extracting trailing log section from dbclient log..."
    call :log "WARNING:"
    call :log "WARNING: the following log text, may, or MAY NOT, be relevant to our call to dbbackex..."
    call :log "WARNING:"
    
    find /i "!z_pids!" "!z_dbclient_spec!" >"!z_file_lis!" 2>&1
    set z_sts=!errorlevel!
    if !z_sts!==0 (
      call :log "-----------------------------------------------"
      type "!z_file_lis!"
      type "!z_file_lis!" >>"!z_file_log!"
      call :log "-----------------------------------------------"
    ) else (
      call :log "...failed to find PID pair in dbclient log, status `!z_sts!`..."
    )
    call :log "...done..."
    
    
    REM *********************************************************************************
    REM *********************************************************************************
    REM *** If we get here then we haven't aborted, so cleanup...
    
    if exist "!z_file_lis!"  del "!z_file_lis!"
    if exist "!z_file_tmp!"  del "!z_file_tmp!"
    
    
    REM *********************************************************************************
    REM *********************************************************************************
    REM *** If/when the script aborts, it can jump here...
    
    :end
    call :log ""
    call :log "Script exiting with status `!z_final_sts!`..."
    echo+
    exit /b !z_final_sts!
    
    
    REM *********************************************************************************
    REM *********************************************************************************
    
    :log
    (echo !date! !time:~0,8!  %~1)
    (echo !date! !time:~0,8!  %~1)>>"!z_file_log!"
    goto :eof