Oracle certification www.oracle.com

Reduce the size of Oracle export files

An easy and handy export script.

Oracle export files get often very big and take much room on disk; it is therefore a usual thing to keep them compressed. The compression in turn requires time and disk space, because a temporary file is needed before the process completes.

For this reason on UNIX it is quite common to use an export script that generated a compressed dump file. Many DBAs have written their own version of such a script, but the one we offer here has the advantage of being very easy to use, with almost no requirement other than specifying the database instance.

It is true that from 8i onwards the export files can be splitted into chunks, but for busy people it is of course easier to deal with only one file.

In our script, gzip is used as the compression utility; you can replace it with compress if you prefer. The script accepts flags and behaves like many UNIX utilities. The first paramater is always the SID; the accepted flags are:
  • -u specify a schema
  • -d specify the destination directory of the export file
  • -e destination directory of the parameter file
The basic syntax is:

quickExp.ksh SID [full export of database SID]
quickExp.ksh SID -u SCOTT [only the user SCOTT is exported]
quickExp.ksh SID -d /var/tmp [full export; compressed file written onto /var/tmp]
quickExp.ksh SID -d . -e . [full export; compressed file and parfile written to the local directory

#!/bin/ksh
#-------------------------------------------------
# quickExp.ksh
#
# A script to export into a compressed file
#            © Front Row Seat, Zurich, Switzerland 
#
#--------------------------------------------------
writeExportPar () 
{
#
# Change the parameters according to your needs; the direct path is used
#
   cd ${EXP_CONF_DIR}
   echo "file=${PIPE_FILE}"             >  ${PAR_FILE}
   echo "log=${LOG}"                    >> ${PAR_FILE}
   if [ -n "$USERNAME" ];then 
     echo "full=n"                      >> ${PAR_FILE}
     echo "owner=$USERNAME"              >> ${PAR_FILE}
   else
     echo "full=y"                      >> ${PAR_FILE}
   fi
   echo "direct=y"                      >> ${PAR_FILE}
   echo "rows=y"                        >> ${PAR_FILE}
   echo "grants=y"                      >> ${PAR_FILE}
   echo "feedback=100000"               >> ${PAR_FILE}
   echo "statistics=none"               >> ${PAR_FILE}
   chmod 775 ${PAR_FILE}
}
#
SID=$1
USERNAME=
#
# EXP_CONF_DIR is the directory where the parfile will be written; hardcode it here or
# pass it using the flag -e
#
EXP_CONF_DIR=${ORACLE_BASE}/admin/exp
#
# ORADMP is the directory where the compressed export file and the log file will be written;
# hardcode it here or pass it to the script using the flag -d
#
ORADMP=/oradmp/${SID}
#
# RECEPIENTS is the list of email recepients that will be notified in case of error
#
RECEPIENTS=support@front-row-seat.com,webmaster@front-row-seat.com
#
if [ $# -lt 1 ]; then
  echo "Usage: $0  SID [ -u USER -d ORADMP -e EXP_CONF_DIR ]"
  exit 1
fi
iPar=$#
#
# the following line sets the environment; as a result ORACLE_HOME and ORACLE_BASE for
# the passed SID get defined. Replace it with the standard method at your site
#
. /usr/local/bin/oraenv ${SID}
shift
iPar=`expr $iPar - 1`
while [ $iPar -gt 0 ]
do
  echo ". iPar = $iPar "
  if [ "$1" = "-u" ]; then
    USERNAME=$2
  fi
  if [ "$1" = "-d" ]; then
    ORADMP=$2
    mkdir -p ${ORADMP}/${SID}
  fi
  if [ "$1" = "-e" ]; then
    EXP_CONF_DIR=$2
  fi
  shift 2
  iPar=`expr $iPar - 2`
done
echo "USERNAME> $USERNAME <"
echo "ORADMP > $ORADMP <"
echo "EXP_CONF_DIR > $EXP_CONF_DIR <"
echo "Press ^C NOW! if not happy; sleeping for 10s before start ..."
sleep 10
cd ${EXP_CONF_DIR}
#
if [ -n "$USERNAME" ];then
  Z_FILE=${ORADMP}/${SID}_${USERNAME}.dmp.gz
else
  Z_FILE=${ORADMP}/${SID}.dmp.gz
fi
echo "Export file will be $Z_FILE"
PIPE_FILE=${ORADMP}/${SID}.dmp
LOG=${ORADMP}/${SID}.log
PAR_FILE=export_${SID}.par
#
writeExportPar
#
# Make a pipe
/usr/sbin/mknod ${PIPE_FILE} p # or mkfifo pipe
# Start sucking from the pipe in the background
gzip -5 < ${PIPE_FILE} > ${Z_FILE} &
# Wait a second or so before kicking off the export
sleep 2
# Start the export
esten=`date '+%d%m%y-%H%M'`
#
# The user performing the export is externally identified, therefore connects using a slash.
# Alternatively, use 
# exp username/password parfile=${PAR_FILE} [which of course is very bad for security reasons] or
# exp username parfile=${PAR_FILE}   and you will be prompted for a password
#
exp / parfile=${PAR_FILE} 
mv ${LOG} ${LOG}.${esten}
mv ${Z_FILE} ${Z_FILE}.${esten}
rm -f $PIPE_FILE
chmod 775 ${LOG}.${esten} ${Z_FILE}.${esten}
#
LOG=${LOG}.${esten}
isGood=`tail -1 $LOG| grep -c 'Export terminated successfully' `
if [ $isGood -eq 0 ];then
  (tail -5 $LOG) | mailx -s "!!!!!: On `hostname` check ${LOG}" $RECEPIENTS  
  EXPSTATUS=1
else
  (echo "Export of ${SID} looks good") | mailx -s "Good: On `hostname` ${LOG}" operator@fictiousCompany.com
  EXPSTATUS=0
fi
#
cd ${ORADMP}
find . -name '*gz*'  -ctime +3 -exec rm -f {} \;
find . -name '*.log*' -ctime +3 -exec rm -f {} \;
exit ${EXPSTATUS}


This was the export script; we also offer an analogous import script which reads compressed files, but being very smart and with good tricks it is only for sale. It costs 50$ and can be bought through PayPal.

Our PayPal email address is support@front-row-seat.com; please specify quickImp.ksh

[Home] [Web Design] [HTML tutorials] [Javascript] [PSP] [About us] [Links] [Anonymous email] [Best hosting] [Daily Oracle Life] [IT jobs in Switzerland] [Web Submission] [Web traffic]
Rate this article ...
Very poor Poor Average Good Very good