It pretty big Script Rakesh.I am pasting a part of it,Let me know if this will help
sub GetBackupIDs {
$VerifyTapeExists=`vmquery -m $MediaID -l 2>&1`;
# print ERRORLOG "Debug Line vmquery output $VerifyTapeExists\n";
if ($VerifyTapeExists =~ "volume does not exist") {
print ERRORLOG "$MediaID does not exist in $MEDIAMGR volume database\n";
# Put end time into log file
$SCRIPTTIME=`date`;
chomp $SCRIPTTIME;
print ERRORLOG "\n$SCRIPTTIME bkuptapedupimages.pl script ended\n";
exit(14);
} # if $VerifyTapeExists
(@MediaInfo)=split(/\s+/,$VerifyTapeExists);
# As tape exists, verify that it is still assigned
if (@MediaInfo[19] =~ "00/00/0000") {
print ERRORLOG "$MediaID is not assigned so NBU catalogue has no images to duplicate.\n";
# Put end time into log file
$SCRIPTTIME=`date`;
chomp $SCRIPTTIME;
print ERRORLOG "\n$SCRIPTTIME bkuptapedupimages.pl script ended\n";
exit(14);
} # if (@MediaInfo[19]
# Subtract 7 days from assigned date for image query
# Add 7 days to last mount to set end time for image query
# verify date diff of Start and End time, if over 30 days then break out into multiple queries
$GetStartTime = &DateCalc("@MediaInfo[19]", "-7 days", \$err);
chomp $GetStartTime;
$SDate = substr($GetStartTime, 0, 8);
$syear = substr($SDate, 0, 4);
$smon = substr($SDate, 4, 2);
$sday = substr($SDate, 6, 2);
$adjsyear=$syear;
$adjsmon=$smon;
$adjsday=$sday;
$StartTime="$smon/$sday/$syear 00:00:00";
$AdjStartTime="$smon/$sday/$syear 00:00:00";
$GetEndTime = &DateCalc("@MediaInfo[23]", "+7 days", \$err);
chomp $GetEndTime;
$EDate = substr($GetEndTime, 0, 8);
$eyear = substr($EDate, 0, 4);
$emon = substr($EDate, 4, 2);
$eday = substr($EDate, 6, 2);
$EndTime="$emon/$eday/$eyear 23:59:59";
$SearchDeltaDays=Delta_Days($syear,$smon,$sday,$eyear,$emon,$eday);
print ERRORLOG "The delta days of the search is $SearchDeltaDays\n";
while ($SearchDeltaDays >= 1) {
($adjeyear,$adjemon,$adjeday)=Add_Delta_Days($adjsyear,$adjsmon,$adjsday,30);
$AdjEndTime="$adjemon/$adjeday/$adjeyear 23:59:59";
print ERRORLOG "$NBUADMCMD/bpduplicate -PM -id $MediaID -s $AdjStartTime -e $AdjEndTime 2>&1\n";
my ($IMAGEIDS) = FileHandle->new("$NBUADMCMD/bpduplicate -PM -id $MediaID -s $AdjStartTime -e $AdjEndTime 2>&1 |");
while (<$IMAGEIDS>) {
if ($_ =~ "found no images") {
print ERRORLOG "$_ \n";
$ValidQuery='Y';
} elsif ($_ =~ "unexpected return value") {
print ERRORLOG "Query failed, reattempting \n $_ \n";
$ValidQuery='N';
} else {
push (@BKUPIDS,$_);
$ValidQuery='Y';
} # if $_
} # while
$IMAGEIDS->close;
if ($ValidQuery eq "Y") {
$adjsyear=$adjeyear;
$adjsmon=$adjemon;
$adjsday=$adjeday;
$AdjStartTime = "$adjsmon/$adjsday/$adjsyear 00:00:00";
$SearchDeltaDays=Delta_Days($adjsyear,$adjsmon,$adjsday,$eyear,$emon,$eday);
} #if $ValidQuery
} # while
if (
!@BKUPIDS) {
print ERRORLOG "No backup images found for tape $MediaID in date range of $StartTime to $EndTime\n";
exit (16);
} # if
!@BKUPIDS return @BKUPIDS;
} # sub GetBkupIDs