I couldn't resist having a go at a Windows DOS batch script...
...it's a bit rough and ready.
I've tried to keep it short, but with enough built-in checking, so that one should be able to work out why it breaks, if/when it breaks.
There's no guarantee that it will work in your environment.
But it should be pretty harmless to run... all it does is use query/list/report type commands - and I've tried to keep the number of calls to NetBackup commands to a minimum, so it should run fairly quickly.
@echo off
setlocal enabledelayedexpansion
set z_path=%~dp0
set z_name=%~n0
set z_file_ima=!z_path!!z_name!.ima
set z_file_lis=!z_path!!z_name!.lis
set z_file_log=!z_path!!z_name!.log
set z_file_med=!z_path!!z_name!.med
set z_file_tmp=!z_path!!z_name!.tmp
set z_file_vol=!z_path!!z_name!.vol
if exist "!z_file_ima!" del "!z_file_ima!"
if exist "!z_file_lis!" del "!z_file_lis!"
if exist "!z_file_log!" del "!z_file_log!"
if exist "!z_file_med!" del "!z_file_med!"
if exist "!z_file_tmp!" del "!z_file_tmp!"
if exist "!z_file_vol!" del "!z_file_vol!"
set z_debug=NO
reg query HKLM\Software\Veritas\NetBackup\CurrentVersion\Config /v "client_name" >"!z_file_tmp!" 2>&1
set z_sts=!errorlevel!
if not !z_sts!==0 (
call :log "...NetBackup does not appear to be installed on this server, script aborting..."
goto :end
)
for /f "tokens=1,2,3 skip=1" %%a in ('type "!z_file_tmp!"') do (
set z_client_def=%%c
)
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 "...cannot locate `InstallDir` registry value, script aborting..."
goto :end
)
for /f "tokens=1,2,* skip=1" %%a in ('type "!z_file_tmp!"') do (
set z_install_dir=%%c
)
reg query HKLM\Software\Veritas\NetBackup\CurrentVersion /v "Install Type" >"!z_file_tmp!" 2>&1
set z_sts=!errorlevel!
if not !z_sts!==0 (
call :log "...cannot locate `Install Type` registry value, script aborting..."
goto :end
)
for /f "tokens=1,2,3,* skip=1" %%a in ('type "!z_file_tmp!"') do (
set z_install_type=%%d
)
if /i not "!z_install_type!"=="NetBackup Master Server" (
if /i not "!z_install_type!"=="NetBackup Media Server" (
if /i not "!z_install_type!"=="NetBackup Administration Console" (
call :log "...this script cannot be run on NetBackup installation of `!z_install_type!`, script aborting..."
goto :end
)
)
)
set z_netbackup_bin=!z_install_dir!NetBackup\bin
if not exist "!z_netbackup_bin!" (
call :log "...unable to locate NetBackup binaries folder `!z_netbackup_bin!`, script aborting..."
goto :end
)
set z_volmgr_bin=!z_install_dir!Volmgr\bin
if not exist "!z_volmgr_bin!" (
call :log "...unable to locate Volume Manager binaries folder `!z_volmgr_bin!`, script aborting..."
goto :end
)
set z_bplist=!z_netbackup_bin!\bplist.exe
set z_bpimagelist=!z_netbackup_bin!\admincmd\bpimagelist.exe
set z_bpverify=!z_netbackup_bin!\admincmd\bpverify.exe
set z_vmquery=!z_volmgr_bin!\vmquery.exe
if not exist "!z_vmquery!" (
call :log "...unable to find `!z_vmquery!`, script aborting..."
goto :end
)
:get_client
call :log ""
set z_client=
set /p z_client=!date! !time:~0,8! _Enter a client name [!z_client_def!] :
if /i "!z_client!"=="" set z_client=!z_client_def!
if /i "!z_client!"=="EXIT" goto :end
"!z_bpimagelist!" -idonly -client !z_client! -d 01/01/1970 00:00:00 >"!z_file_lis!" 2>&1
set z_sts=!errorlevel!
if !z_sts!==227 (
call :log "...there are no backups for a client named `!z_client!`, try another client name..."
goto :get_client
) else (
if not !z_sts!==0 (
call :log "...call to bpimagelist failed, status `!z_sts!`, script aborting..."
goto :end
)
)
set /a z_datetimes=0
set z_oldest_datetime=
set z_newest_datetime=
for /f "tokens=1,2,3" %%a in ('type "!z_file_lis!"') do (
set /a z_datetimes+=1
if "!z_newest_datetime!"=="" set z_newest_datetime=%%b %%c
set z_oldest_datetime=%%b %%c
set z_datetimes_[!z_datetimes!]=%%b %%c
)
call :log "...found `!z_datetimes!` backups..."
call :log "...oldest backup: !z_oldest_datetime!"
call :log "...newest backup: !z_newest_datetime!"
set z_find_def=!z_install_dir!NetBackup\version.txt
:get_find
set z_find=
call :log ""
set /p z_find=!date! !time:~0,8! _Enter file name to find [!z_find_def!] :
if /i "!z_find!"=="" set z_find=!z_find_def!
if /i "!z_find!"=="exit" goto :end
call :log ""
call :log "...searching for file in date range..."
"!z_bplist!" -B -l -b -s !z_oldest_datetime! -e !z_newest_datetime! -C !z_client! -I "!z_find!" >"!z_file_lis!" 2>&1
set z_sts=!errorlevel!
if !z_sts!==227 (
call :log "...unable to find file `!z_find!` in any backup, try again..."
goto :get_find
) else (
if not !z_sts!==0 (
call :log "...call to bplist failed, status `!z_sts!`, script aborting..."
goto :end
)
)
set /a z_count=0
for /f "tokens=1,2,3,4,5,6,7,*" %%a in ('type "!z_file_lis!"') do (set /a z_count+=1)
call :log "...file was found in `!z_count!` backups..."
Call :log ""
call :log "...now identifying which backups contain the file..."
set /a z_backups=0
for /l %%a in (1,1,!z_datetimes!) do (
set z_datetime=!z_datetimes_[%%a]!
call :log "...checking backup `%%a` from `!z_datetime!`..."
"!z_bplist!" -B -l -b -s !z_datetime! -e !z_datetime! -C !z_client! -I "!z_find!" >"!z_file_tmp!" 2>&1
set z_sts=!errorlevel!
if !z_sts!==0 (
set /a z_backups+=1
set z_backups_[!z_backups!]=!z_datetime!
) else (
if not !z_sts!==227 (
call :log "...failed when checking datetime `%%a` of `!z_datetime!`, status `!z_sts!`, script continuing..."
)
)
)
call :log "...file was found in `!z_backups!` backups..."
call :log "...file was found in the following backups..."
set /a z_images=0
for /l %%a in (1,1,!z_backups!) do (
set z_backup=!z_backups_[%%a]!
if /i "!z_debug!"=="yes" call :log "...backup `%%a` from `!z_backup!`..."
"!z_bpimagelist!" -idonly -client !z_client! -d !z_backup! -e !z_backup! >"!z_file_lis!" 2>&1
set z_sts=!errorlevel!
if not !z_sts!==0 (
call :log "...unexpected unable to list backup for date `!z_backup!`, status `!z_sts!`, script aborting..."
goto :end
)
set z_image=
for /f "tokens=1,2,3,4,5" %%a in ('type "!z_file_lis!"') do (
set z_image=%%e
)
if "!z_image!"=="" (
call :log "...unexpected no image ID for backup `!z_backup!`, script aborting..."
goto :end
)
if /i "!z_debug!"=="yes" call :log "...listing image `!z_image!`..."
"!z_bpimagelist!" -backupid !z_image! >"!z_file_ima!" 2>&1
set z_sts=!errorlevel!
if not !z_sts!==0 (
call :log "...unexpected unable to list image `!z_image!`, status `!z_sts!`, script aborting..."
goto :end
)
call :r_image
)
:end
call :log ""
call :log "Script exiting..."
echo+
pause
exit /b
:r_image
set z_copies=
set z_copy_prev=
for /f "tokens=1,2,3,4,5,6,7,8,9" %%a in ('type "!z_file_ima!"') do (
if "%%a"=="FRAG" (
set z_copy_curr=%%b
if not "!z_copy_curr!"=="!z_copy_prev!" (
if /i "!z_debug!"=="yes" call :log "...new copy number `!z_copy_curr!`..."
set z_copies=!z_copies! !z_copy_curr!
)
set z_copy_prev=!z_copy_curr!
)
)
set z_copies=!z_copies:~1,999!
if /i "!z_debug!"=="yes" call :log "...found copy numbers `!z_copies!`..."
call :log ""
call :log "***************************************************************"
call :log "***************************************************************"
call :log "Client: !z_client!"
call :log "File: !z_find!"
call :log "Backup: !z_backup!"
call :log "Image: !z_image!"
for %%a in (!z_copies!) do (
set z_copy=%%a
call :log "Copy !z_copy!"
"!z_bpverify!" -pb -backupid !z_image! -cn !z_copy! >"!z_file_med!" 2>&1
set z_sts=!errorlevel!
if not !z_sts!==0 (
call :log "...call to bpverify failed for image `!z_image!`, backup `!z_backup!`, status `!z_sts!`, subroutine exiting..."
goto :eof
)
if "!z_debug!"=="yes" type "!z_file_med!"
set /a z_count_tape=0
for /f "tokens=1,2,3,4" %%a in ('type "!z_file_med!"') do (
set z_media_type=unknown
if /i "%%a"=="Path" (
set z_media_type=disk
)
if /i "%%a"=="media" (
set z_media=%%d
set z_media_type=tape
if "!z_media:~0,1!"=="@" set z_media_type=MSDP
)
if /i not "!z_media_type!"=="tape" (
call :log " Media Type: !z_media_type!"
) else (
"!z_vmquery!" -m !z_media! -b >"!z_file_vol!" 2>&1
set z_sts=!errorlevel!
if not !z_sts!==0 (
call :log "...call to vmquery for media `!z_media!` failed, status `!z_sts!`, subroutine exiting..."
goto :eof
)
set /a z_count_tape+=1
if !z_count_tape!==1 (
for /f "tokens=*" %%a in ('type "!z_file_vol!"') do (call :log " %%a")
) else (
for /f "tokens=* skip=3" %%a in ('type "!z_file_vol!"') do (call :log " %%a")
)
)
)
)
goto :eof
:log
(echo !date! !time:~0,8! %~1)
(echo !date! !time:~0,8! %~1)>>"!z_file_log!"
goto :eof
.
...and an example run:
D:\Scripts\find-media-for-restore> find-media-for-restore.bat
12/04/2015 18:41:50
12/04/2015 18:41:50 _Enter a client name [myserver] :
12/04/2015 18:41:53 ...found `27` backups...
12/04/2015 18:41:53 ...oldest backup: 15/06/2014 11:09:29
12/04/2015 18:41:53 ...newest backup: 12/04/2015 18:04:41
12/04/2015 18:41:53
12/04/2015 18:41:53 _Enter file name to find [C:\Program Files\Veritas\NetBackup\version.txt] : D:\NetBackup
12/04/2015 18:41:54
12/04/2015 18:41:54 ...searching for file in date range...
12/04/2015 18:41:55 ...file was found in `2` backups...
12/04/2015 18:41:55
12/04/2015 18:41:55 ...now identifying which backups contain the file...
12/04/2015 18:41:55 ...checking backup `1` from `12/04/2015 18:04:41`...
12/04/2015 18:41:55 ...checking backup `2` from `12/04/2015 16:53:48`...
12/04/2015 18:41:55 ...checking backup `3` from `28/02/2015 21:29:24`...
12/04/2015 18:41:55 ...checking backup `4` from `28/02/2015 21:29:07`...
12/04/2015 18:41:55 ...checking backup `5` from `27/02/2015 03:31:50`...
12/04/2015 18:41:55 ...checking backup `6` from `27/02/2015 03:31:34`...
12/04/2015 18:41:55 ...checking backup `7` from `30/10/2014 18:00:01`...
12/04/2015 18:41:55 ...checking backup `8` from `26/10/2014 14:40:44`...
12/04/2015 18:41:55 ...checking backup `9` from `21/10/2014 18:00:02`...
12/04/2015 18:41:56 ...checking backup `10` from `16/10/2014 18:00:01`...
12/04/2015 18:41:56 ...checking backup `11` from `09/10/2014 18:00:01`...
12/04/2015 18:41:56 ...checking backup `12` from `08/10/2014 17:33:54`...
12/04/2015 18:41:56 ...checking backup `13` from `08/10/2014 17:33:38`...
12/04/2015 18:41:56 ...checking backup `14` from `02/10/2014 18:00:01`...
12/04/2015 18:41:56 ...checking backup `15` from `25/09/2014 18:00:01`...
12/04/2015 18:41:56 ...checking backup `16` from `18/09/2014 18:00:01`...
12/04/2015 18:41:56 ...checking backup `17` from `11/09/2014 18:00:01`...
12/04/2015 18:41:56 ...checking backup `18` from `19/08/2014 18:55:28`...
12/04/2015 18:41:57 ...checking backup `19` from `19/08/2014 18:55:11`...
12/04/2015 18:41:57 ...checking backup `20` from `07/08/2014 23:40:49`...
12/04/2015 18:41:57 ...checking backup `21` from `07/08/2014 23:40:32`...
12/04/2015 18:41:57 ...checking backup `22` from `07/08/2014 10:16:50`...
12/04/2015 18:41:57 ...checking backup `23` from `07/08/2014 09:45:17`...
12/04/2015 18:41:57 ...checking backup `24` from `06/08/2014 07:44:39`...
12/04/2015 18:41:57 ...checking backup `25` from `05/08/2014 11:53:02`...
12/04/2015 18:41:57 ...checking backup `26` from `15/06/2014 11:09:37`...
12/04/2015 18:41:57 ...checking backup `27` from `15/06/2014 11:09:29`...
12/04/2015 18:41:57 ...file was found in `2` backups...
12/04/2015 18:41:57 ...file was found in the following backups...
12/04/2015 18:41:58
12/04/2015 18:41:58 ***************************************************************
12/04/2015 18:41:58 ***************************************************************
12/04/2015 18:41:58 Client: myserver
12/04/2015 18:41:58 File: D:\NetBackup
12/04/2015 18:41:58 Backup: 07/08/2014 10:16:50
12/04/2015 18:41:58 Image: myserver_1407403010
12/04/2015 18:41:58 Copy 1
12/04/2015 18:41:58 media media robot robot robot side/ optical # mounts/ last
12/04/2015 18:41:58 ID type type # slot face partner cleanings mount time
12/04/2015 18:41:58 -------------------------------------------------------------------------------
12/04/2015 18:41:58 A00002 HCART2 NONE - - - - 9 07/08/2014 10:16
12/04/2015 18:41:58 A00003 HCART2 NONE - - - - 11 07/08/2014 18:25
12/04/2015 18:41:58
12/04/2015 18:41:58 ***************************************************************
12/04/2015 18:41:58 ***************************************************************
12/04/2015 18:41:58 Client: myserver
12/04/2015 18:41:58 File: D:\NetBackup
12/04/2015 18:41:58 Backup: 15/06/2014 11:09:29
12/04/2015 18:41:58 Image: myserver_1402826969
12/04/2015 18:41:58 Copy 1
12/04/2015 18:41:58 media media robot robot robot side/ optical # mounts/ last
12/04/2015 18:41:58 ID type type # slot face partner cleanings mount time
12/04/2015 18:41:58 -------------------------------------------------------------------------------
12/04/2015 18:41:58 A00015 HCART2 NONE - - - - 3 15/06/2014 11:09
12/04/2015 18:41:59 A00016 HCART2 NONE - - - - 2 15/06/2014 14:24
12/04/2015 18:41:59 A00001 HCART2 NONE - - - - 9 15/06/2014 18:57
12/04/2015 18:41:59
12/04/2015 18:41:59 Script exiting...
Press any key to continue . . .
HTH.