当前位置:Gxlcms > mysql > Linux/Unixshell自动FTP备份档案

Linux/Unixshell自动FTP备份档案

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

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

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

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

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

Linux/Unix shell sql 之间传递变量

Linux Unix shell 调用 PL/SQL

1、Linux/Unix shell 自动 FTP 脚本

robin@SZDB:~/dba_scripts/custom/bin> more auto_ftp_dump.sh
# +---------------------------------------------------+
# + Filename: ftp_dump_auto.sh |
# + Desc: Ftp the dump file automatically |
# + Usage: |
# + ./auto_ftp_dump.sh $ORACLE_SID |
# + Author: Robinson |
# + Blog : |
# +---------------------------------------------------+
#
#!/bin/bash
#
# -----------------------------
# Define environment variable
# -----------------------------
#

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

# --------------------------
# Check SID
# --------------------------

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

ORACLE_SID=${1}; export ORACLE_SID
TARGET_SID=SY0755BK
TIMESTAMP=`date +%Y%m%d%H%M`;
DT=`date +%Y%m%d`;
RETENTION=1

DUMP_DIR=/u01/oracle/admin/SY0755/BNR/dump
DUMP_FILE=${DUMP_DIR}/EXP_${ORACLE_SID}_${DT}.dmp
LAST_EXP_DUMP_LOG=${DUMP_DIR}/EXP_${ORACLE_SID}_${DT}.log

FTP_LOG=${DUMP_DIR}/ftp_${ORACLE_SID}_${TIMESTAMP}.log
FTP_ERR_LOG=${DUMP_DIR}/ftp_${ORACLE_SID}_${TIMESTAMP}_err.log

date >>${FTP_LOG}
echo "FTP source : ${ORACLE_SID}" >>${FTP_LOG}
echo "FTP target : ${TARGET_SID}" >>${FTP_LOG}
echo "Staring tar the dump file " >>${FTP_LOG}

# -------------------------
# tar the dump file
# -------------------------

if [ -s "${DUMP_FILE}" ] && [ -s "${LAST_EXP_DUMP_LOG}" ];then
cd ${DUMP_DIR}
tar -czvf EXP_${ORACLE_SID}_${DT}.tar.gz ./EXP_${ORACLE_SID}_${DT}.???
else
echo "Dump file does not exist for ${ORACLE_SID}" >>${FTP_LOG}
MAIL_SUB="Dump files were not found for ${ORACLE_SID} on `hostname` before start to tar the dump file"
mail -s $MAIL_SUB dba@trade.com <${FTP_LOG}
exit 1
fi

# --------------------------------------------
# config ftp parameter and ftp the dump file
# --------------------------------------------

if [ ! -s "${DUMP_DIR}/EXP_${ORACLE_SID}_${DT}.tar.gz" ];then
echo "The dump files were not compressed before start ftp">>${FTP_LOG}
MAIL_SUB=" The dump files were not compressed for ${ORACLE_SID} on `hostname` before start ftp, exit!"
mail -s $MAIL_SUB dba@trade.com <${FTP_LOG}
exit 1
fi

echo "Start copy tar file to ${TARGET_SID} ...." >>${FTP_LOG}
echo "--------------------------------------------------">>${FTP_LOG}

FTP_SRV=10.200.48.21
FTP_USR=oracle
FTP_PWD=oracle
FTP_LOCAL_PATH=${DUMP_DIR}
FTP_REMOTE_PATH=/u02/database/${TARGET_SID}/BNR/dump

ftp -nv >> $FTP_LOG <open ${FTP_SRV}
user ${FTP_USR} ${FTP_PWD}
binary
cd ${FTP_REMOTE_PATH}
lcd ${FTP_LOCAL_PATH}
prompt
mput EXP_${ORACLE_SID}_${DT}.tar.gz
close
bye
EOF

RC=$?
flag=`cat ${FTP_LOG} | grep -i "receive OK" | grep -v grep`
if [ -z "${flag}" ] || [ "${RC}" -ne 0 ] ; then
echo "FTP tar file to ${TARGET_SID} failed" >>${FTP_LOG}
MAIL_SUB="FTP dump file from ${ORACLE_SID} to ${TARGET_SID} failed, please check !"
mail -s $MAIL_SUB dba@trade.com <${FTP_LOG}
exit 1
else
echo "End ftp dump file to ${TARGET_SID} ...." >> ${FTP_LOG}
echo "End ftp time at:" `date` --`date +%Y%m%d%H%M` >>${FTP_LOG}
echo "">>${FTP_LOG}
echo "-------------------------- End of the log file -----------------------">>${FTP_LOG}
MAIL_SUB="FTP the dump file from ${ORACLE_SID} to ${TARGET_SID} completed successful."
mail -s $MAIL_SUB dba@trade.com <${FTP_LOG}
fi

find ${DUMP_DIR} -name "*ftp*" -mtime +$RETENTION -exec rm {} \;
find ${DUMP_DIR} -name "EXP_${ORACLE_SID}*.gz" -mtime +$RETENTION -exec rm {} \;

exit

linux

人气教程排行