Thursday, 21 January 2016

RMAN Database Backup Script.

00 18 * * 6  nohup /home/oracle/scripts/backupscripts/backup_script_v5.ksh FSPRD 0 8 "/backups" "Emailid@domain.com" >> /tmp/FSPRD_nohup_rmanrun.log 1>&2 &

#!/bin/ksh
set -xv
export DB_NAME=$1;
export BKP_LEVEL=$2;
export PARALLEL=$3;
export FOLDER_PATH=$4;
export DBA_EMAIL="$5";
export PROCESS_NUMBER=$$;
mkdir -p "${FOLDER_PATH}/rman/${DB_NAME}/BACKUPS/LOGS/";
ORACLE_HOSTNAME=xyz; export ORACLE_HOSTNAME
ORACLE_UNQNAME=$DB_NAME; export ORACLE_UNQNAME
ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1; export ORACLE_HOME
ORACLE_SID=$DB_NAME; export ORACLE_SID
PATH=/usr/sbin:$PATH; export PATH
PATH=$ORACLE_HOME/bin:$PATH; export PATH
LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH
CLASSPATH=$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib; export CLASSPATH
LOG_DATE=`date +%d%m%Y`
LOGFILE="${FOLDER_PATH}/rman/$DB_NAME/BACKUPS/LOGS/${ORACLE_UNQNAME}_${PROCESS_NUMBER}_LEVEL${BKP_LEVEL}_`date +%d%m%Y`.log"
TRACEFILE="${FOLDER_PATH}/rman/$DB_NAME/BACKUPS/LOGS/${ORACLE_UNQNAME}_${PROCESS_NUMBER}_LEVEL${BKP_LEVEL}_TRACE_`date +%d%m%Y`.trc"
TIMETRACKER="${FOLDER_PATH}/rman/$DB_NAME/BACKUPS/LOGS/${ORACLE_UNQNAME}_${PROCESS_NUMBER}_TIME_TRACK_L${BKP_LEVEL}_`date +%d%m%Y`.log"

echo -e "  ${ORACLE_UNQNAME} LEVEL ${BKP_LEVEL} BACKUP START AND END TIME DETAILS" > $TIMETRACKER
echo  "====================================================" >> $TIMETRACKER
date >>$TIMETRACKER
rman target / catalog RMANCAT/<PASSWORD>@RMANDB USING $DB_NAME $BKP_LEVEL $PARALLEL "'$FOLDER_PATH'" trace=$TRACEFILE log=$LOGFILE <<EOF
run {
CONFIGURE SNAPSHOT CONTROLFILE NAME TO '&4/rman/&1/BACKUPS/&1_snapshot_ctrl_file.ctl';
crosscheck backup;
crosscheck archivelog all;
CONFIGURE DEVICE TYPE DISK PARALLELISM &3 BACKUP TYPE TO BACKUPSET;
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT   '&4/rman/&1/BACKUPS/L&2_%T_%d_%s_%p_%U';
backup incremental level &2 device type disk as compressed backupset database tag = 'weekly0';
sql 'alter system archive log current';
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT   '&4/rman/&1/BACKUPS/&2_ARCH_%T_%d_%s_%p_%U';
backup device type disk as compressed backupset archivelog all delete input tag L&2_ARCHIVE_LOGS;
delete noprompt obsolete device type disk;
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT   '&4/rman/&1/BACKUPS/&2_CNTL_%T_%d_%s_%p_%U';
backup current controlfile tag L&2_CNT_FILE_BKP;
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT   '&4/rman/&1/BACKUPS/&2_SPFILE_%T_%d_%s_%p_%U';
backup spfile tag L&2_SP_FILE_BKP;
}
EOF

date >> $TIMETRACKER
echo -e "\n====================================================\n\n" >> $TIMETRACKER
echo -e "\n  HISTORICAL RMAN BACKUP DETAILS " >> $TIMETRACKER
echo -e "====================================================\n" >> $TIMETRACKER
sqlplus -s \"/ as sysdba\"  @/home/oracle/scripts/backupscripts/rman_backup_info.sql >> $TIMETRACKER
echo -e "\n====================================================\n\n" >> $TIMETRACKER

echo -e "  ORA- ERROR DETAILS If ANY? " >> $TIMETRACKER
echo -e "====================================================\n" >> $TIMETRACKER
cat $TRACEFILE | grep ORA-  >> $TIMETRACKER
echo -e "====================================================\n\n" >> $TIMETRACKER

echo -e "  FEW LINES of LOG FILE " >> $TIMETRACKER
echo -e "====================================================\n" >> $TIMETRACKER
tail -20 $TRACEFILE >> $TIMETRACKER
echo -e "====================================================\n\n" >> $TIMETRACKER

error_count=$(cat $TRACEFILE | grep ORA- | wc -l);
if  [ $error_count -gt 0 ]; then
        mailx -s "RMAN Level ${BKP_LEVEL} BACKUP FAILED FOR $DB_NAME ${LOG_DATE}" -r noreply@domain.com  ${DBA_EMAIL} < $TIMETRACKER
else
        mailx -s "RMAN Level ${BKP_LEVEL} BACKUP SUCCESS FOR $DB_NAME ${LOG_DATE}" -r noreply@domain.com  ${DBA_EMAIL} < $TIMETRACKER
        cp /backups/rman/FSPRD/BACKUPS/*CNTL* /backups/rman/FSPRD/BACKUPS/CNTLFILESAVE/
        find /backups/rman/FSPRD/BACKUPS/CNTLFILESAVE/ -name '*CNTL*' -type f -mtime +30 -exec rm {} \;
        nohup sh -xv /home/oracle/scripts/syncdrincr.ksh 1>&2 > /tmp/trace_dr_backups.log &
fi

exit