Professional Documents
Culture Documents
/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 [[ -f $sMsgLog ]] then
sMsgRMAN=$($TAIL -15 $sMsgLog)
sMsgRMAN="\n\n"$sMsgRMAN
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=""
sTag=$sTargetDb"_"$sHostName"_"$sBk`date "+%m%d%y"`
sRmanFileName=$sBackupDir/$sTargetDb"_"$sBk"_backup.cmd"
sStoredScript=$sBk"_backup"
print "run {EXECUTE SCRIPT $sStoredScript;}" > $sRmanFileName
else
else
print "BACKUP TABLESPACE \"$sTablespace\" FILESPERSET $sSet" >>
$sRmanFileName
print "TAG $sTag" >> $sRmanFileName
# print "FORMAT '$sDir"$sTargetDb"_"$sBkupType"_%U.bak'" >> $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
fi
# iCtr=1
# while (($iCtr <= $iC))
# do
# print "RELEASE CHANNEL c$iCtr;" >> $sRmanFileName
# (( iCtr = $iCtr + 1 ))
# done
# FUNCTION : compressFile
# PARAMETERS: None
# DESC : Compress RMAN backup files.
function compressFile
{
sCompressFile=""
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 [[ ! -d $sWeekOldDir ]] then
errorMsg "Directory $sWeekOldDir not found!!!"
fi
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
if (( $? > 0 ))
then
iSw=6
sRmFile="$sRmFile\nERROR-> Failed to remove file $sFile ..."
fi
else
(( iLogFileCtr = iLogFileCtr + 1 ))
fi
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
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}')
}
# 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=""
if [[ $s != "" ]]
then
sParam=$(print $s | $AWK -F= '{print $1}')
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
# FUNCTION : getParam
# PARAMETERS: None
# DESC : Use the parameters in the past parameter file.
function getParam
{
sParam=""
sValue=""
if [[ $s != "" ]] then
fi
fi
done
fi
##############################################################################
#
# Main
#
##############################################################################
iSw=0
if (( $# >= 2 ))
then
fi
#
# Validate and get the oracle home, user and password.
#
if [[ -f $CONFIG_FILE ]] then
else
errorMsg "ERROR-> Configuration file $CONFIG_FILE not found!!!"
fi
sDiskTape="'SBT_TAPE'"
sParms="$sParms)\";"
fi
else
sParms=";"
fi
elif [[ $sDiskTape == "d" ]] then
sDiskTape="DISK"
sParms=";"
else
scriptUsage
fi
fi
else
scriptUsage
fi
#
# Check if the target db is available.
#
# if (( $(checkProcesses) > 0 ))
# then
# errorMsg "Database $sTargetDb is not available."
# fi
sTargetUser="/"
sRmanUser=$sRmanUser/$sPwdRman
#sErrFile=$sBackupDir/$sFileNames.err
if [[ $sBkupType == "0" || \
$sBkupType == "1" || \
$sBkupType == "2" || \
$sBkupType == "a" || \
$sBkupType == "t" || \
$sBkupType == "b" || \
$sBkupType == "d" ]]
then
if [[ ! -d $s2ndArchDir ]] then
errorMsg "ERROR-> Archivelog directory $s2ndArchDir not found!!!"
fi
fi
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
#csRmanFileName=$sBackupDir/$sStoredScript".cmd"
sRmanFileName=$sBackupDir/$sStoredScript
#if [[ -f $sRmanFileName && $sOverWrite == "" ]] then
# errorMsg "ERROR(6)-> File $sRmanFileName found aborting backup..."
#fi
if [[ ! -f $sRmanFileName ]] then
errorMsg "ERROR(6)-> File $sRmanFileName not found, aborting backup..."
fi
sBkupType="s"
else
errorMsg "ERROR-> Invalid command parameter: $sBkupType"
fi
sMsgLog=$sBackupDir/$sTargetDb"_"$sBkupType"_bkup."$($DATE +%Y%m%d_%H%M%S)".log"
#
# Execute the RMAN command file.
#
if [[ -f $sRmanFileName ]] then
else
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
#
#
# Check if the an email should be sent when the backup is successful.
#
if [[ $sSendMail != "" ]] then
#
# Compress disk backup files
#
#
# Remove old backup and move previous bakup to the week old backup directory.
#
# Remove old rman log files.
#
sStr=""
sStr=$($LS -1t "$sBackupDir" | $GREP ^"$sTargetDb" | $GREP ".log" )
remove_log_files "$sStr"
exit 0
fi