使用shell脚本实现对Oracle数据库的监控与管理将大大简化DBA的工作负担,如常见的对实例的监控,监听的监控,告警日志的监控,以及数据库的备份,AWR report的自动邮件等。本文给出Linux 下使用 shell 脚本来实现数据库自动导出。

1、自动导出Oracle数据库shell脚本

# +-------------------------------------------------------+
# +    Export database by schema every day                |
# +    Author : Robinson                                  |
# +    Blog   :         |
# +    Usage  :                                           | 
# +         expdb.sh $ORACLE_SID                          |
# +-------------------------------------------------------+
#
#!/bin/bash 
# --------------------
# Define variable
# --------------------

if [ -f ~/.bash_profile ]; then
. ~/.bash_profile
fi

if [ -z "${1}" ];then
    echo "Usage: "
    echo "      `basename $0` ORACLE_SID"
    exit 1
fi

ORACLE_SID=$1;                 export ORACLE_SID 
DT=`date +%Y%m%d`;             export DT
TIMESTAMP=`date +%Y%m%d_%H%M`
MAIL_LIST="robinson.chen@12306.com"; export MAIL_LIST
MAIL_DIR=/users/robin/dba_scripts/sendEmail-v1.56
MAIL_FM='oracle@szdb.com'
LOG_DIR=/users/robin/dba_scripts/custom/log
LOG_FILE=$LOG_DIR/expdb_${ORACLE_SID}_${TIMESTAMP}.log
DUMP_FILE=EXP_${ORACLE_SID}_${DT}.dmp
DUMP_LOG=EXP_${ORACLE_SID}_${DT}.log
DUMP_DIR=/u02/database/${ORACLE_SID}/BNR/dump
RETENTION=1

# ------------------------------------------------------------------------
#  Check the target database status, if not available send mail and exit
# ------------------------------------------------------------------------

db_stat=`ps -ef | grep pmon_$ORACLE_SID | grep -v grep| cut -f3 -d_`
if [ -z "${db_stat}" ]; then
    MAIL_SUB=" $ORACLE_SID is not available on `hostname` before try to export data  !!!"
    $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_SUB 
    exit 1
fi;

# ------------------------------------
#  Check the dumpfile exist or not
# ------------------------------------

if [ -s "${DUMP_DIR}/${DUMP_FILE}" ]; then
    MAIL_SUB=" The dump file ${DUMP_FILE} exists for ${ORACLE_SID} on `hostname`, exit !!!!"
    $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_SUB
    exit 1
fi

echo "`date`" >>$LOG_FILE
echo "The hostname is :`hostname`">>$LOG_FILE
echo "The database is :${ORACLE_SID}">>$LOG_FILE
echo "  Start to export data ..............." >>$LOG_FILE

# --------------------------------------
#  Start to export data
# --------------------------------------

expdp \'\/ as sysdba\' directory=db_dump_dir dumpfile=${DUMP_FILE} logfile=${DUMP_LOG} schemas=goex_admin #parallel=3
RC=$?

echo " ">>$LOG_FILE
cat ${DUMP_DIR}/${DUMP_LOG} >>$LOG_FILE
echo " ">>$LOG_FILE
echo "------------------------- End of the log file ------------------------">>$LOG_FILE

flag=`cat ${DUMP_DIR}/${DUMP_LOG} | grep -i "successfully completed"`

if [ "${RC}" -eq 0 ] && [ -n "${flag}" ];then
    MAIL_SUB=" Export database ${ORACLE_SID} finished successful on `hostname`  !!!"
    $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -o message-file=$LOG_FILE
else
    MAIL_SUB=" Export database ${ORACLE_SID} failed on `hostname`  !!!"
    $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -o message-file=$LOG_FILE
fi

# ------------------------------------------------
# Removing files older than $RETENTION parameter 
# ------------------------------------------------

find ${LOG_DIR} -name "expdb*.*" -mtime +$RETENTION -exec rm {} \;
find ${DUMP_DIR} -name "EXP*" -mtime +$RETENTION -exec rm {} \;

exit

2、补充说明
a、该脚本实现了基于schema的数据库导出,如果要整个导出数据库,需要修改导出部分代码
b、导出的dump文件的格式为EXP_$ORACLE_SID_yyyymmdd.dmp,EXP_$ORACLE_SID_yyyymmdd.log
c、由于每天仅导出一次,所以对上面文件的命名使用了yyyymmdd,如果多次导出应考虑修改代码或文件名格式
d、注意导出命令中转义字符的使用e、使用了sendEmail邮件发送程序来发送邮件。

该脚本在Oracle 10g测试可用,Oracle 11g待测
g、要实现自动导出,当然是将其部署到crontab,此不赘述