#!/bin/sh
#
# This is dbexport -- script to create a full export of
# a database, the script exports normally, and then compresses the
# export file 
#
# script sets all needed environment parameters
#
# script removes export files older than XX days
#
# this is version 3.0 -- BRH 12172002
#

ORACLE_SID=
export ORACLE_SID

ORACLE_OWNER=oracle
export $ORACLE_OWNER

ORACLE_HOME=
export ORACLE_HOME

NOTIFY_LIST='<email address>'
export NOTIFY_LIST

LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib:/usr/openwin/lib
export LD_LIBRARY_PATH

PATH=/bin:/usr/bin:/usr/ccs/bin:/usr/sbin:${ORACLE_HOME}/bin:.
export PATH

NLS_LANG=AMERICAN_AMERICA.US7ASCII, .WE8ISO8859P1, .UTF8
export NLS_LANG

CSHRC_FILE=~/.cshrc_<SID>
export CSHRC_FILE

LOGDIR=$ORACLE_HOME/admin/dbexport_log
export LOGDIR

RETENTION_TIME_DAYS=<number of days>
export RETENTION_TIME_DAYS

#
# EXPORT_LOG_TMP isn't needed for this script alone, but, since the
# export log file is written to disk while the export is made, if
# multiple export jobs are running, and they all use the same disk filename,
# one script may remove the export log file for the other script. We use
# the filename EXPORT_LOG_TMP so that on disk each script will have its own
# export log file to prevent any other scripts removing it before the export
# logfile is cat-ed to the overall export script log
#

EXPORT_LOG_TMP=${LOGDIR}/EXPORT_LOG_TMP_${ORACLE_SID}_`date +\%m\%d\%Y_\%T`.tmp
export EXPORT_LOG_TMP

EXPORT_PIPE=EXPORT_PIPE_${ORACLE_SID}_`date +\%m\%d\%Y_\%T`
export EXPORT_PIPE

EXPORTDIR=$ORACLE_HOME/admin/dbexport_exp
export EXPORTDIR

DBEXPORT=${EXPORTDIR}/DBEXPORT_${ORACLE_SID}_`date +\%m\%d\%Y_\%T`.exp
export DBEXPORT

DBEXPORT_COMPRESSED=${DBEXPORT}.Z
export DBEXPORT_COMPRESSED

DBEXPORTLOG=${LOGDIR}/DBEXPORT_${ORACLE_SID}_`date +\%m\%d\%Y_\%T`.out
export DBEXPORTLOG

echo 'This is output from TAO DBA script dbexport' > ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'date' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
date >> ${DBEXPORTLOG}

#
# document shell script environment for dbexport
#

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'Document shell script environment for dbexport' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'echo ${ORACLE_OWNER}' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
echo ${ORACLE_OWNER} >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'echo ${ORACLE_HOME}' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
echo ${ORACLE_HOME} >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'echo ${DBEXPORTLOG}' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
echo ${DBEXPORTLOG} >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'echo ${ORACLE_SID}' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
echo ${ORACLE_SID} >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'cat ${CSHRC_FILE}' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
cat ${CSHRC_FILE} >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'env for dbexport' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
env >> ${DBEXPORTLOG}

#
# document the server machine
#

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'Document the server machine' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'hostname' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
hostname >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'df -k' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
df -k >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}

#
# change directory to where the export file will be created
#

cd ${EXPORTDIR}

pwd

#
# default is export followed by compress
#
# if needed, export directly to pipe to use less disk space
# uncomment the # lines below 
#

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'make full export...' >> ${DBEXPORTLOG}
# echo 'make full export to pipe...' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}

#echo 'mknod /tmp/${EXPORT_PIPE} p' >> ${DBEXPORTLOG}
#echo 'compress < /tmp/${EXPORT_PIPE} > '${DBEXPORT_COMPRESSED}' &' >> ${DBEXPORTLOG}
#echo '${ORACLE_HOME}/bin/exp / full=y consistent=y direct=y BUFFER=10240000 file=/tmp/${EXPORT_PIPE} log=${EXPORT_LOG_TMP}' >> ${DBEXPORTLOG}
echo '${ORACLE_HOME}/bin/exp / full=y consistent=y direct=y BUFFER=10240000 file='${DBEXPORT}' log=${EXPORT_LOG_TMP}' >> ${DBEXPORTLOG}
echo 'cat ${EXPORT_LOG_TMP}' >> ${DBEXPORTLOG}
echo 'rm ${EXPORT_LOG_TMP}' >> ${DBEXPORTLOG}
#echo 'rm /tmp/${EXPORT_PIPE}' >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'start time of export' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
date >> ${DBEXPORTLOG}

#mknod /tmp/${EXPORT_PIPE} p
#compress < /tmp/${EXPORT_PIPE} > ${DBEXPORT_COMPRESSED} &
#${ORACLE_HOME}/bin/exp / full=y consistent=y direct=y BUFFER=10240000 file=/tmp/${EXPORT_PIPE} log=${EXPORT_LOG_TMP}
${ORACLE_HOME}/bin/exp / full=y consistent=y direct=y BUFFER=10240000 file=${DBEXPORT} log=${EXPORT_LOG_TMP}
cat ${EXPORT_LOG_TMP} >> ${DBEXPORTLOG}
rm ${EXPORT_LOG_TMP} >> ${DBEXPORTLOG}
#rm /tmp/${EXPORT_PIPE} >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'end time of export' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
date >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'directory of export file' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
pwd >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'df -k . for export directory' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
df -k . >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'export file name ${DBEXPORT}' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
echo ${DBEXPORT} >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'ls -l for export directory' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
ls -l >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'df -k for this server' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
df -k >> ${DBEXPORTLOG}

#
# if exporting to pipe, no sense in compressing (again), skip
# all commands down to next 'if exporting to pipe' comment
#

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'compress export file...' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}

echo 'compress '${DBEXPORT} >> ${DBEXPORTLOG}

compress ${DBEXPORT}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'end time of compression' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
date >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'directory of export file' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
pwd >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'df -k . for export directory' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
df -k . >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'compressed export file name ${DBEXPORT_COMPRESSED}' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
echo ${DBEXPORT_COMPRESSED} >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'ls -l for export directory' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
ls -l >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'df -k for this server' >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
df -k >> ${DBEXPORTLOG}

#
# if exporting to pipe -- end of commands to be commented out
#

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'Existing dbexport log files...' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo 'current sysdate' >> ${DBEXPORTLOG}
date >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
echo 'cd ${LOGDIR} ' >> ${DBEXPORTLOG}
cd ${LOGDIR}
pwd >> ${DBEXPORTLOG}
ls -l >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'Remove dbexport log files older than '${RETENTION_TIME_DAYS}' days' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}

/usr/bin/find . -name "DBEXPORT_${ORACLE_SID}_*.out" -ctime +${RETENTION_TIME_DAYS} -exec /bin/rm {} \;
echo ' ' >> ${DBEXPORTLOG}
pwd >> ${DBEXPORTLOG}
ls -l >> ${DBEXPORTLOG}


echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'Existing dbexport export files...' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo 'current sysdate' >> ${DBEXPORTLOG}
date >> ${DBEXPORTLOG}
echo ' ' >> ${DBEXPORTLOG}
echo 'cd ${EXPORTDIR}' >> ${DBEXPORTLOG}
cd ${EXPORTDIR}
pwd >> ${DBEXPORTLOG}
ls -l >> ${DBEXPORTLOG}

echo ' ' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}
echo 'Remove dbexport export files older than '${RETENTION_TIME_DAYS}' days' >> ${DBEXPORTLOG}
echo '*****************************************' >> ${DBEXPORTLOG}

/usr/bin/find . -name "DBEXPORT_${ORACLE_SID}_*.exp*" -ctime +${RETENTION_TIME_DAYS} -exec /bin/rm {} \;
echo ' ' >> ${DBEXPORTLOG}
pwd >> ${DBEXPORTLOG}
ls -l >> ${DBEXPORTLOG}



echo ' ' >> ${DBEXPORTLOG}
echo 'End of dbexport' >> ${DBEXPORTLOG}

MSG='DBEXPORT for '`hostname`':'${ORACLE_SID}' '`date +\%m\%d\%Y_\%T`

/usr/bin/mailx -s "${MSG}" ${NOTIFY_LIST} < ${DBEXPORTLOG}

#
#end of dbexport
#







