Linux/Unix shell 自动导出Oracle数据库

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

Linux Shell以及导入导出的相关参考:

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

# +-------------------------------------------------------+
# +    Export database by schema every day                |
# +    Author : Robinson                                  |
# +    Blog  : http://blog.csdn.net/robinson-0612        |
# +    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="[email protected]"; export MAIL_LIST
MAIL_DIR=/users/robin/dba_scripts/sendEmail-v1.56
MAIL_FM='[email protected]'
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

相关推荐