当前位置:Gxlcms > mysql > Linux/Unixshell自动导出Oracle数据库

Linux/Unixshell自动导出Oracle数据库

时间:2021-07-01 10:21:17 帮助过:26人阅读

使用shell脚本实现对Oracle数据库的监控与管理将大大简化DBA的工作负担,如常见的对实例的监控,监听的监控,告警日志的监控,以

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

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

Linux/Unix shell 脚本中调用SQL,RMAN脚本

Linux/Unix shell sql 之间传递变量

Linux Unix shell 调用 PL/SQL

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

# +-------------------------------------------------------+
# + Export database by schema every day |
# + Author : Robinson |
# + Blog : -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="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

linux

人气教程排行