#!/bin/ksh

# Full database backup to a Tivoli tape drive system.
# Script checks that other rman processes are not running before starting
# RMAN and sends email of the backup log to list of recipients upon completion
# full_database_backup.sh

#  Set environment variables

export ORACLE_SID=ORADB1
export ORACLE_HOME=/u01/app/oracle/product/10.2.0/dbhome
export PATH=$PATH:$ORACLE_HOME/bin

host=$(hostname)
filext=$(date +%m%d%y'_'%H%M)


# check that no other rman processes are running. The archivelog backup script might be running.

rmancheck=$(ps -ef | grep "rman" | grep -v grep | awk '{print $8}')
pcheck=$(echo $rmancheck | awk '{print $1}')

if [[ $pcheck == rman ]]; then
count=0;
while (( count < 20)) && [[ $pcheck == rman ]]; do
         sleep 60
         rmancheck=$(ps -ef | grep "rman" | grep -v grep | awk '{print $8}')
         pcheck= $(echo $rmancheck | awk '{print $1}')
         $((count += 1))
     done
         rmancheck=$(ps -ef | grep "rman" | grep -v grep | awk '{print $8}')
	 pcheck=$(echo $rmancheck | awk '{print $1}')
     if [[ $pcheck != rman ]] then
	   :
     else
	 (echo 'backup conflict') | mail -s "$ORACLE_SID Full backup conflict with another rman backup" \
             $(cat /u01/app/oracle/admin/$ORACLE_SID/scripts/oracle/alert.lst)
fi


# start rman backup. Remove quotes from "<"
/u01/app/oracle/product/10.2.0/dbhome/bin/rman <"<"EOS

spool log to '/u01/app/oracle/admin/$ORACLE_SID/adminlogs/full_tape_backup_$fileid.log';


connect target sys/password@ORADB1
connect catalog rmanadmin/password@rcat

configure controlfile autobackup on;
configure maxsetsize to 250G;
CONFIGURE CHANNEL DEVICE TYPE 'SBT_TAPE' MAXOPENFILES=1 PARMS='ENV=(TDPO_OPTFILE=/path/to/tivoli/parameter/file/tdpo.opt)';
show all;

run
{crosscheck archivelog all;}

run
{delete expired copy of archivelog all;}
yes

run{
backup incremental level 0 format 'inc0_%d_%T_%U.bus'
database plus archivelog;}

EOF

# Email output file as a Word document attachment

fileloc='/usr/local/scripts/oracle/logs/full_tape_backup_'$fileid'.log';

filename='full_tape_backup__'$fileid'.log.doc'

$(chmod 755 $fileloc)

 (cat /u01/app/oracle/admin/$ORACLE_SID/backup_scripts/full_tape_mail.txt; uuencode $fileloc $filename) \
 |mail -s "$ORACLE_SID Full Database Backup Completed" \
 $(cat /u01/app/oracle/admin/$ORACLE_SID/backup_scripts/mail.lst)


exit