#!/bin/ksh
#
# dbexp.sh
# ========
#
# This script is provided by Database Specialists, Inc. 
# (http://www.dbspecialists.com) for individual use and not for sale. 
# Database Specialists, Inc. does not warrant the script in any way 
# and will not be responsible for any loss arising out of its use.
#
# Your feedback is welcome! Please send your comments about this script
# to scriptfeedback@dbspecialists.com
#
# This script performs a full database export of the specified database
# and sends an email message to the specified user if anything goes wrong.
# The export is written through a named pipe directly into a compressed file.
#
# This script is suitable for running via cron. As such, it generates no
# standard output. All output is written to a log. You will need to edit
# this script before using in order to invoke your password lookup facility
# or otherwise determine the correct Oracle password.
#
# This script has been used on Solaris 2.5.1 and 2.6 against Oracle 7.3.4 
# and 8.0.5 databases.
#
# Usage: dbexp.sh -d sid -t type -n notify_list [-c]
#        sid is the ORACLE_SID of the database to export. 
#        type is either FULL or STRUCT. FULL exports the entire database,
#            while STRUCT exports all of the DDL to recreate the database, but
#            not the actual data (rows in the tables).
#        notify_list is a list of comma-separated email addresses where email
#            should be sent if the export fails for any reason.
#        -c may optionally be specified to export in consistent mode.
#
# Version 03-26-1999
#

#
# The quit_dbexp function sends a failure email message, cleans up, and exits.
#

function quit_dbexp
{
  echo $1 >> $LOGFILE
  mailx -s"Export of $ORACLE_SID database failed" $NOTIFY < $LOGFILE
  rm -f $TMPLOG $PIPEFILE $CERRFILE
  exit 1
}

#
# Parse the command line arguments.
#

USAGE="Usage: `basename $0` -d sid -t type -n notify_list [-c]"
SID=""
EXP_TYPE=""
NOTIFY=""
CONSISTENT=""

while getopts :d:t:n:c opt
do
  case "$opt" in
    "d") SID="$OPTARG" ;;
    "t") typeset -u EXP_TYPE="$OPTARG"
         case "$EXP_TYPE" in
           "FULL")   EXP_ROWS="yes" ; FILENAME="full" ;;
           "STRUCT") EXP_ROWS="no" ; FILENAME="strct" ;;
           *)        echo "Export type must be FULL or STRUCT" 1>&2
                     exit 1 ;;
         esac ;;
    "n") NOTIFY="$OPTARG" ;;
    "c") CONSISTENT="CONSISTENT=Y" ;;
    ":"|"?") echo "$USAGE" 1>&2 ; exit 1 ;;
  esac
done
 
let i=$#+1
if [ "$i" != "$OPTIND" -o -z "$SID" -o -z "$EXP_TYPE" -o -z "$NOTIFY" ]
then
  echo "$USAGE" 1>&2
  exit
fi

#
# Set up the Oracle environment.
#

export ORACLE_SID="$SID"
export ORAENV_ASK="NO"
export PATH="$PATH:/usr/local/bin"
. /usr/local/bin/oraenv
# Note: If oraenv does not set $ORACLE_BASE, you must set it here.

#
# Set up the general environment.
#

EXPDIR="$ORACLE_BASE/admin/$ORACLE_SID/exp"
LOGFILE="$EXPDIR/exp_${ORACLE_SID}_`date +%m%d%y`_${FILENAME}.log"
TMPLOG="$EXPDIR/exp_${ORACLE_SID}_`date +%m%d%y`_${FILENAME}.tmp"
EXPFILE="$EXPDIR/exp_${ORACLE_SID}_`date +%m%d%y`_${FILENAME}.dmp.Z"
PIPEFILE="$EXPDIR/exp_${ORACLE_SID}_`date +%m%d%y`_${FILENAME}.pipe"
CERRFILE="/tmp/exp_${ORACLE_SID}_`date +%m%d%y`_${FILENAME}.cerr"

cat <<EOF > $LOGFILE
Beginning database export at `date`
  Server is `uname -n`
  Database is $ORACLE_SID
  Export type is $EXP_TYPE
  Export file is $EXPFILE

EOF

#
# Purge full exports older than 3 days.
#

(cd $EXPDIR; find . -name exp_\*_full.dmp.Z -type f -mtime +2 -exec rm -f {} \;)

#
# Get the necessary Oracle password.
#

ORAPWD="`<enter your password lookup service here> $ORACLE_SID system 2>&1"
# If you want to hardcode the Oracle password, change the above line to read:
# ORAPWD="<enter your password here>"

if [ "$?" != "0" ]
then
  echo $ORAPWD >> $LOGFILE
  quit_dbexp "Unable to get Oracle password for system schema"
fi
  
#
# Create a named pipe and begin the background compress.
#

/etc/mknod $PIPEFILE p >> $LOGFILE 2>&1
[ "$?" != "0" ] && quit_dbexp "Error occurred while creating named pipe"

compress < $PIPEFILE > $EXPFILE 2> $CERRFILE &

#
# Perform the actual export.
#

echo $ORAPWD | exp system full=y direct=y rows=$EXP_ROWS $CONSISTENT \
                   file=$PIPEFILE log=$TMPLOG >> $LOGFILE 2>&1

RETCODE="$?"

echo "Export completed at `date` with status code $RETCODE\n" >> $LOGFILE

if [ "$RETCODE" != "0" ] 
then
  quit_dbexp " "
fi

#
# Make sure that the export log and background compress do not indicate 
# any errors.
#

grep "EXP\-" $TMPLOG > /dev/null 2> /dev/null
RETCODE="$?"
[ "$RETCODE" = "0" ] && echo "Export log contains export errors" >> $LOGFILE
grep "ORA\-" $TMPLOG > /dev/null 2> /dev/null
RETCODE2="$?"
[ "$RETCODE2" = "0" ] && echo "Export log contains Oracle errors" >> $LOGFILE
[ "$RETCODE" = "0" -o "$RETCODE2" = "0" ] && quit_dbexp " "

if [ -s "$CERRFILE" ]
then
  cat $CERRFILE >> $LOGFILE
  quit_dbexp "Error detected in background compress"
fi

#
# The export was successful. Now clean up and exit.
#

rm -f $TMPLOG $PIPEFILE $CERRFILE
exit 0

