Today's Birthday
Quote of the Day
This Day in History

Saturday, June 30, 2007

RMAN complete database backup script

This script does a full database backup including the archive logs.


#!/bin/ksh
. ~/.profile
##
## script name : rman_bkup.ksh
## Author : Ramakrishna Nemani
##
## Purpose :
## This script  takes database sid as a command line argument and does a full database
## backup including the archive logs. It backs up to the default device using automatic
## channel allocation. Make sure you have configured the default device and channels.
## You can find the configuration that I have used in a script called rman_config.ksh
##
## Other Features :
## This script emails the backup log to the email ids set up in the email configuration
## file email configuration file is just a text file containing the email ids to where
## the backup log will be emailed to.
## This script creates a log file on the datbase server where this script is hosted.
##
## It also creates and uses a lockfile to make sure no other rman process is running for
## the database sid that was supplied through the command line. Should this script fail
## for any reason, it does not remove the lockfile so that no rman process can run for this
## database until the error is fixed and the lock file is removed manually
##
##
## Note :
## You need to login as a unix user id that belongs to the dba group and run this script
## There are a bunch of modifiable parameters in this script that you should modify to
## reflect your environment.
## I have tested this script on only Oracle 10g on an IBM AIX platform. Use it at your own risk.
##
## This is an example or sample script
##

##
## Make sure ORACLE_SID is passed as an argument
##
if (( $# < 1 ))
then
        echo
        echo Error Missing Arguement, Please supply database sid (Oracle Sid)
        echo
        echo Usage: $0 ORACLE_SID
        echo
        exit
fi
export ORACLE_SID=$1
## ----------------------------------------------------------------------------------
##
## -------------------- Start of modifiable parameters ------------------------------
##
## Change the values for the following parameters to reflect your environment
##
##
LOG_DIR="/u01/oracle/admin/rman"
LOCKFILE="${LOG_DIR}/$ORACLE_SID.lock"
EMAIL_INFO_FILE="${LOG_DIR}/${ORACLE_SID}_email.cfg"
SENDER="mailto:rman@%60hostname"
CURR_DATE=`date +"%Y%m%d_%H%M%S"`
##
## using two seperate files instead of one for keeping the logs
## The LOGFILE will have log from just the latest run
## The LOGARCFILE will have the cumulative log
##
LOGFILE="${LOG_DIR}/rman_bkup.$ORACLE_SID.log"
LOGARCFILE="${LOG_DIR}/rman_bkup.$ORACLE_SID.log.arc"
##
## using two seperate files instead of one for keeping the errors
## The EMAILFILE will have errors if any from just the latest run
## The EMAILARCFILE will have the cumulative errors
##
EMAILFILE="${LOG_DIR}/rman_bkup.$ORACLE_SID.email"
EMAILARCFILE="${LOG_DIR}/rman_bkup.$ORACLE_SID.email.arc"
## -----------------------------------------------------------------------------------
##
## -------------------- End of modifiable parameters -----------------------------
##
## ------------------------------------------------------------------------------------

##
## Make sure no other rman process is running for this ORACLE_SID
##
echo --------------------------------------------------------- > $LOGFILE
date >> $LOGFILE
echo ---------------- rman_bkup.ksh started ------------------ >> $LOGFILE
while [ 1 ]
do

if [  -e  $LOCKFILE ]
then
##
## Some rman process is running. Check again after 3 minutes
##
 echo "$LOCKFILE exists. Will check again after 3 minutes" >> $LOGFILE
 echo  >> $LOGFILE
        sleep 180
else
##
## No rman process is running. Create a lock file
##
        echo Do not remove this file. This is a lock file for $ORACLE_SID > $LOCKFILE
        break
fi
done
echo $ORACLE_SID >> $LOGFILE
whoami >> $LOGFILE
which rman >> $LOGFILE
rman <<EOF   >> $LOGFILE
CONNECT TARGET /  ;
SHOW ALL ;
BACKUP DATABASE FILESPERSET 1 PLUS ARCHIVELOG DELETE INPUT;
DELETE NOPROMPT OBSOLETE;
REPORT SCHEMA ;
REPORT UNRECOVERABLE ;
EXIT ;
EOF
rmanstatus=$?
if (( $rmanstatus > 0 ))
then
 if [ ! -e  $EMAIL_INFO_FILE ]
 then
##
## The following line is executed if the Email config file is not set up.
## Replace emailid@defaultemail.com with the email id to where the email
## needs to be sent.
##
         echo "To:emailid@defaultemail.com" > $EMAIL_INFO_FILE
 fi
 cat $EMAIL_INFO_FILE > $EMAILFILE
 echo "Subject: The $0 script failed for $ORACLE_SID !!!" >> $EMAILFILE
        echo >> $EMAILFILE
        date >> $EMAILFILE
        echo  "rman error!!! ..." >> $EMAILFILE
        echo  "Not removed the lock file : $LOCKFILE " >> $EMAILFILE
        echo  "Please fix the error and remove the lock file" >> $EMAILFILE
        echo  "before re-running the script again!!!" >> $EMAILFILE
        echo >> $EMAILFILE
 grep -i 'RMAN-'  $LOGFILE >> $EMAILFILE
 grep -i 'ORA-'   $LOGFILE >> $EMAILFILE
 grep -i 'sbt'    $LOGFILE >> $EMAILFILE
 grep -i 'tsm'    $LOGFILE >> $EMAILFILE
 sendmail -F "$SENDER" -t < $EMAILFILE
 cat $EMAILFILE >> $EMAILARCFILE
        exit 12
fi
echo --------------------------------------------------------- >> $LOGFILE
date >> $LOGFILE
echo ---------------- rman_bkup.ksh completed ---------------- >> $LOGFILE
echo >> $LOGFILE
cat $EMAIL_INFO_FILE > $EMAILFILE
echo "Subject: The $0 script completed successfully for $ORACLE_SID !!!" >> $EMAILFILE
cat $LOGFILE >> $EMAILFILE
sendmail -F "$SENDER" -t < $EMAILFILE
cat $EMAILFILE >> $EMAILARCFILE
cat $LOGFILE >> $LOGARCFILE
##
## Remove lock file to allow other rman processs for this ORACLE_SID to run, if any
##
rm $LOCKFILE
## ---------------------------------------------------------------------------------
## End of  rman_bkup.ksh
## ---------------------------------------------------------------------------------

No comments:

Post a Comment