RMAN catalog maintenance script

posted Sep 16, 2010, 9:31 AM by Sachchida Ojha
#!/bin/sh
PATH=/bin:/sbin:/usr/sbin:/usr/bin:/usr/ucb:$PATH:/u01/PRDDB/app/oracle/product/1020/bin:/bin:/usr/openv/netbackup/bin/admincmd
export PATH
TMPFILE=/tmp/catalog_maint.$$
ERROR=0
NOTIFY="sa@dbaref.com,dba@dbaref.com"

NB_ORA_SCRIPTS=/u01/PRDDB/oradba/db/tasks/backup
ENV=$NB_ORA_SCRIPTS/dbaref_rman.env
RMAN=rman
NB_ORA_SERV=va-em01

# ============================================================================
#   Define Sub-routines here...
# ============================================================================

sendit()
{

if [ $ERROR -eq 0 ] ; then
      {

      echo
      echo "============$OUTF==================================="
      echo
      echo
      if [ -f $OUTF ] ; then
         cat $OUTF
      else
         echo "No $OUTF"
      fi
   } | mailx -s "Success `hostname` RMAN catalog maintenance of $ORACLE_SID" $NOTIFY
   rm -f $TMPFILE

else
   {
      echo "ERROR.............."
      echo "============$TMPFILE================================"
      echo
      echo
      if [ -f $TMPFILE ] ; then
         cat $TMPFILE
      else
         echo "No $TMPFILE"
      fi
      echo
      echo
      echo
      echo "============$OUTF==================================="
      echo
      echo
      if [ -f $OUTF ] ; then
         cat $OUTF
      else
         echo "No $OUTF"
      fi
   } | mailx -s "ERROR [$ERROR] `hostname` RMAN catalog maintenance of $ORACLE_SID" $NOTIFY
fi

rm -f $TMPFILE
}


# ============================================================================
#   End of Sub-routine definitions...
# ============================================================================


if [ ! -f $ENV ] ; then
   echo "ERROR: didn't find env file [$ENV]" >> $TMPFILE
   echo "Exiting out" >> $TMPFILE
   ERROR=99
   sendit
   exit $ERROR
fi


# Test to see if there are any valid lines, or if it is all comments
#
count=`cat $ENV | egrep -v "^#" | wc -l`
if [ $count -eq 0 ] ; then
    echo "ERROR: $ENV didn't contain anything but comments" >> $TMPFILE
    echo "Exiting out" >> $TMPFILE
    ERROR=100
    sendit
    exit $ERROR
fi

exec < $ENV
while read i
do
   echo $i | /usr/bin/grep -q "#"
   if [ $? -eq 0 ] ; then
     continue
   fi

   count=`echo $i | awk -F: '{print NF}' `
   if [ $count -ne 3 ] ; then
      echo "Wrong number of fields, there were $count" >> $TMPFILE
      echo "Skipping [$i]" >> $TMPFILE
      echo >> $TMPFILE
      continue
   fi

   ORACLE_HOME=`echo $i | awk -F: '{print $1}' `
   ORACLE_SID=`echo $i  | awk -F: '{print $2}' `
   ORACLE_USER=`echo $i | awk -F: '{print $3}' `

   OUTF=${NB_ORA_SCRIPTS}/${ORACLE_SID}-catalog_maint.out"-`date +%b-%d-%R`"
   echo "[$ORACLE_HOME] [$ORACLE_SID] [$ORACLE_USER] [$OUTF]" >> $TMPFILE


   if [ -f "$OUTF" ] ; then
      rm -f "$OUTF"
   fi

   export ORACLE_HOME
   export ORACLE_SID
   export ORACLE_USER
   export OUTF

   echo "`date` ---------Beginning of $ORACLE_SID catalog maintenance ------------" >> $OUTF
   echo "Script name: $0 - for SID $ORACLE_SID" >> $OUTF
   BDATE=`date`

   echo   >> $OUTF
   echo   "RMAN: $RMAN" >> $OUTF
   echo   "ORACLE_SID: $ORACLE_SID" >> $OUTF
   echo   "ORACLE_USER: $ORACLE_USER" >> $OUTF
   echo   "ORACLE_HOME: $ORACLE_HOME" >> $OUTF
   echo   "NB_ORA_SCRIPTS: $NB_ORA_SCRIPTS" >> $OUTF
   echo  >> $OUTF

   CMDFILE=${NB_ORA_SCRIPTS}/catalog_maint.rcv

   # ---------------------------------------------------------------------------
   # Call Recovery Manager to initiate catalog maintenance.
   # Note: Any environment variables needed at run time by RMAN or $CMDFILE
   #       must be set and exported within the switch user (su) command.
   # ---------------------------------------------------------------------------
{

   echo
   echo "% ${ORACLE_HOME}/bin/$RMAN cmdfile '$CMDFILE'"
   echo

    if [ \! -f $CMDFILE ] ; then
        echo "Missing $CMDFILE"
        ERROR=110
        sendit
        exit $ERROR
   fi

   me=`whoami`
   if [ "${me}" = "root" ] ; then
# echo "I'm root and would su to call ${ORACLE_HOME}/bin/$RMAN cmdfile $CMDFILE" >> $OUTF
      su - $ORACLE_USER -c "
      ORACLE_SID=$ORACLE_SID
      export ORACLE_SID
      ${ORACLE_HOME}/bin/$RMAN cmdfile $CMDFILE
      "
      RETURN_STATUS=$?
   else
      if [ "$me" != "$ORACLE_USER" ] ; then
         echo "Skipping $ORACLE_SID because my UID [$me] does not"
         echo "match what the $ENV had for this SID [$ORACLE_USER]"
         continue
      fi
# echo "I'm [$me] and calling ${ORACLE_HOME}/bin/$RMAN cmdfile $CMDFILE"
      ${ORACLE_HOME}/bin/$RMAN cmdfile $CMDFILE
      RETURN_STATUS=$?
   fi

   echo
   echo "Catalog maintenance started at $BDATE"
   echo "Catalog maintenance ended at   `date`"
   echo "---------End of $ORACLE_SID backup------------------"
   echo
   echo "exit $RETURN_STATUS"

   echo "Catalog maintenance of $ORACLE_SID completed with $RETURN_STATUS" >> $TMPFILE
   if [ $RETURN_STATUS -ne 0 ] ; then
      ERROR=$RETURN_STATUS
   fi

   sendit

} >> $OUTF

done #with loop for multiple SIDs...

exit $ERROR

Comments