Friday, June 18, 2010

RMAN Backup Scripts

#!/bin/ksh
# Script to kick off RMAN LEVEL 0 and 1 (cumulative) backup

# Modify the following values as required
ORACLE_HOME=
export ORACLE_HOME
ORACLE_SID=
export ORACLE_SID
LOG_DIR=$HOME/
DMP_DIR=
export DMP_DIR
REC_WINDOW=14
export REC_WINDOW

# Settings below should not normally require changing
#
export PATH=$ORACLE_HOME/bin:$PATH
export DATESTAMP="`date +"%y%m%d"`"
export R_NOW=`date +'%Y_%m_%d-%H-%M-%S'`
export INCR_LEVEL=0

if [ `date +%a` = "Sat" ] #Change to any day you like to
then
export INCR_LEVEL=0
export BKP_LEVEL=FULL
else
export INCR_LEVEL="1 CUMULATIVE"
export BKP_LEVEL=CUMULATIVE
fi

export R_LOG=${LOG_DIR}/${ORACLE_SID}_RMAN_${BKP_LEVEL}_${R_NOW}.log

echo " "
echo "Beginning Backup at: `date`"

rman < SPOOL MSGLOG TO '${R_LOG}';
CONNECT TARGET /;
SHOW ALL;
LIST INCARNATION;
BACKUP INCREMENTAL LEVEL ${INCR_LEVEL}
AS COMPRESSED BACKUPSET DEVICE TYPE DISK
DATABASE TAG 'DB_${DATESTAMP}_${BKP_LEVEL}' FORMAT '${DMP_DIR}/%d_%T_DB_%U'
PLUS ARCHIVELOG TAG 'ARCH_${DATESTAMP}' FORMAT '${DMP_DIR}/%d_%T_ARCH_%U';
LIST BACKUP SUMMARY;
EXIT;
EOF
export BKP_STATUS=$?
echo BKP_STATUS: ${BKP_STATUS}
if [ $BKP_STATUS -eq "0" ]
then
echo " "
echo "Deleting old archivelogs and backups outside recovery window..."
echo " "
rman < SPOOL MSGLOG TO '${R_LOG}' APPEND;
CONNECT TARGET /;
DELETE NOPROMPT ARCHIVELOG ALL BACKED UP 1 TIMES TO DISK;
REPORT OBSOLETE RECOVERY WINDOW OF ${REC_WINDOW} DAYS;
DELETE NOPROMPT OBSOLETE RECOVERY WINDOW OF ${REC_WINDOW} DAYS;
LIST BACKUP SUMMARY;
EXIT;
EOF1
else
echo " "
echo "Backup does not appear to be successful. Not deleting archivelogs or backups..."
fi

# Backup Controlfile irrespective of backup status
echo " "
echo "Backing up current controlfile..."
rman < SPOOL LOG TO '${R_LOG}' APPEND;
CONNECT TARGET /;
run {
SET CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F';
BACKUP AS COPY CURRENT CONTROLFILE FORMAT '${DMP_DIR}/%d_%T_CTLFILE_%U';
}
LIST BACKUP SUMMARY;
EXIT;
EOF2

r_err_count=0;
r_err_count=`cat $R_LOG | egrep "RMAN-|ORA-" | wc -l`
if [ $r_err_count -ne 0 ]
then
echo "Errors reported. Sending email..."
mailx -s "Errors in todays "`hostname`:${ORACLE_SID}" RMAN log file" db_admin@xxxxxx < $R_LOG
fi
echo " "
echo "Backup Script completed: `date`"

No comments: