RMAN hot backup script

#! /bin/sh

#———————————–
# Available Options For Configurations Below
#———————————–
# ORACLE RELEASE options: [9i,10g]
# BACKUP MEDIA options: [DISK,SBT]
# BACKUP TYPE options:
# For 9i [FULL_UNCOMP]
# For 10g [FULL_UNCOMP,FULL_COMP,INC_MERGE]
# BACKUP_PARALLEL options: [1-16]
#

#———————————–
# Start of RMAN Configurable Section
#———————————–
export ORACLE_RELEASE=10g
export BACKUP_MEDIA=DISK
export BACKUP_TYPE=INC_MERGE
export BACKUP_RETENTION=1
export BACKUP_PARALLEL=4
export DATA_MAXPIECESIZE=5G
export ARCH_MAXPIECESIZE=3G
export P_DBAEMAIL=”abc@abc.com”
export DBAEMAIL=”abc@abc.com”
#export DBAPAGER=”abc@abc.com”
export TIMESTAMP=`date +%T-%m-%d-%Y`
#———————————–
# PATHS
export ORACLE_SID=ABC
export ORACLE_BASE=/oracle
export ORACLE_HOME=/oracle/BID/102_64
export BACKUP_DIR=/dbbackups/BID/backups
export BK_ARCHIVE_DIR=/dbbackups/BID/backups
export BK_CF_DIR=/dbbackups/BID/backups
export BK_SPFILE_DIR=/dbbackups/BID/backups
export LOG_DIR=/dbbackups/BID/logs
export LOG=${LOG_DIR}
LOG=${LOG}/${ORACLE_SID}_${BACKUP_TYPE}_${TIMESTAMP}.log
export TMPLOG=${LOG_DIR}/tmplog.$$

#———————————–
# CATALOG
#export CATALOG_CONN=${ORACLE_SID}/${ORACLE_SID}@catalog

#———————————–
# GENERIC ENVIRONMENTAL
export TMPDIR=/tmp
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib:lib
export LIBPATH=$ORACLE_HOME/lib:/usr/lib:lib
export SHLIB_PATH=$ORACLE_HOME/lib:/usr/lib:lib

#———————————–
# LOCK FILE
export LOCKFILE=${LOG_DIR}/${ORACLE_SID}_rman.lock

#———————————
# End of RMAN Configurable Section
#———————————

echo `date` “Starting $BACKUP_TYPE Backup of $ORACLE_SID to $BACKUP_MEDIA” > $LOG

if [ -f $LOCKFILE ]; then
echo `date` “The script is currently running. Exiting …” >> $LOG
else
echo “DO NOT delete this file. Used for RMAN locking” > $LOCKFILE
#***********************************************************************
if [ $ORACLE_RELEASE = 9i ]; then
#===================================================================
if [ $BACKUP_TYPE = FULL_UNCOMP ]; then

$ORACLE_HOME/bin/rman target / nocatalog log=$TMPLOG <> $LOG
fi
#***********************************************************************
elif [ $ORACLE_RELEASE = 10g ]; then
#===================================================================
if [ $BACKUP_TYPE = FULL_UNCOMP ]; then

$ORACLE_HOME/bin/rman target / nocatalog log=$TMPLOG <<EOF
sql "alter session set optimizer_mode=RULE";
run
{
CONFIGURE RETENTION POLICY TO REDUNDANCY ${BACKUP_RETENTION};
CONFIGURE SNAPSHOT CONTROLFILE NAME TO '${ORACLE_HOME}/dbs/SNAPSHOT_${ORACLE_SID}_CTL';
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '${BACKUP_DIR}/ctlbkp_${ORACLE_SID}_%F.CTL';
CONFIGURE DEVICE TYPE $BACKUP_MEDIA BACKUP TYPE TO BACKUPSET PARALLELISM ${BACKUP_PARALLEL};
CONFIGURE CHANNEL 1 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 2 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 3 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 4 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 5 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 6 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 7 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 8 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 9 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 10 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 11 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 12 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 13 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 14 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 15 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 16 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
crosscheck backup;
crosscheck copy;
backup
tag = '${ORACLE_SID}_$BACKUP_TYPE'
database;
crosscheck archivelog all;
allocate channel a1 type disk maxpiecesize ${ARCH_MAXPIECESIZE};
allocate channel a2 type disk maxpiecesize ${ARCH_MAXPIECESIZE};
allocate channel a3 type disk maxpiecesize ${ARCH_MAXPIECESIZE};
allocate channel a4 type disk maxpiecesize ${ARCH_MAXPIECESIZE};
BACKUP ARCHIVELOG ALL format '${BK_ARCHIVE_DIR}/arch_%d_%s_%T_%U_DB' tag '${ORACLE_SID}_ARCH_BKUP' DELETE INPUT;
release channel a1;
release channel a2;
release channel a3;
release channel a4;
BACKUP CURRENT CONTROLFILE FORMAT '${BK_CF_DIR}/cf_%d_%s_%T_%U_DB' tag "${ORACLE_SID}_CTRL_FILE_BKUP";
BACKUP SPFILE FORMAT '${BK_SPFILE_DIR}/spfile_%d_%s_%T_%U_DB' tag "${ORACLE_SID}_SPFILE_BKUP";
sql "create pfile=''${BK_SPFILE_DIR}/pfile_${ORACLE_SID}_${TIMESTAMP}_DB.ora'' from spfile";
crosscheck backup;
delete noprompt obsolete;
delete noprompt expired backup;
report unrecoverable;
}
exit
EOF
#===================================================================
elif [ $BACKUP_TYPE = FULL_COMP ]; then

$ORACLE_HOME/bin/rman target / nocatalog log=$TMPLOG <<EOF
sql "alter session set optimizer_mode=RULE";
run
{
CONFIGURE RETENTION POLICY TO REDUNDANCY ${BACKUP_RETENTION};
CONFIGURE SNAPSHOT CONTROLFILE NAME TO '${ORACLE_HOME}/dbs/SNAPSHOT_${ORACLE_SID}_CTL';
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '${BACKUP_DIR}/ctlbkp_${ORACLE_SID}_%F.CTL';
CONFIGURE DEVICE TYPE $BACKUP_MEDIA BACKUP TYPE TO COMPRESSED BACKUPSET PARALLELISM ${BACKUP_PARALLEL};
CONFIGURE CHANNEL 1 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 2 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 3 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 4 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 5 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 6 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 7 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 8 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 9 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 10 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 11 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 12 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 13 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 14 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 15 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
CONFIGURE CHANNEL 16 DEVICE TYPE DISK FORMAT '${BACKUP_DIR}/data_%d_%s_%T_%U_DB' maxpiecesize ${DATA_MAXPIECESIZE};
crosscheck backup;
crosscheck copy;
backup
tag = '${ORACLE_SID}_$BACKUP_TYPE'
database;
crosscheck archivelog all;
allocate channel a1 type disk maxpiecesize ${ARCH_MAXPIECESIZE};
allocate channel a2 type disk maxpiecesize ${ARCH_MAXPIECESIZE};
allocate channel a3 type disk maxpiecesize ${ARCH_MAXPIECESIZE};
allocate channel a4 type disk maxpiecesize ${ARCH_MAXPIECESIZE};
BACKUP ARCHIVELOG ALL format '${BK_ARCHIVE_DIR}/arch_%d_%s_%T_%U_DB' tag '${ORACLE_SID}_ARCH_BKUP' DELETE INPUT;
release channel a1;
release channel a2;
release channel a3;
release channel a4;
BACKUP CURRENT CONTROLFILE FORMAT '${BK_CF_DIR}/cf_%d_%s_%T_%U_DB' tag "${ORACLE_SID}_CTRL_FILE_BKUP";
BACKUP SPFILE FORMAT '${BK_SPFILE_DIR}/spfile_%d_%s_%T_%U_DB' tag "${ORACLE_SID}_SPFILE_BKUP";
sql "create pfile=''${BK_SPFILE_DIR}/pfile_${ORACLE_SID}_${TIMESTAMP}_DB.ora'' from spfile";
crosscheck backup;
delete noprompt obsolete;
delete noprompt expired backup;
report unrecoverable;
}
exit
EOF
#===================================================================
elif [ $BACKUP_TYPE = INC_MERGE ]; then

$ORACLE_HOME/bin/rman target / nocatalog log=$TMPLOG <> $LOG
fi
#***********************************************************************
else
echo `date` “Oracle $ORACLE_RELEASE is not currently supported by this script.” >> $LOG
fi
#***********************************************************************
RC=$?
cat $TMPLOG >> $LOG
rm $LOCKFILE
echo `date` “Lock file removed.” >> $LOG
if [ $RC -ne “0” ]; then
echo `date` “RMAN $BACKUP_TYPE Of $ORACLE_SID To $BACKUP_MEDIA Failed.” >> $LOG
mailx -s “RMAN $BACKUP_TYPE Of $ORACLE_SID To $BACKUP_MEDIA Failed.” \
$DBAEMAIL,$DBAPAGER > $LOG
mailx -s “RMAN $BACKUP_TYPE Of $ORACLE_SID To $BACKUP_MEDIA Completed Successfully.” \
$P_DBAEMAIL < $LOG
find ${BK_SPFILE_DIR} -type f -name 'pfile_${ORACLE_SID}_*.ora' -mtime +${BACKUP_RETENTION} -exec /bin/rm -f {} \;
find ${LOG_DIR} -type f -name '*.log' -mtime +6 -exec /bin/rm -f {} \;
fi
rm $TMPLOG
fi

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

%d bloggers like this: