26 February 2009

Always working on backups....

Today I tried to improve a backup. On a host with a large number of databases running, which do change now and then, you would like that your backup more or less manages itself. So it would have to pick up all running databases, and backup these.

The backup script is stored in a RMAN catalog. The script picks up all databases on a (linux) system and selects them on the running pmon process:

ps -ef | grep pmon | grep -v 'grep pmon' | awk '{print "backupdb " substr($8,10,8) ";"}'

With the awk command, you extract the instance name from the grep output. The result of the grep is put into a tmpwrk.lst file, and looks likes this:

backupdb TEST01;
backupdb PROD01;
backupdb PROD02;

etc.

The total script I use is the following:

#!/bin/sh
#
# Script for backup all databases on the server
#
export ORACLE_BASE=/opt/oracle
export ORACLE_HOME=$ORACLE_BASE/product/10.2.0
export PATH=$ORACLE_HOME/bin:/usr/local/bin:/usr/local/sbin:$PATH

export WRKDIR=/home/oracle/scripts
export LOGDIR=/backup/$(hostname)
export JOBLOG=$LOGDIR/backup_$(hostname).log
echo "Starting backup $(hostname) databases at $(date)" > $JOBLOG

backupdb()
{
if [ -z $1 ]
then
echo "No parameters passed to function."
return 0
else
export ORACLE_SID=$1
rman target / catalog rman/rmanpswd@RMANCAT CMDFILE=$WRKDIR/rman_backup.rcv LOG=$LOGDIR/$ORACLE_SID.backup.log;
FOUTEN=$(grep 'ERROR MESSAGE' $LOGDIR/$ORACLE_SID.backup.log | wc -l);
if [ $FOUTEN -ne 0 ]
then
cat LOG=$LOGDIR/$ORACLE_SID.backup.log | nail -s "BACKUP FAILED $ORACLE_SID" mailme@myisp.com;
echo "$(date) : backup $ORACLE_SID FAILED!!!" >> $JOBLOG
else
echo "$(date) : backup $ORACLE_SID completed" >> $JOBLOG
fi
fi
}

#-----------------------------------------
# MAIN
#-----------------------------------------

#--
# Make list of all active databases
# and create tmpbck.lst file
#---------
ps -ef | grep pmon | grep -v 'grep pmon' | awk '{print "backupdb " substr($8,10,8) ";"}' > $WRKDIR/tmpbck.lst

#--
# run database cold backup for this list
#---------
source $WRKDIR/tmpbck.lst

echo "Backup $(hostname) finished at $(date)" >> $JOBLOG

exit