@echo off setlocal enabledelayedexpansion REM ****************************************************************************************************** REM * File: whats-at-top-of-full-images.bat REM * Desc: For a client, for past n days, for a policy type, list first entry of the backup job. REM * REM * Vers Date Who Description REM * ---- ---- --- ----------- REM * v0.01 12-FEB-2015 sdo Initial version. REM ****************************************************************************************************** REM * !!! WARNING and DISCLAIMER !!! REM * !!! WARNING and DISCLAIMER !!! REM * !!! WARNING and DISCLAIMER !!! REM * REM * Before using this script, be aware of the following points: REM * - Use of this script is entirely at the end user's own risk. REM * - Neither the author of this script, nor Veritas, will accept any responsbility for issues or REM * problems or data loss caused by use, or mis-use, of this script, in either its original form or REM * a modified form. REM * - This script is not endorsed by Veritas. REM * - This script is not supported by Veritas. REM * - This script has not been tested by Veritas. REM * - This script may not be suitable for use in any given NetBackup environment. REM * - This script is furnished on an example basis only. REM * - Whilst every effort has been made, by the author of this script, to produce something that is REM * useful and viable, bugs and errors may exist which may cause data loss. 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) This script was developed and tested using: REM * Windows 2008 R2 SP1 with NetBackup Server v7.7.1 ik UK locale REM * Windows 2008 R2 SP1 with NetBackup Server v7.7.2 in UK locale REM * Windows 2012 R2 with NetBackup Server v7.7.1 in US locale REM * ...but should work with any Windows version since 2003 and any v7.* of NetBackup Server. REM * 2) You will be prompted with three questions before the report runs. REM * 3) This script assumes that you already have system wide PATH to include the NetBackup binaries. REM * 4) This script starts off by using depth 1, to keep the bplist files small and quick, but if the REM * script is unable to determine a list, then it will increment the depth and remember it for the REM * next run of the script. So, if you see some 'unable to determine' lines, then just re-run the REM * script a couple of times. REM ****************************************************************************************************** set z_path=%~dp0 set z_name=%~n0 REM ****************************************************************************************************** REM ****************************************************************************************************** REM *** Define the names of some local working text files... set z_file_dat=!z_path!!z_name!.!computername!.dat.txt set z_file_ddd=!z_path!!z_name!.!computername!.ddd.txt set z_file_ima=!z_path!!z_name!.!computername!.ima.txt set z_file_inp=!z_path!!z_name!.!computername!.inp.txt set z_file_log=!z_path!!z_name!.!computername!.log.txt set z_file_tmp=!z_path!!z_name!.!computername!.tmp.txt if exist "!z_file_ima!" del "!z_file_ima!" if exist "!z_file_log!" del "!z_file_log!" if exist "!z_file_tmp!" del "!z_file_tmp!" call :log "" call :log "" REM ****************************************************************************************************** REM ****************************************************************************************************** REM *** Determine date format produced in the bpimagelist -idonly output... set /a z_test_days=18 set /a z_test_hours=!z_test_days!*24 call :log "...please wait a few moments..." if not exist "!z_file_dat!" ( call :log "...performing a one-time image idonly collection, for previous month, so that date format can be determined..." bpimagelist -idonly -hoursago !z_test_hours! >"!z_file_dat!" 2>&1 set z_sts=!errorlevel! if not !z_sts!==0 ( call :log "...failed to list any images at all, status `!z_sts!`, script aborting..." goto :end ) ) call :log "...processing list..." set z_ima_format_uk=no set z_ima_format_us=no set z_ima_ampm=no for /f "tokens=1,2,3,4,5,6" %%a in ('type "!z_file_dat!"') do ( set z_image_date=%%b for /f "tokens=1,2,3 delims=/" %%a in ("!z_image_date!") do ( set z_part1=%%a set z_part2=%%b set z_part3=%%c ) if !z_part1! gtr 12 set z_ima_format_uk=yes if !z_part2! gtr 12 set z_ima_format_us=yes if !z_part3! lss 1970 ( call :log "...unexpected year, script aborting..." goto :end ) if /i "%%d"=="am" set z_ima_ampm=yes if /i "%%d"=="pm" set z_ima_ampm=yes ) call :log "...done..." if /i "!z_ima_format_uk!"=="no" ( if /i "!z_ima_format_us!"=="no" ( if /i "!z_ima_ampm!"=="yes" ( call :log "...could not detect bpimagelist idonly date format, but based on presence of AM/PM in time, assuming to be US date format..." set z_ima_format_us=yes ) else ( call :log "...unable to determine bpimagelist output date format, script aborting..." goto :end ) ) ) if /i "!z_ima_format_uk!"=="yes" ( if /i "!z_ima_format_us!"=="yes" ( call :log "...unexpected both UK and US date formats found in bpimagelist output, script aborting..." goto :end ) ) if /i "!z_ima_format_uk!"=="yes" call :log "...the bpimagelist output has UK date format..." if /i "!z_ima_format_us!"=="yes" call :log "...the bpimagelist output has US date format..." REM ****************************************************************************************************** REM ****************************************************************************************************** REM *** Determine date format for bpclist CLI swicthes/arguments... set z_bplist_format= bplist -h >"!z_file_tmp!" 2>&1 find /i "dd" "!z_file_tmp!" >"!z_file_ddd!" for /f "tokens=1,2,3,4,5,6,7,8,9" %%a in ('type "!z_file_ddd!"') do ( if "%%a"=="[-s" set z_bplist_format=%%b if "%%b"=="[-s" set z_bplist_format=%%c if "%%c"=="[-s" set z_bplist_format=%%d if "%%d"=="[-s" set z_bplist_format=%%e if "%%e"=="[-s" set z_bplist_format=%%f if "%%f"=="[-s" set z_bplist_format=%%g if "%%g"=="[-s" set z_bplist_format=%%h if "%%h"=="[-s" set z_bplist_format=%%i if "%%i"=="[-s" set z_bplist_format=%%j if "%%j"=="[-s" set z_bplist_format=%%k ) if "!z_bplist_format!"=="" ( call :log "...unable to determine bplist input date format, script aborting..." goto :end ) set z_bplist_format_uk=no set z_bplist_format_us=no if /i "!z_bplist_format!"=="dd/mm/yyyy" set z_bplist_format_uk=yes if /i "!z_bplist_format!"=="mm/dd/yyyy" set z_bplist_format_us=yes if /i "!z_bplist_format_uk!"=="no" ( if /i "!z_bplist_format_us!"=="no" ( call :log "...unable to determine bplist input date format, script aborting..." goto :end ) ) if /i "!z_bplist_format_uk!"=="yes" call :log "...the bplist input has UK date format..." if /i "!z_bplist_format_us!"=="yes" call :log "...the bplist input has US date format..." REM ****************************************************************************************************** REM ****************************************************************************************************** REM *** Default answer to questions from those saved from previous run... set z_client_def=!computername! call :r_lower z_client_def set z_daysago_def=20 set z_ptype_def=Windows set z_depth_def=1 if exist "!z_file_inp!" ( for /f "tokens=1,2 delims=:" %%a in ('type "!z_file_inp!"') do ( if /i "%%a"=="client" set z_client_def=%%b if /i "%%a"=="daysago" set z_daysago_def=%%b if /i "%%a"=="depth" set z_depth_def=%%b if /i "%%a"=="ptype" set z_ptype_def=%%b ) ) if "!z_depth_def!" gtr "8" ( call :log "...auto incrementing depth has exceeded level 8, seek advice from script developer, script aborting..." goto :end ) REM ****************************************************************************************************** REM ****************************************************************************************************** REM *** Now ask user three questions... call :log "" call :log "" :ask_client set /p z_client=_Enter a client name (name,EXIT) [!z_client_def!] : if "!z_client!"=="" set z_client=!z_client_def! if /i "!z_client!"=="exit" goto :end :ask_daysago set /p z_daysago=_Enter days ago to look back (nnn,EXIT) [!z_daysago_def!] : if "!z_daysago!"=="" set z_daysago=!z_daysago_def! if /i "!z_daysago!"=="exit" goto :end if "!z_daysago!" lss "0" goto :ask_daysago if "!z_daysago!" gtr "999" goto :ask_daysago if !z_daysago! lss 1 goto :ask_daysago if !z_daysago! gtr 416 goto :ask_daysago :ask_ptype set /p z_ptype=_Enter policy type (NDMP,Standard,Windows,EXIT) [!z_ptype_def!] : if "!z_ptype!"=="" set z_ptype=!z_ptype_def! if /i "!z_ptype!"=="exit" goto :end if /i not "!z_ptype!"=="ndmp" ( if /i not "!z_ptype!"=="standard" ( if /i not "!z_ptype!"=="windows" ( goto :ask_ptype ) ) ) :ask_depth set z_depth=!z_depth_def! REM ****************************************************************************************************** REM ****************************************************************************************************** REM *** Remember the user's inputs... if exist "!z_file_inp!" del "!z_file_inp!" (echo client:!z_client!) >>"!z_file_inp!" (echo daysago:!z_daysago!) >>"!z_file_inp!" (echo depth:!z_depth!) >>"!z_file_inp!" (echo ptype:!z_ptype!) >>"!z_file_inp!" REM ****************************************************************************************************** REM ****************************************************************************************************** REM *** Now work out the real strings for policy type name, and also the policy type number... if /i "!z_ptype!"=="ndmp" set z_policy_type_name=NDMP if /i "!z_ptype!"=="standard" set z_policy_type_name=Standard if /i "!z_ptype!"=="windows" set z_policy_type_name=MS-Windows set z_policy_type_number= if /i "!z_policy_type_name!"=="Standard" set z_policy_type_number=0 if /i "!z_policy_type_name!"=="MS-Windows" set z_policy_type_number=13 if /i "!z_policy_type_name!"=="NDMP" set z_policy_type_number=19 if "!z_policy_type_number!"=="" ( call :log "...unable to determine policy type number from `!z_policy_type_name!`, script aborting..." goto :end ) REM ****************************************************************************************************** REM ****************************************************************************************************** REM *** Which schedule type are we interested in... set z_schedule_type_name=FULL REM set z_schedule_type_name=INCR REM set z_schedule_type_name=CINC REM ****************************************************************************************************** REM ****************************************************************************************************** REM *** Now run the report... set /a z_hoursago=!z_daysago!*24 set z_command=bpimagelist -idonly -hoursago !z_hoursago! -client !z_client! -pt !z_policy_type_name! -st !z_schedule_type_name! call :log "" call :log "" call :log "Images: !z_command!" call :log "" call :log "" call :log "Date Time Image Name Top Of Image" call :log "------------------- ---------- ------------" !z_command! >"!z_file_ima!" 2>&1 set z_sts=!errorlevel! if not !z_sts!==0 ( call :log "...failed to list images for client, status `!z_sts!`, script aborting..." goto :end ) for /f "tokens=1,2,3,4,5,6" %%a in ('type "!z_file_ima!"') do ( if "%%d"=="ID:" ( set z_image_date=%%b set z_image_time=%%c set z_image_ampm= set z_image_name=%%e ) else ( set z_image_date=%%b set z_image_time=%%c set z_image_ampm=%%d set z_image_name=%%f ) set z_image_top= call :r_bplist_date set z_command=bplist -s !z_bplist_date! !z_bplist_time! -e !z_bplist_date! !z_bplist_time! -C !z_client! -t !z_policy_type_number! -PI -R !z_depth! -nt_files * !z_command! >"!z_file_tmp!" 2>&1 set z_sts=!errorlevel! if !z_sts!==0 ( call :r_get_top ) else ( call :log "...failed, status `!z_sts!`, command: !z_command!" set z_image_top=unable to determine if !z_sts!==227 ( set /a z_depth=!z_depth!+1 (echo depth:!z_depth!)>>"!z_file_inp!" ) ) set z_image_name_disp=!z_image_name! *** set z_image_name_disp=!z_image_name_disp:~0,45! call :log "!z_bplist_date! !z_bplist_time! !z_image_name_disp! !z_image_top!" ) REM ****************************************************************************************************** REM ****************************************************************************************************** call :log "" call :log "" call :log "Script completed..." :end echo+ pause exit /b REM ****************************************************************************************************** REM ****************************************************************************************************** :r_bplist_date if /i "!z_ima_format_uk!"=="yes" ( for /f "tokens=1,2,3 delims=/" %%a in ("!z_image_date!") do ( set z_date_dd=%%a set z_date_mm=%%b set z_date_yyyy=%%c ) for /f "tokens=1,2,3 delims=:" %%a in ("!z_image_time!") do ( set z_time_hh=%%a set z_time_mm=%%b set z_time_ss=%%c ) ) else ( for /f "tokens=1,2,3 delims=/" %%a in ("!z_image_date!") do ( set z_date_mm=%%a set z_date_dd=%%b set z_date_yyyy=%%c ) for /f "tokens=1,2,3 delims=:" %%a in ("!z_image_time!") do ( set z_time_hh=%%a set z_time_mm=%%b set z_time_ss=%%c ) if /i "!z_image_ampm!"=="am" ( if !z_time_hh! equ 12 set z_time_hh=0 ) else ( if not !z_time_hh! equ 12 set /a z_time_hh=!z_time_hh!+12 ) ) set z_date_dd=0!z_date_dd! set z_date_mm=0!z_date_mm! set z_date_dd=!z_date_dd:~-2! set z_date_mm=!z_date_mm:~-2! set z_time_hh=0!z_time_hh! set z_time_mm=0!z_time_mm! set z_time_ss=0!z_time_ss! set z_time_hh=!z_time_hh:~-2! set z_time_mm=!z_time_mm:~-2! set z_time_ss=!z_time_ss:~-2! if /i "!z_bplist_format_uk!"=="yes" ( set z_bplist_date=!z_date_dd!/!z_date_mm!/!z_date_yyyy! set z_bplist_time=!z_time_hh!:!z_time_mm!:!z_time_ss! ) else ( set z_bplist_date=!z_date_mm!/!z_date_dd!/!z_date_yyyy! set z_bplist_time=!z_time_hh!:!z_time_mm!:!z_time_ss! ) goto :eof REM ****************************************************************************************************** REM ****************************************************************************************************** :r_get_top for /f "tokens=*" %%a in ('type "!z_file_tmp!"') do ( if "!z_image_top!"=="" ( set z_image_top=%%a goto :eof ) ) goto :eof REM ****************************************************************************************************** REM ****************************************************************************************************** :r_lower for %%a in ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z" ) do ( call set "%~1=%%%~1:%%~a%%" ) goto :eof REM ****************************************************************************************************** REM ****************************************************************************************************** :log if "%~1"=="" ( (echo+) (echo+)>>"!z_file_log!" ) else ( (echo %~1) (echo %~1)>>"!z_file_log!" ) goto :eof