Deploy Recovery Manager (RMAN) with this handy script and command file

Providing You With Handy Scripts
Most of our senior consultants have been working with Oracle technology for over ten years. As you can imagine, they’ve developed quite a few tools and scripts that they carry with them from project to project. Below, you’ll find a sampling of our handy database administration scripts that are yours for the taking. Feel free to download and use them.

Deploy Recovery Manager (RMAN) with this handy script and command file
RMAN is the obvious choice for performing hot backups and managing the flash recovery area (FRA) but the plethora of options go well beyond a basic “backup database” command.  Rather than starting from scratch with scripting, adding options one at a time, and going through several cycles of needless debugging, why not use our Linux bash shell script and accompanying RMAN command file complete with error handling and comments.  The RMAN command file works like a template, making it easy to use the options you want and remove the one you don’t.  The embedded comments help to clarify your backup strategy and serve as a training tool if you’re not totally comfortable with RMAN yet.

#!/bin/bash
#
# rman_backup.sh
# ==============
#
# This script is provided by Database Specialists
# for individual use and not for sale.
# We do not warrant the script in any way
# and will not be responsible for any loss arising out of its use.
#
# This script submits an RMAN command file in order to backup an Oracle database.
#
# Before you can use this script, you must do the following:
# 1. Make sure the database to be backed up is currently mounted or open and running
# in ARCHIVELOG mode.
# 2. Review the “Configuration settings” section of this script to customize
# as needed.
# 3. Configure backup retention and backup destination if FRA isn’t used, for example:
# CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;
# CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT ‘/u01/app/oracle/admin/backup/%U’;
# 4. Configure archivelog deletion policy if desirable. The policy is used by Oracle to
# delete logs which have satisfied the backup retention policy as well. When an FRA is
# used then the deletion happens automatically. Otherwise you have to delete the logs
# within RMAN via DELETE INPUT, DELETE ARCHIVELOG, or DELETE OBSOLETE. The following
# example is valid for 11g or better.
# CONFIGURE ARCHIVELOG DELETION POLICY TO BACKED UP 1 TIMES TO DISK SHIPPED TO ALL STANDBY;
# 5. Add the proper database and recovery catalog connection strings to the RMAN command file
# referenced with the -c command-line argument.
#
# Usage: rman_backup.sh -i instance -c command_file [-l log_directory] [-n notify_list]
# instance is the ORACLE_SID of the database to back up.
# command_file is the name of the RMAN command file; assumption is that the file is in the
# same directory as this script file
# log_directory is where the log file should be written. (If not
# provided then the log file will be written to /tmp.)
# notify_list is a list of comma-separated email addresses where email
# should be sent if the backup fails for any reason.
#

#
# Configuration settings.
#
export MAIL_PROGRAM=”mailx”
TEMPDIR=”/tmp”
SCRIPTDIR=`dirname “$0″`

#
# Parse and validate the command line arguments.
#
SID=””
CMD_FILE=””
LOG_DIR=${TEMPDIR}
NOTIFY=””
USAGE=”Usage: `basename $0` -i instance -c command_file [-l log_directory] [-n notify_list]”

while getopts :i:c:C:l:n: opt
do
case “$opt” in
“i”) SID=”$OPTARG” ;;
“c”) CMD_FILE=”$OPTARG”
if [ ! -r “${SCRIPTDIR}/${CMD_FILE}” ]
then
echo “RMAN command file does not exist or is not readable by you” 1>&2
exit
fi ;;
“n”) NOTIFY=”$OPTARG” ;;
“l”) LOG_DIR=”$OPTARG”
if [ ! -d “$LOG_DIR” -o ! -w “$LOG_DIR” ]
then
echo “Log directory must be a directory writable by you” 1>&2
exit 1
fi ;;
“:”|”?”) echo “$USAGE” 1>&2 ; exit 1 ;;
esac
done

let i=$#+1
if [ “$i” != “$OPTIND” ]
then
echo “$USAGE” 1>&2
exit 1
fi

if [ -z “${SID}” -o -z “${SCRIPTDIR}/${CMD_FILE}” ]
then
echo “$USAGE” 1>&2
exit 1
fi

#
# Set local variables based on command line arguments
#
OUTFILE=”${LOG_DIR}/${SID}_${CMD_FILE}_`date +%Y%m%d_%H%M%S`.log”

#
# Set up the environment.
#
export ORACLE_SID=”$SID”
export ORAENV_ASK=”NO”
export PATH=”$PATH:/usr/local/bin:/usr/sbin”
. /usr/local/bin/oraenv
export NLS_DATE_FORMAT=”dd-mon-yy hh24:mi:ss”

#
# Check to make sure that there is not another backup employing this same command file
# that is already running.
#
export SCRIPTLOCKFILE=${SCRIPTDIR}/${SID}_${CMD_FILE}.scriptlock
if [ -f $SCRIPTLOCKFILE ]
then
OLDCOPY=`ls -l $SCRIPTLOCKFILE`
echo “Found lockfile: $OLDCOPY” > $OUTFILE
OLDPS=`ps -ef|egrep $0|egrep -v $$|egrep -v egrep`
echo “Searching ps for $0 (ignoring $$ because that is me) ” >> $OUTFILE
echo “Found: $OLDPS” >> $OUTFILE
echo “If you see a ps, it is still running. If not the job got jammed, remove the lockfile.” >> $OUTFILE
exit
else
touch $SCRIPTLOCKFILE
fi

#
# Invoke RMAN with connection strings hard-coded in the command file.
#
$ORACLE_HOME/bin/rman log ${OUTFILE} cmdfile ${SCRIPTDIR}/${CMD_FILE}

#
# If there is an error returned from the rman executable, send an email to those recipients
# designated on the command line. Note that no email is sent upon successful completion.
# Therefore some means of monitoring backups such as Database Rx should be employed so that
# someone gets notified if this script is not being executed at all.
#
RETCODE=”$?”
if [ “$RETCODE” != “0” ]
then
${MAIL_PROGRAM} -s “Hot backup of $ORACLE_SID database failed” “${NOTIFY}” < “${OUTFILE}”
fi

rm $SCRIPTLOCKFILE
exit 0

Note: * These scripts are provided by Database Specialists for individual use and not for sale. We do not warrant the scripts in any way and will not be responsible for any loss arising out of their use. By downloading or using these scripts you are agreeing to these terms and conditions.

bs-xprts

Contact us – Bright*Star Solutions Pvt. Ltd / http://www.bslion.in / sales@bslion.in / +91-9823.00.5326

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s