You are on page 1of 23

#!

/usr/bin/ksh
#----------------------------------------------------------------------------
# Script : rman_bkup.ksh
# Desc : To perform Oracle RMAN backup commands
# Date : 10/22/2003
# Usage : rman_bkup.ksh <[ -r rman_db_name ]>
# < -t target_db_name >
# <[ -b backup_type ]>
# <[ -a d | t ]>
# [ -e email | p ]
# [ -c d | <rman_script> ]
# [ -s tablespace_name ]
# [ -v nb_ora_serv ]
# [ -y nb_ora_policy ]
# [ -i nb_ora_client ]
# [ -x start_logseq ]
# [ -z end_logseq ]
# [ -l no_of_log_files ]
# [ -w parameter_file ]
# [ -p k | r ]
# [ -m ]
# [ -n ]
# [ -o ]
# [ -g ]
# [ -d ]
# [ -u ]
# [ -q ]
#
# where:
# -r rman_db_name - recovery manager database name.
# -t target_db_name - target database name.
# -b backup_type - valid backup types:
# 0 - level zero backup
# 1 - level one backup
# 2 - level two backup
# a - archivelog backup
# d - duplicate archivelog backup
# t - tablespace backup
# b - full database backup
# -a t - backup to tape
# -a d - backup to disk
# -s tablespace_name - tablespace name, needed during
# tablespace backup.
# -m - Remove old disk backup files from the
# old dir & move previous backup files to
# the old dir.
# -e email - email address to use.
# -e p - use the email address in the
# db_config.dat configuration file.
# -y nb_ora_policy - Netbackup policy to use for the Oracle
# backup.
# -v nb_ora_serv - Name of the Netbackup master server.
# -i nb_ora_client - Name of the Netbackup client.
# -c d - Use the default compiled RMAN script.
# -c rman_script - Name of the RMAN command script.
# If this parameter is passed parameters
# [<-b backup_type >] and [<-a d|t>]
# are not anymore required.
# -x start_logseq - Backup archivelog starting from this
# sequence number.
# -z end_logseq - Backup archivelog ending from this
# sequence number.
# -l no_of_log_files - Number of RMAN log files to retain.
# -w parameter_file - Allows the script to get and set up
# some or all its backup configuration
# parameters by using this parameter
# file. It overwrites backup parameter
# settings past in the script, if the
# parameter has been defined in the
# file.
# -p k - Include the archive log in the backup
# and keep it.
# -p r - Include the archive log in the backup
# and remove it.
# -n - Don't do any backup, just create the
# command file.
# -o - Over write the command file and
# continue backup.
# -g - Use the Netbackup enviromental
# variables to set up the backup to
# be done.
# -d - Backup only datafiles that have not
# been backup. Use in conjunction
# with full database backup.
# -u - Do an RMAN NOCATALOG backup.
# If this parameter is passed parameter
# <[ -r rman_db_name ]> is not anymore
# required.
# -q - Compress the RMAN disk backup files.
#
#----------------------------------------------------------------------------
# REQUIRED FILE:
#
# Make sure the following file exist and are located in the directory this
# script resides.
#
# db_config.dat
#
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# Replace the CURR_DIR variable (below) with the directory path where this
# script is currently located.
#----------------------------------------------------------------------------
sCnt=`ps -ef | awk '$1 == "oracle"' | grep -v grep | grep -c rman`
if [ $sCnt -gt "0" ]
then
exit
else
CURR_DIR=/orabin/apps/oracle/dbadm/general/config
CONFIG_FILE=$CURR_DIR/.db_config
#SBT_LIB="/usr/openv/netbackup/bin/libobk.so64.1"

sScriptName=$(basename $0)
sUser=$(whoami)
sGroup=$(groups)
sHostName=$(hostname)
sFileNames=${sScriptName%.ksh}

AWK="/usr/bin/awk"
PSEF="/usr/bin/ps -ef"
GREP="/usr/bin/grep"
WC="/usr/bin/wc -l"
CAT="/usr/bin/cat"
CHMOD="/usr/bin/chmod"
LS="/usr/bin/ls"
TOUCH="/usr/bin/touch"
MAILX="/usr/bin/mailx"
RM="/usr/bin/rm"
MV="/usr/bin/mv"
COMPRESS="/usr/bin/compress"
UNCOMPRESS="/usr/bin/uncompress"
SORT="/usr/bin/sort"
DF="/usr/bin/df"
DATE="/usr/bin/date"
ID="/usr/bin/id"
ENV="/usr/bin/env"
TAIL="/usr/bin/tail"
typeset -i iSw=0
typeset -i iCtr=0
typeset -i iStartSeq=0
typeset -i iEndSeq=0
typeset -i iNoOfLogFiles=10
typeset -i iLogFiles=0

typeset -u sMoveRemove=""
typeset -u sNoBackup=""
typeset -u sOverWrite=""
typeset -u sGetNbEnv=""
typeset -u sNotBackup=""
typeset -u sNoCatalog=""
typeset -u sCompress=""

sRmanDb=""
sTargetDb=""
sDiskTape=""
sBkupType=""

RMAN=""
sPassword=""
sRmanUser=""
sTargetUser=""
sPwdRman=""
sPwdTarget=""
sStr=""

sBackupDir=""
sTag=""
sMsgLog=""
sParamFile=""

sRmanFileName=""
sTablespace=""
sMail=""
sMoveFiles=""
sSendMail=""
sParms=""
s2ndArchDir=""
sStoredScript=""
sCommand=""
sNbPolicy=""
sNbServer=""
sNbPCSched=""
sNbClient=""
sPlusArch=""
sSbtLib=""

sErrFile=$CURR_DIR/$sFileNames.err

# FUNCTION : scriptUsage
# PARAMETERS: None
# DESC : Prints the usage of this script.

function scriptUsage
{
print "USAGE: $sScriptName <[ -r rman_db_name ]>"
print " < -t target_db_name>"
print " <[ -b backup type ]>"
print " <[ -a d|t ]>"
print " [ -y nb_ora_policy ]"
print " [ -v nb_ora_serv ]"
print " [ -e email | p ]"
print " [ -s tablespace_name ]"
print " [ -c d | <stored_script>]"
print " [ -x start_logseq ]"
print " [ -z end_logseq ]"
print " [ -l no_of_log_files ]"
print " [ -w parameter_file ]"
print " [ -p k | r ]"
print " [ -m ]"
print " [ -n ]"
print " [ -o ]"
print " [ -g ]"
print " [ -d ]"
print " [ -u ]"
print " [ -q ]"

exit 6
}

# FUNCTION : errorMsg
# PARAMETERS: error_message
# DESC : Send and log error encountered during execution of this script.

function errorMsg
{
sErrMsg=$1
sMsgRMAN=""

if [[ -f $sErrFile ]] then
iSw=$( $CAT $sErrFile | $WC)

if (( iSw > 100 ))


then
$RM $sErrFile
$TOUCH $sErrFile
$CHMOD g+w $sErrFile
fi
else
$TOUCH $sErrFile
$CHMOD g+w $sErrFile
fi

print $($DATE) >> $sErrFile


print "\n$sErrMsg"
print "$sErrMsg \n" >> $sErrFile

# Don't email if their is no email address.


if [[ $sMail != "" ]] then

if [[ -f $sMsgLog ]] then
sMsgRMAN=$($TAIL -15 $sMsgLog)
sMsgRMAN="\n\n"$sMsgRMAN
fi

print $sErrMsg" "$sMsgRMAN | $MAILX -s "$sTargetDb($sHostName) $sDiskTape


Backup Error!!!" $sMail
fi

exit 16
}

# FUNCTION : createScript
# PARAMETERS: maxopenfiles filesperset number_of_channels
# DESC : Create the RMAN command script

function createScript
{
sMax=$1
sSet=$2
iC=$3
sDir=""
sDir1=""

if [[ $sDiskTape == "DISK" ]] then


sDir="$sBackupDir/"
sDir1="$sBackupDir1/"
fi

if [[ $sBkupType == "0" ]] then


sBk="level_zero"
elif [[ $sBkupType == "1" ]] then
sBk="level_one"
elif [[ $sBkupType == "2" ]] then
sBk="level_two"
elif [[ $sBkupType == "a" ]] then
sBk="archivelog"
elif [[ $sBkupType == "d" ]] then
sBk="dup_arch"
elif [[ $sBkupType == "b" ]] then
sBk="b"
else
sBk="tablespace"
fi

sTag=$sTargetDb"_"$sHostName"_"$sBk`date "+%m%d%y"`

sRmanFileName=$sBackupDir/$sTargetDb"_"$sBk"_backup.cmd"

if [[ -f $sRmanFileName && $sOverWrite == "" ]] then


errorMsg "ERROR(6)-> File $sRmanFileName found aborting backup..."
fi

if [[ $sStoredScript == "d" ]] then

sStoredScript=$sBk"_backup"
print "run {EXECUTE SCRIPT $sStoredScript;}" > $sRmanFileName

else

print "run {" > $sRmanFileName


# iCtr=1
# while (($iCtr <= $iC))
# do
# print "ALLOCATE CHANNEL c$iCtr TYPE $sDiskTape MAXOPENFILES $sMax" >>
$sRmanFileName
# print $sParms >> $sRmanFileName
# (( iCtr = $iCtr + 1 ))
# done
fdate=`date +"%m-%d-%y"`
print "ALLOCATE CHANNEL c1 TYPE $sDiskTape MAXOPENFILES $sMax FORMAT
'$sDir"$sTargetDb"_"$sBkupType"_%U_"$fdate".bak' maxpiecesize 50g" >>
$sRmanFileName
print $sParms >> $sRmanFileName
print "ALLOCATE CHANNEL c2 TYPE $sDiskTape MAXOPENFILES $sMax FORMAT
'$sDir1"$sTargetDb"_"$sBkupType"_%U_"$fdate".bak' maxpiecesize 50g" >>
$sRmanFileName
print $sParms >> $sRmanFileName

print "ALLOCATE CHANNEL c3 TYPE $sDiskTape MAXOPENFILES $sMax FORMAT


'$sDir"$sTargetDb"_"$sBkupType"_%U_"$fdate".bak' maxpiecesize 50g" >>
$sRmanFileName
print $sParms >> $sRmanFileName
print "ALLOCATE CHANNEL c4 TYPE $sDiskTape MAXOPENFILES $sMax FORMAT
'$sDir1"$sTargetDb"_"$sBkupType"_%U_"$fdate".bak' maxpiecesize 50g" >>
$sRmanFileName
print $sParms >> $sRmanFileName

if [[ $sBkupType != "a" && $sBkupType != "d" ]] then

if [[ $sBkupType != "t" ]] then


if [[ $sBkupType == "b" ]] then

# Backup only datafiles that had not been back up.


if [[ $sNotBackup == "Y" ]] then
print "BACKUP DATABASE NOT BACKED UP FILESPERSET $sSet" >>
$sRmanFileName
else
print "CROSSCHECK ARCHIVELOG ALL;" >> $sRmanFileName
# print "DELETE EXPIRED ARCHIVELOG ALL;" >> $sRmanFileName
print "BACKUP as compressed backupset DATABASE FILESPERSET $sSet" >>
$sRmanFileName
fi

print "TAG $sTag" >> $sRmanFileName


# print "FORMAT '$sDir"$sTargetDb"_"$sBkupType"_%U.bak'" >>
$sRmanFileName
else
print "CROSSCHECK ARCHIVELOG ALL;" >> $sRmanFileName
print "BACKUP as compressed backupset INCREMENTAL LEVEL $sBkupType
FILESPERSET $sSet database" >> $sRmanFileName
print "TAG $sTag" >> $sRmanFileName
# print "FORMAT '$sDir"$sTargetDb"_"$sBkupType"_%U.bak' database" >>
$sRmanFileName
fi

else
print "BACKUP TABLESPACE \"$sTablespace\" FILESPERSET $sSet" >>
$sRmanFileName
print "TAG $sTag" >> $sRmanFileName
# print "FORMAT '$sDir"$sTargetDb"_"$sBkupType"_%U.bak'" >> $sRmanFileName
fi

# Include the archive log in the backup.


if [[ $sPlusArch == "k" ]] then
print "PLUS ARCHIVELOG;" >> $sRmanFileName
elif [[ $sPlusArch == "r" ]] then
print "PLUS ARCHIVELOG DELETE ALL INPUT;" >> $sRmanFileName
print "DELETE NOPROMPT FORCE OBSOLETE;" >> $sRmanFileName
else
print ";" >> $sRmanFileName
print "DELETE OBSOLETE;" >> $sRmanFileName
fi

else
print "SQL 'alter system switch logfile';" >> $sRmanFileName
print "BACKUP FILESPERSET $sSet " >> $sRmanFileName
print "FORMAT '$sDir"$sTargetDb"_"$sBkupType"_%U.bak'" >> $sRmanFileName
print "TAG $sTag" >> $sRmanFileName

if [[ $sBkupType == "a" ]] then

if (( iStartSeq > 0 && iEndSeq > 0 )) then


print "ARCHIVELOG FROM SEQUENCE $iStartSeq UNTIL SEQUENCE $iEndSeq
DELETE INPUT;" >> $sRmanFileName
elif (( iStartSeq > 0 )) then
print "ARCHIVELOG FROM SEQUENCE $iStartSeq DELETE INPUT;" >>
$sRmanFileName
elif (( iEndSeq > 0 )) then
print "ARCHIVELOG UNTIL SEQUENCE $iEndSeq DELETE INPUT;" >>
$sRmanFileName
else
print "ARCHIVELOG ALL DELETE INPUT;" >> $sRmanFileName
fi
else
print "ARCHIVELOG LIKE '$s2ndArchDir/arch%' DELETE INPUT;" >>
$sRmanFileName
fi

fi

# iCtr=1
# while (($iCtr <= $iC))
# do
# print "RELEASE CHANNEL c$iCtr;" >> $sRmanFileName
# (( iCtr = $iCtr + 1 ))
# done

print "RELEASE CHANNEL c1;" >> $sRmanFileName


print "RELEASE CHANNEL c2;" >> $sRmanFileName
print "RELEASE CHANNEL c3;" >> $sRmanFileName
print "RELEASE CHANNEL c4;" >> $sRmanFileName

print "}" >> $sRmanFileName

$CHMOD g+w $sRmanFileName


fi
}

# FUNCTION : compressFile
# PARAMETERS: None
# DESC : Compress RMAN backup files.

function compressFile
{
sCompressFile=""

sStr=$($LS -1t $sBackupDir/*.bak)

if [[ $sStr != "" ]] then

for sFile in $sStr


do
if [[ -f $sFile ]] then
# print "Compressing $sFile"
$COMPRESS $sFile

if (( $? > 0 ))
then
iSw=6
sCompressFile="$sCompressFile\nERROR-> Failed to compress file $sFile
..."
fi

fi
done
fi

if (( iSw > 0 ))
then
errorMsg "$sCompressFile"
fi

# FUNCTION : remove_move_file
# PARAMETERS: list_of_rman_backup_files
# DESC : Moves and removes RMAN disk backup files.

function remove_move_file
{
sFilesToMove=$1
sMoveFile=""
sWeekOldDir="$sMoveDir"

if [[ $sFilesToMove != "" ]] then

if [[ ! -d $sWeekOldDir ]] then
errorMsg "Directory $sWeekOldDir not found!!!"
fi

# Empty the week old backup directory


$RM $sWeekOldDir/* 2>/dev/null

for sFile in $sFilesToMove


do
if [[ -f $sFile ]] then
# print "$MV $sFile $sWeekOldDir"
$MV $sFile $sWeekOldDir

if (( $? > 0 ))
then
iSw=6
sMoveFile="$sMoveFile\nERROR-> Failed to move file $sFile ..."
fi

fi
done

fi

if (( iSw > 0 ))
then
errorMsg "$sMoveFile"
fi

# FUNCTION : remove_log_files
# PARAMETERS: list_of_rman_log_files
# DESC : Removes old RMAN log files.
function remove_log_files
{
sLogFiles=$1
iLogFileCtr=1
sRmFile=""
if [[ $sLogFiles != "" ]] then

for sFile in $sLogFiles


do
#print "FILE: $sFile"
if [[ -w $sBackupDir/$sFile ]] then
# By default there should be not more than 10 log files.
if (( iLogFileCtr > iNoOfLogFiles ))
then

#print "$RM $sBackupDir/$sFile"


$RM $sBackupDir/$sFile

if (( $? > 0 ))
then
iSw=6
sRmFile="$sRmFile\nERROR-> Failed to remove file $sFile ..."
fi
else
(( iLogFileCtr = iLogFileCtr + 1 ))
fi

#print "iLogFileCtr = $iLogFileCtr"


fi
done

fi

if (( iSw > 0 ))
then
errorMsg "$sRmFile"
fi

# FUNCTION : checkProcesses
# PARAMETERS: None
# DESC : Check if the target database process are running.

function checkProcesses
{
typeset -i iSw=4

if (( $($PSEF | $GREP "ora_pmon_$sTargetDb" | $WC -l) > 1 ))


then
if (( $($PSEF | $GREP "ora_smon_$sTargetDb" | $WC -l) > 1 ))
then
if (( $($PSEF | $GREP "ora_lgwr_$sTargetDb" | $WC -l) > 1 ))
then
if (( $($PSEF | $GREP "ora_dbw0_$sTargetDb" | $WC -l) > 1 ))
then
if (( $($PSEF | $GREP "ora_ckpt_$sTargetDb" | $WC -l) > 1 ))
then
iSw=0
fi
else
iSw=1
fi
else
iSw=2
fi
else
iSw=3
fi
fi

print $iSw
}

# FUNCTION : get_nb_env
# PARAMETERS: None
# DESC : Use to get the Netbackup enviromental variables

function get_nb_env
{
sNbPolicy=$($ENV | $GREP ^"NB_ORA_POLICY" | $AWK -F= '{print $2}')
sNbServer=$($ENV | $GREP ^"NB_ORA_SERV" | $AWK -F= '{print $2}')
sNbPCSched=$($ENV | $GREP ^"NB_ORA_PC_SCHED" | $AWK -F= '{print $2}')
sNbClient=$($ENV | $GREP ^"NB_ORA_CLIENT" | $AWK -F= '{print $2}')

#print "sNbPolicy = $sNbPolicy"


#print "sNbServer = $sNbServer"
#print "sNbPCSched = $sNbPCSched"
#print "sNbClient = $sNbClient"

if [[ $sNbPCSched == "0" ]] then


sBkupType="0"
elif [[ $sNbPCSched == "1" ]] then
sBkupType="1"
elif [[ $sNbPCSched == "2" ]] then
sBkupType="2"
elif [[ $sNbPCSched == "arch" ]] then
sBkupType="a"
elif [[ $sNbPCSched == "dup" ]] then
sBkupType="d"
elif [[ $sNbPCSched == "db" ]] then
sBkupType="b"
else
if [[ $sNbPCSched == "" ]] then
errorMsg "No NB_ORA_PC_SCHED was passed!!!"
else
errorMsg "An invalid NB_ORA_PC_SCHED $sNbPCSched was passed!!!"
fi
fi

# Netbackup does tape backup only


sDiskTape="t"

}
# FUNCTION : getDbConfig
# PARAMETERS: database_name
# DESC : Gets the db configuration from the db_config.dat file.

function getDbConfig
{
sDb=$1
cSwRec="0"
sOraHome=""
sUserName=""
sUserPwd=""
sDbHost=""
sDbHostAlias=""
sRmanDir=""
sMailTo=""
sValue=""
sParam=""
sNbP=""
sNbS=""
sNbC=""
sSBT=""
sMoveDir=""
sRmanDir1=""

#print "sDb = $sDb"


for s in $( $CAT $CONFIG_FILE | $GREP -v ^"#")
do

#print ">>> s = $s "

if [[ $s != "" ]]
then
sParam=$(print $s | $AWK -F= '{print $1}')

# Check if the record had already been found.


if [[ $sParam == "ORACLE_SID" && $cSwRec == "0" ]]
then
sValue=$(print $s | $AWK -F= '{print $2}')

if [[ $sValue == $sDb ]]
then
cSwRec="1"
#print ">>> sParam = $sParam"
#print ">>> sValue = $sValue"
continue
fi
elif [[ $sParam == "ORACLE_SID" && $cSwRec == "1" ]]
then
# Exit when the record had already been found and
# ORACLE_SID parameter had been encountered again.
break
elif [[ $cSwRec == "1" ]]
then
sValue=$(print $s | $AWK -F= '{print $2}')
if [[ $sParam = "ORACLE_HOME" ]] then
sOraHome=$sValue
elif [[ $sParam = "USER_NAME" ]] then
sUserName=$sValue
elif [[ $sParam = "USER_PWD" ]] then
sUserPwd=$sValue
# . /orabin/apps/oracle/dbadm/general/bin/.get_pw dba_sup
# sUserPwd=$rplc
elif [[ $sParam = "DB_HOST" ]] then
sDbHost=$sValue
elif [[ $sParam = "DB_HOST_ALIAS" ]] then
sDbHostAlias=$sValue
elif [[ $sParam = "RMAN_DIR" ]] then
sRmanDir=$sValue
elif [[ $sParam = "RMAN_DIR1" ]] then
sRmanDir1=$sValue
elif [[ $sParam = "MAIL_TO" ]] then
sMailTo="$sMailTo $sValue"
elif [[ $sParam = "NB_ORA_SERV" ]] then
if [[ $sNbServer = "" ]] then
sNbS=$sValue
fi
elif [[ $sParam = "NB_ORA_POLICY" ]] then
if [[ $sNbPolicy = "" ]] then
sNbP=$sValue
fi
elif [[ $sParam = "NB_ORA_CLIENT" ]] then
if [[ $sNbClient = "" ]] then
sNbC=$sValue
fi
elif [[ $sParam = "SBT_LIBRARY" ]] then
sSBT=$sValue
elif [[ $sParam = "MOVE_DIR" ]] then
sMoveDir=$sValue
else
cSwRec="2"
break
fi
fi

fi
done

if [[ $cSwRec == "0" ]] then


print ""
elif [[ $cSwRec == "2" ]] then
print "ERROR"
else
print
"$sOraHome:$sUserName:$sUserPwd:$sDbHost:$sDbHostAlias:$sRmanDir:$sMailTo:$sNbP:$s
NbS:$sNbC:$sSBT:$sMoveDir:$sRmanDir1"
fi

# FUNCTION : getParam
# PARAMETERS: None
# DESC : Use the parameters in the past parameter file.
function getParam
{
sParam=""
sValue=""

# Check if the parameter file exists.


if [[ ! -f $CURR_DIR/$sParamFile ]] then
errorMsg "ERROR-> File $CURR_DIR/$sParamFile not found!"
fi

for s in $( $CAT $CURR_DIR/$sParamFile | $GREP -v ^"#")


do

#print ">>> s = $s "

if [[ $s != "" ]] then

sParam=$(print $s | $AWK -F= '{print $1}')


sValue=$(print $s | $AWK -F= '{print $2}')

if [[ $sValue != "" ]] then


if [[ $sParam = "RMAN_DB" ]] then
sRmanDb=$sValue
elif [[ $sParam = "TARGET_DB" ]] then
sTargetDb=$sValue
elif [[ $sParam = "BKUP_TYPE" ]] then
sBkupType=$sValue
elif [[ $sParam = "DISK_TAPE" ]] then
sDiskTape=$sValue
elif [[ $sParam = "TABLESPACE" ]] then
sTablespace=$sValue
elif [[ $sParam = "SEND_MAIL" ]] then
sSendMail="$sSenMail $sValue"
elif [[ $sParam = "NB_POLICY" ]] then
sNbPolicy=$sValue
elif [[ $sParam = "NB_SERVER" ]] then
sNbServer=$sValue
elif [[ $sParam = "NB_CLIENT" ]] then
sNbClient=$sValue
elif [[ $sParam = "STORED_SCRIPT" ]] then
sStoredScript=$sValue
elif [[ $sParam = "START_SEQ" ]] then
iStartSeq=$sValue
elif [[ $sParam = "END_SEQ" ]] then
iEndSeq=$sValue
elif [[ $sParam = "LOG_FILES" ]] then
iLogFiles=$sValue
elif [[ $sParam = "MOVE_REMOVE" ]] then
sMoveRemove=$sValue
elif [[ $sParam = "NO_BACKUP" ]] then
sNoBackup=$sValue
elif [[ $sParam = "OVER_WRITE" ]] then
sOverWrite=$sValue
elif [[ $sParam = "GET_NB_ENV" ]] then
sGetNbEnv=$sValue
elif [[ $sParam = "NOT_BACKUP" ]] then
sNotBackup=$sValue
elif [[ $sParam = "PLUS_ARCH" ]] then
sPlusArch=$sValue
elif [[ $sParam = "NOCATALOG" ]] then
sNoCatalog=$sValue
elif [[ $sParam = "COMPRESS" ]] then
sCompress=$sValue
else
errorMsg "ERROR-> Parameter $sParam is an invalid parameter!"
break
fi

# Check if the correct value was passed.


if [[ $sParam = "MOVE_REMOVE" || \
$sParam = "NO_BACKUP" || \
$sParam = "OVER_WRITE" || \
$sParam = "GET_NB_ENV" || \
$sParam = "NOCATALOG" || \
$sParam = "COMPRESS" || \
$sParam = "NOT_BACKUP" ]]
then
if [[ $sValue != "Y" && $sValue != "y" && $sValue != "" ]] then
errorMsg "ERROR-> Parameter $sParam has an invalid value ($sValue)!!!"
fi
fi

fi
fi

done

if [[ $sNoCatalog != "Y" ]] then


# Check if the required parameters have been passed.
if [[ $sRmanDb == "" || $sTargetDb == "" ]] then
errorMsg "ERROR-> RMAN_DB and TARGET_DB are mandatory fields!"
fi
else
if [[ $sTargetDb == "" ]] then
errorMsg "ERROR-> TARGET_DB is a mandatory fields!"
fi
fi

# If no rman stored script was passed or the default stored


# script parameter was passed.
if [[ $sStoredScript == "" || $sStoredScript == "d" ]] then
# Check if it is a valid BKUP_TYPE value.
if [[ $sBkupType != "0" && \
$sBkupType != "1" && \
$sBkupType != "2" && \
$sBkupType != "a" && \
$sBkupType != "d" && \
$sBkupType != "b" && \
$sBkupType != "t" ]]
then
errorMsg "ERROR-> BKUP_TYPE ($sBkupType) field valid values are 0, 1, 2, a,
d, b or t."
fi

# Check if it is a valid DISK_TAPE value.


if [[ $sDiskTape != "t" && $sDiskTape != "d" ]] then
errorMsg "ERROR-> DISK_TAPE field valid values are t or d."
fi

# Check if it is a valid PLUS_ARCH value.


if [[ $sPlusArch != "k" && $sPlusArch != "r" && $sPlusArch != "" ]] then
errorMsg "ERROR-> PLUS_ARCH field valid values are k or r."
fi

fi

##############################################################################
#
# Main
#
##############################################################################

iSw=0

if (( $# >= 2 ))
then

while getopts r:t:b:a:s:e:v:y:i:c:x:z:l:w:p:mnogduq option


do
case $option in
r) sRmanDb=$OPTARG;;
t) sTargetDb=$OPTARG;;
b) sBkupType=$OPTARG;;
a) sDiskTape=$OPTARG;;
s) sTablespace=$OPTARG;;
e) sSendMail=$OPTARG;;
y) sNbPolicy=$OPTARG;;
v) sNbServer=$OPTARG;;
i) sNbClient=$OPTARG;;
c) sStoredScript=$OPTARG;;
x) iStartSeq=$OPTARG;;
z) iEndSeq=$OPTARG;;
l) iLogFiles=$OPTARG;;
w) sParamFile=$OPTARG;;
p) sPlusArch=$OPTARG;;
m) sMoveRemove="Y";;
n) sNoBackup="Y";;
o) sOverWrite="Y";;
g) sGetNbEnv="Y";;
d) sNotBackup="Y";;
u) sNoCatalog="Y";;
q) sCompress="Y";;
*) print "ERROR-> $sScriptName: unknown option $OPTARG"
scriptUsage;;
esac
done

if [[ $sParamFile != "" ]] then


getParam
fi

# Check if backup will be done with NOCATALOG option.


if [[ $sNoCatalog != "Y" ]] then
if [[ $sRmanDb == "" || $sTargetDb == "" ]] then
scriptUsage
fi
else
if [[ $sTargetDb == "" ]] then
scriptUsage
fi

#if [[ $sDiskTape != "d" && $sStoredScript == "" ]] then


# errorMsg "ERROR-> Only disk backup is supported by NOCATALOG option!"
#fi

fi

if [[ $sGetNbEnv == "Y" ]] then


get_nb_env
fi

# If no RMAN command script was passed or the default stored


# script parameter was passed.
if [[ ($sStoredScript == "" || $sStoredScript == "d") && $sBkupType == "" ]]
then
scriptUsage
fi

# Check if it is a valid PLUS_ARCH value.


if [[ $sPlusArch != "k" && $sPlusArch != "r" && $sPlusArch != "" ]] then
errorMsg "ERROR-> valid -p parameter values are k or r."
fi

#
# Validate and get the oracle home, user and password.
#

if [[ -f $CONFIG_FILE ]] then

# Validate the target database name


sStr=$(getDbConfig $sTargetDb)
if [[ $sStr == "" ]] then
errorMsg "ERROR-> Target db name $sTargetDb is an invalid db name!"
elif [[ $sStr == "ERROR" ]] then
errorMsg "ERROR-> An invalid parameter was found in $CONFIG_FILE file!!!"
else
ORACLE_HOME=$(print $sStr | $AWK -F: '{print $1}')
sTargetUser=$(print $sStr | $AWK -F: '{print $2}')
sPwdTarget=$(print $sStr | $AWK -F: '{print $3}')
sBackupDir=$(print $sStr | $AWK -F: '{print $6}')
sMail=$(print $sStr | $AWK -F: '{print $7}')
sSbtLib=$(print $sStr | $AWK -F: '{print $11}')
sMoveDir=$(print $sStr | $AWK -F: '{print $12}')
sBackupDir1=$(print $sStr | $AWK -F: '{print $13}')
# Get the Netbackup enviromental variables only
# if the variables where not passed by Netbackup.
if [[ $sGetNbEnv == "" ]] then
sNbPolicy=$(print $sStr | $AWK -F: '{print $8}')
sNbServer=$(print $sStr | $AWK -F: '{print $9}')
sNbClient=$(print $sStr | $AWK -F: '{print $10}')
fi

# Check if the backup directory exists.


if [[ ! -d $sBackupDir ]] then
errorMsg "ERROR-> Backup directory $sBackupDir not found!!!"
fi

# Check if the ORACLE_HOME directory exists.


if [[ ! -d $ORACLE_HOME ]] then
errorMsg "ERROR-> ORACLE_HOME directory $ORACLE_HOME not found!!!"
fi

# Check if the SBT_LIBRARY was passed


if [[ $sSbtLib != "" ]] then
if [[ ! -f $sSbtLib ]] then
errorMsg "ERROR-> SBT_LIBRARY file $sSbtLib not found!!!"
fi
fi
fi

# If it is NOCATALOG backup don't check the RMAN Db.


if [[ $sNoCatalog != "Y" ]] then
# Validate recovery catalog database name
sStr=$(getDbConfig $sRmanDb)

if [[ $sStr == "" ]] then


errorMsg "ERROR-> RMAN db name $sRmanDb is an invalid db name!"
elif [[ $sStr == "ERROR" ]] then
errorMsg "ERROR-> An invalid parameter was found in $CONFIG_FILE
file!!!"
else
sRmanUser=$(print $sStr | $AWK -F: '{print $2}')
sPwdRman=$(print $sStr | $AWK -F: '{print $3}')
fi
fi

else
errorMsg "ERROR-> Configuration file $CONFIG_FILE not found!!!"
fi

# If no RMAN command script was passed or the default stored


# script should be use.
if [[ $sStoredScript == "" || $sStoredScript == "d" ]] then

if [[ $sDiskTape == "t" ]] then

sDiskTape="'SBT_TAPE'"

if [[ $sNbPolicy != "" || $sNbServer != "" ]] then


if [[ $sNbPolicy == "" || $sNbServer == "" ]] then
errorMsg "Tape Netbackup policy and master server most be supplied!!!"
else
if [[ $sSbtLib != "" ]] then
sParms="parms=\"SBT_LIBRARY=$sSbtLib,"
sParms="$sParms ENV=(NB_ORA_POLICY="$sNbPolicy",
NB_ORA_SERV="$sNbServer
else
sParms="parms=\"ENV=(NB_ORA_POLICY="$sNbPolicy",
NB_ORA_SERV="$sNbServer
fi

if [[ $sNbClient != "" ]] then


sParms="$sParms, NB_ORA_CLIENT="$sNbClient
fi

sParms="$sParms)\";"
fi
else
sParms=";"
fi
elif [[ $sDiskTape == "d" ]] then
sDiskTape="DISK"
sParms=";"
else
scriptUsage
fi

if [[ $sNoCatalog != "Y" ]] then


sCommand="$sScriptName -r $sRmanDb -t $sTargetDb -b $sBkupType ..."
else
sCommand="$sScriptName -t $sTargetDb -b $sBkupType ..."
fi

fi

else
scriptUsage
fi

#
# Check if the target db is available.
#

# if (( $(checkProcesses) > 0 ))
# then
# errorMsg "Database $sTargetDb is not available."
# fi

export NLS_DATE_FORMAT="DD-MON-YYYY HH24:MI:SS"


export ORACLE_SID=$sTargetDb
export ORACLE_HOME
export TNS_ADMIN=$ORACLE_HOME/network/admin
export
LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib:/usr/ucblib:/opt/bin:/bin:/usr/bin
export
PATH=$PATH:$ORACLE_HOME/bin:$ORACLE_BASE/local/bin:$HOME/bin:/usr/bin:/usr/sbin:/b
in:.

sTargetUser="/"
sRmanUser=$sRmanUser/$sPwdRman

#print "sTargetUser = $sTargetUser"


#print "sRmanUser = $sRmanUser"
RMAN="$ORACLE_HOME/bin/rman"

#sErrFile=$sBackupDir/$sFileNames.err

if [[ $sBkupType == "0" || \
$sBkupType == "1" || \
$sBkupType == "2" || \
$sBkupType == "a" || \
$sBkupType == "t" || \
$sBkupType == "b" || \
$sBkupType == "d" ]]
then

if [[ $sBkupType == "a" ]] then


if (( iEndSeq > 0 && iStartSeq > iEndSeq )) then
errorMsg "ERROR-> Archivelog start sequence ($iStartSeq) should be less than
or equal to the end sequence ($iEndSeq) !!!"
fi
fi

if [[ $sBkupType = "t" && $sTablespace = "" ]] then


errorMsg "ERROR-> Tablespace backup should have a tablespace name!!!"
fi

if [[ $sBkupType == "d" ]] then


# Assuming the archivelog secondary directory path is correct.
# (i.e. /gcw01/app/oracle/admin/dup_arch/GCW)
s2ndArchDir=${ORACLE_HOME%%/product/*}"/admin/dup_arch/$sTargetDb"

if [[ ! -d $s2ndArchDir ]] then
errorMsg "ERROR-> Archivelog directory $s2ndArchDir not found!!!"
fi

fi

if [[ $sDiskTape == "DISK" ]] then

createScript 3 1 4
if [[ $sMoveRemove == "Y" ]] then
sMoveFiles=$($LS -1 $sBackupDir/*.bak)
echo $sMoveFiles
echo $sBackupDir
if [[ $sBkupType == "0" && $sMoveRemove == "Y" ]] then
remove_move_file "$sMoveFiles"
echo $sMoveRemove
fi

fi
else
createScript 3 16 4
fi

elif [[ $sStoredScript != "" && $sStoredScript != "d" ]] then

#csRmanFileName=$sBackupDir/$sStoredScript".cmd"
sRmanFileName=$sBackupDir/$sStoredScript
#if [[ -f $sRmanFileName && $sOverWrite == "" ]] then
# errorMsg "ERROR(6)-> File $sRmanFileName found aborting backup..."
#fi

# print "run {EXECUTE SCRIPT $sStoredScript;}" > $sRmanFileName

if [[ ! -f $sRmanFileName ]] then
errorMsg "ERROR(6)-> File $sRmanFileName not found, aborting backup..."
fi

if [[ $sNoCatalog != "Y" ]] then


sCommand="$sScriptName -r $sRmanDb -t $sTargetDb -c $sStoredScript ..."
else
sCommand="$sScriptName -t $sTargetDb -c $sStoredScript ..."
fi

sBkupType="s"
else
errorMsg "ERROR-> Invalid command parameter: $sBkupType"
fi

# Number of RMAN log files to retain passed.


if (( iLogFiles > 0 )) then
iNoOfLogFiles=$iLogFiles
fi

sMsgLog=$sBackupDir/$sTargetDb"_"$sBkupType"_bkup."$($DATE +%Y%m%d_%H%M%S)".log"

#
# Execute the RMAN command file.
#

if [[ -f $sRmanFileName ]] then

# Don't do any backup if sNoBackup = Y


if [[ $sNoBackup == "" ]] then

if [[ $sNoCatalog != "Y" ]] then

$RMAN msglog=\'$sMsgLog\' append catalog=\'$sRmanUser@$sRmanDb\' \


target=\'$sTargetUser\' cmdfile=\'$sRmanFileName\'
iSw=$?

else

$RMAN msglog=\'$sMsgLog\' append \


target=\'$sTargetUser\' cmdfile=\'$sRmanFileName\'

iSw=$?
fi

fi

else
errorMsg "ERROR(6)-> File $sRmanFileName not found aborting backup..."
fi
if (( iSw > 0 ))
then
errorMsg "ERROR($iSw)-> failed to execute: $sCommand"
fi

#
# Delete the rman command file
#

# Don't delete the command file if sNoBackup = Y


if [[ -f $sRmanFileName && $sNoBackup == "" && \
($sStoredScript == "" || $sStoredScript == "d") ]]
then
$RM $sRmanFileName
fi

#
# Check if the an email should be sent when the backup is successful.
#
if [[ $sSendMail != "" ]] then

# Check if the email address in the db_config.dat file should be used.


if [[ $sSendMail == "p" ]] then
sSendMail=$sMail
fi

print "$sDiskTape Bakup Successful: $sCommand" | $MAILX -s "Backup Successfull


on $sTargetDb($sHostName)" $sSendMail
fi

#
# Compress disk backup files
#

if [[ $sTag != "" && \


$sDiskTape == "DISK" && \
$sNoBackup == "" && \
$sCompress == "Y" ]]
then
compressFile
fi

#
# Remove old backup and move previous bakup to the week old backup directory.

# Moved below create script

#if [[ $sBkupType == "0" && $sMoveRemove == "Y" ]] then


# remove_move_file "$sMoveFiles"
#fi

#
# Remove old rman log files.
#

sStr=""
sStr=$($LS -1t "$sBackupDir" | $GREP ^"$sTargetDb" | $GREP ".log" )
remove_log_files "$sStr"

exit 0

fi