当前位置:Linux教程 - Oracle - shell - ORACLE自动备份并且自动FTP到备份机的SHELL脚本

shell - ORACLE自动备份并且自动FTP到备份机的SHELL脚本

ORACLE自动备份并且自动FTP到备份机的SHELL脚本
2004-04-23 15:18 pm
来自:Linux文档
现载:Www.8s8s.coM
地址:无名

本次上传的脚本有很多可以改进的地方!这也是本人第一次在网上发帖,SH写的不好的地方
不要见笑!!!!!!本SH以在SCO OPENSERVER 505(ORACLE 7.3.2)和IBM RS6000 AIX 4.3(ORACLE 8.1.6)调试通过!!如果要转载,请加上出处!!!谢谢!!!详细功能见脚本文件SH的批注!!!如果能要加上自动执行的功能请使用crontab配置即可!!!!

第一个文件:INIT.TXT这是脚本INI初始化文件:
##################################################################
#This is a system ini file :
# CopyRight By WangZuXiang 2002
# the '#' is ingnor :
#the format style is xxxx=value
#
##################################################################

#Oracle cionfig
[oracle]
username=ydyx
password=ydyx
con_alias=MISDB

#the main machine config
[mainhost]
myhost_ip=10.232.193.2
myhost_username=oracle
myhost_password=oracle
dump_dir=/u2/oracleback

#the backup machine config
[backuphost]
backhost_ip=10.232.193.5
backhost_username=oracle
backhost_password=oracleydyx
backup_dir=/u2/oracleshaoshanback

#delete the dump file x days ago?!!!
[deletedumpfile]
delete_days=2

#the Oracle database's back method
#如果start_tactic=Y 则采用策略备份,否则按每天完全到出
#如果采用策略备份,则必须给SYSTEM用户的口令
[backmethod]
start_tactic=N
system_password=manager11

#the recode the system execute's log!
[systemlog]
sys_log=/home/oracle/wzx/x.log
#end

第二个文件:主程序文件:MY.SH:
#!/bin/sh
#############################################################################
#
#Get the all ini file config
#CopyRight By WangZuXiang 2002-2005?
#版权所有(C) 2002-2005? WangZuXiang .
#All Rights Reserved.
#program date 2002-10-22
#usage: main
#This script is used to start oracle dump database .
#It should ONLY be executed as part of the crontabs.
#function content is:
#1:自动检查是否能连上Oracle!
#2:通过初始化文本来解析各种需要的参数!
#3:自动完全到出ORACLE数据库(每天)!,以后将增加相关的增量备份等!
#4:自动删除配置文件中规定的前X天的备份文件!
#5:自动将到出文件打包并转存到其他的备份机器上!
#6:自动记录程序执行过程中的所有日期信息!
#if you find some bugs please send it to my mailpost :
#e-mail:[email protected] !
#new function:增加了对ORACLE数据库备份策略的方法!2002-10-31
##############################################################################
mypwd=`pwd`
inifile=/home/oracle/wzx/init.txt
oratab=/etc/oratab
TEMP=temp.$$
DUMP_FILE=`date +'%Y%m%d`

initme ()
{
#read from the config file 'ini.txt'

#oracle config
USERNAME=`cat ${inifile:-"init.txt"} | grep -i '^username' | awk -F = '{print $2}'`
PASSWORD=`cat ${inifile:-"init.txt"} | grep -i '^password' | awk -F = '{print $2}'`
MY_ALIAS=`cat ${inifile:-"init.txt"} | grep -i '^con_alias' | awk -F = '{print $2}'`

#the main host config
MYHOST_IP=`cat ${inifile:-"init.txt"} | grep -i '^myhost_ip' | awk -F = '{print $2}'`
MYHOST_USERNAME=`cat ${inifile:-"init.txt"} | grep -i '^myhost_username' | awk -F = '{print $2}'`
MYHOST_PASSWORD=`cat ${inifile:-"init.txt"} | grep -i '^myhost_password' | awk -F = '{print $2}'`
DUMP_DIR=`cat ${inifile:-"init.txt"} | grep -i '^dump_dir' | awk -F = '{print $2}'`

#the backup host config
BACKHOST_IP=`cat ${inifile:-"init.txt"} | grep -i '^backhost_ip' | awk -F = '{print $2}'`
BACKHOST_USERNAME=`cat ${inifile:-"init.txt"} | grep -i '^backhost_username' | awk -F = '{print $2}'`
BACKHOST_PASSWORD=`cat ${inifile:-"init.txt"} | grep -i '^backhost_password' | awk -F = '{print $2}'`
BACKUP_DIR=`cat ${inifile:-"init.txt"} | grep -i '^backup_dir' | awk -F = '{print $2}'`

#the define's delete dump file days!
DAYS=`cat ${inifile:-"init.txt"} | grep -i '^delete_days' | awk -F = '{print $2}'`
#to get the system log
LOGS=`cat ${inifile:-"init.txt"} | grep -i '^sys_log' | awk -F = '{print $2}'`
TACTIC=`cat ${inifile:-"init.txt"} | grep -i '^start_tactic' | awk -F = '{print $2}'`
SYSTEMPASS=`cat ${inifile:-"init.txt"} | grep -i '^system_password' | awk -F = '{print $2}'`

#read the config file end.
}

#WHOAMI=`id | awk '/uid=[0-9]*(oracle)/ {print $1}'`
#WHOAMI=`id | awk '{print $1}' | sed 's/uid=[0-9]*(.*).*/1/'`
WHOAMI=`id | sed 's/uid=[0-9]*(.*) gid.*/1/'`
#if test "$WHOAMI" = "" ; then
if test "$WHOAMI" != "(oracle)" ; then
echo " "
echo "=============================================================="
echo "=========you must use oracle user to run this script!========="
echo "=============================================================="
echo " "
exit 0
fi

#run the init parameter
test ! -e $inifile && echo "not find the init file:"$inifile"!" && exit 0

initme

if [ -z $LOGS ]; then
echo "<<can not to find the define of system log...!!!>>"
fi

#LOG = ${LOGS:-"x.log"}
LOGME="tee -a ${LOGS:-"x.log"}"

if [ -e $LOGS -a -f $LOGS ]; then
if test ! -w $LOGS; then
echo "<<the log file:"$LOGS" can not to write!....................>>"
exit
fi
fi

echo " " | $LOGME
echo "-----------------------------------------------------------------" | $LOGME
echo "<<system now is initing ..............!>>" | $LOGME

#check the parameter value
if [ -z $USERNAME ]; then
echo "<<the oracle database's username is null..............!>>" | $LOGME
exit 1
fi

if [ -z $PASSWORD ]; then
echo "<<the oracle database's password is null..............!>>" | $LOGME
exit 2
fi

if [ -z $MY_ALIAS ]; then
echo "<the oracle connect alias is null!..............>>" | $LOGME
exit 0
fi

if [ -z $MYHOST_IP ]; then
echo "<<the oracle's host ip is null..............!>>" | $LOGME
exit 3
fi

if [ ! -d $DUMP_DIR ]; then
echo "<the oracle's dump dir is not exist..............!>>" | $LOGME
exit 0
fi

if [ -z $MYHOST_USERNAME ]; then
echo "<<then oracle host username is null..............!>>" | $LOGME
exit 4
fi

if [ -z $MYHOST_PASSWORD ]; then
echo "<<the oracle host password is null..............!>>" | $LOGME
exit 5
fi

if [ -z $BACKHOST_IP ]; then
echo "<<the backup host's ip is null..............!>>" | $LOGME
exit 6
fi

if [ -z $BACKHOST_USERNAME ]; then
echo "<<the backup host's uesername is null..............!>>" | $LOGME
exit 7
fi

if [ -z $BACKHOST_PASSWORD ]; then
echo "<<the backup host's password is null..............!>>" | $LOGME
exit 8
fi

if [ -z $BACKUP_DIR ]; then
echo "<<the backup host's backup dir is null..............!>>" | $LOGME
exit 9
fi
#elif [ ! -e $BACKUP_DIR -o ! -d $BACKUP_DIR ]; then
#echo "<<the backup dir "$BACKUP_DIR" is not exist or is not directory..............!>>" | $LOGME
#exit 0
#fi

#if [ ! -O $BACKUP_DIR ]; then
#echo "<<the backup host's backup dir is not owner to oracle..............!>>" | $LOGME
#exit 0
#fi

if [ -z $DAYS ]; then
echo "<<the delete file define days is null so not to delete...............!>>" | $LOGME
fi

if test $TACTIC = "Y" ; then
if test -z $SYSTEMPASS ; then
echo "<<if you use TACTIC back,then you must give the oracle system password!...............!>>" | $LOGME
exit 0
fi
fi

if test $? -eq 0 ; then
echo "<<system init is OK............................!>>" | $LOGME
fi

#check end.
#
#main
#

if [ ! -f $oratab -o ! -e $oratab ]; then
echo "<<the oracle define's oratab file is not normal file or is not exist..............!>>" | $LOGME
exit 0
fi

ORACLE_SID=`cat $oratab | grep -v "^#" | grep "Y$" | awk -F : '{print $1}' `
if [ -z $ORACLE_SID ]; then
echo "<<the ORACLE_SID is not find ..............!>>" | $LOGME
exit -1
fi
export ORACLE_SID
ORACLE_HOME=`cat $oratab | grep -v "^#" | grep "Y$" | awk -F : '{print $2}' `
if [ ! -d $ORACLE_HOME ]; then
echo "the oracle's home directory "$ORACLE_HOME" is not exist..............!>>" | $LOGME
exit -1
fi
export ORACLE_HOME
PATH=$PATH:$ORACLE_HOME/bin:/usr/lib:/bin:$HOME/bin:.
ORACLE_BASE=/u1/app/oracle
export ORACLE_BASE
ORACLE_SID=misdb
export ORACLE_SID
ORACLE_HOME=$ORACLE_BASE/product/8.1.6
export ORACLE_HOME
PATH=$PATH:$ORACLE_HOME/bin
export PATH
LIBPATH=$LIBPATH:$ORACLE_HOME/lib
export LIBPATH
ORA_NLS33=$ORACLE_HOME/ocommon/nls/admin/data
export ORA_NLS33
export PATH
#############################################
## begin to dump the database
#############################################

#trying to connect oracle .....
if [ -e $TEMP ]; then
rm -f $TEMP
if [ ! $? -eq 0 ]; then
echo "<<delete the file: "$TEMP" is fail..............!!!!>>" | $LOGME
exit -1
fi
fi

if [ -x $ORACLE_HOME/bin/tnsping ]; then
#$ORACLE_HOME/bin/tnsping $MY_ALIAS > $TEMP
tnsping $MY_ALIAS > $TEMP
ME=`cat $TEMP | grep "OK" | awk '{print $1}'`
if [ "$ME" != "OK" ]; then
echo "<<can not to connect oracle by tnsping:"$MY_ALIAS"..............!>>" | $LOGME
rm -f $TEMP
exit $?
else
echo "<<connect oracle is ok!...............................!>>" | $LOGME
fi
fi

rm -f $TEMP

if [ -x $ORACLE_HOME/bin/exp ]; then
#MY_ALIAS
#cd $ORACLE_HOME/bin
#if [ ! $? -eq 0 ]; then
#echo "error "
#exit 0
#fi

trap "" 1 2 3 15

#如果不用策略备份,则采用倒用户的方式,到出!!!!
#echo $TACTIC
#
#if [ "$TACTIC" = "Y" ]; then
#if [ -x $ORACLE_HOME/bin/svrmgrl ]; then
#svrmgrl <<EOF
#connect $USERNAME/$SYSTEMPASS@$MY_ALIAS
#exit
#EOF
#if test $? -ne 0 ; then
#echo "<<SYSTEM user password is error!.................>>" | $LOGME
#exit $?
#fi
#fi
#fi
#

if [ "$TACTIC" != "Y" -o "$TACTIC" != "y" ]; then
echo "<<系统将按倒出用户方式进行!..............................>>" | $LOGME
$ORACLE_HOME/bin/exp $USERNAME/$PASSWORD@$MY_ALIAS buffer=40960 file=$DUMP_DIR/$DUMP_FILE.dmp owner=$USERNAME grants=Y rows=Y compress=Y indexes=Y log=$DUMP_DIR/$DUMP_FILE.log

#采用策略备份,则对整个数据库进行策略备份!

else
echo "<<系统将按策略备份用户方式进行!..............................>>" | $LOGME
MYWEEK=`date | awk '{print $1}'`
case $MYWEEK in
#星期一完全导出
Mon)exp system/$SYSTEMPASS@$MY_ALIAS inctype=complete buffer=40960 file=$DUMP_DIR/$DUMP_FILE.dmp grants=Y rows=Y compress=Y indexes=Y log=$DUMP_DIR/$DUMP_FILE.log
echo "<<Mon is complete dump the db,file:"$DUMP_FILE.dmp"...............!>>" | $LOGME
;;
#星期二增量导出
Tue)exp system/$SYSTEMPASS@$MY_ALIAS inctype=incremental buffer=40960 file=$DUMP_DIR/$DUMP_FILE.dmp grants=Y rows=Y compress=Y indexes=Y log=$DUMP_DIR/$DUMP_FILE.log
echo "<<Tue is incremental dump the db,file:"$DUMP_FILE.dmp"...............!>>" | $LOGME
;;
#星期三增量导出
Wed)exp system/$SYSTEMPASS@$MY_ALIAS inctype=incremental buffer=40960 file=$DUMP_DIR/$DUMP_FILE.dmp grants=Y rows=Y compress=Y indexes=Y log=$DUMP_DIR/$DUMP_FILE.log
echo "<<Wed is incremental dump the db,file:"$DUMP_FILE.dmp"...............!>>" | $LOGME
;;
#星期四增量导出
Thu)exp system/$SYSTEMPASS@$MY_ALIAS inctype=incremental buffer=40960 file=$DUMP_DIR/$DUMP_FILE.dmp grants=Y rows=Y compress=Y indexes=Y log=$DUMP_DIR/$DUMP_FILE.log
echo "<<Thu is incremental dump the db,file:"$DUMP_FILE.dmp"...............!>>" | $LOGME
;;
#星期五累计导出
Fri)exp system/$SYSTEMPASS@$MY_ALIAS inctype=cumulative buffer=40960 file=$DUMP_DIR/$DUMP_FILE.dmp grants=Y rows=Y compress=Y indexes=Y log=$DUMP_DIR/$DUMP_FILE.log
echo "<<Fri is cumulative dump the db,file:"$DUMP_FILE.dmp"...............!>>" | $LOGME
;;
#星期六增量导出
Sat)exp system/$SYSTEMPASS@$MY_ALIAS inctype=incremental buffer=40960 file=$DUMP_DIR/$DUMP_FILE.dmp grants=Y rows=Y compress=Y indexes=Y log=$DUMP_DIR/$DUMP_FILE.log
echo "<<Sat is incremental dump the db,file:"$DUMP_FILE.dmp"...............!>>" | $LOGME
;;
#星期日增量导出
Sun)exp system/$SYSTEMPASS@$MY_ALIAS inctype=incremental buffer=40960 file=$DUMP_DIR/$DUMP_FILE.dmp grants=Y rows=Y compress=Y indexes=Y log=$DUMP_DIR/$DUMP_FILE.log
echo "<<Sun is incremental dump the db,file:"$DUMP_FILE.dmp"...............!>>" | $LOGME
;;
*)
;;
esac
fi

if test $? -eq 0 -a -s $DUMP_DIR/$DUMP_FILE.dmp
then
echo "<<dump the oracle database success at: "$DUMP_FILE"..............!>>" | $LOGME
else
echo "<<dump the oracle database fail at: "$DUMP_FILE"..............!>>" | $LOGME
exit $?
fi
else
echo "<<sorry!,the exp not to find or not execute,please check it..............!>>" | $LOGME
exit $?
fi

#其实可以用find . -atime -days -exec rm {} 删除前DAYS天的备份文件,但查找不是很精确!
#if define the days then to execute delete the dump file $DAYS ago!
if [ -n $DAYS -a $DAYS -gt 0 ]; then
echo "<<system will to remove the dump file $DAYS days ago..............!>>" | $LOGME
if [ -x $ORACLE_HOME/bin/svrmgrl ]; then
svrmgrl <<EOF
connect $USERNAME/$PASSWORD@$MY_ALIAS
spool $TEMP
select to_char(sysdate-$DAYS,'yyyymmdd') mydate from dual;
spool off
exit
EOF
if test $? -eq 0 -a -f $TEMP -a -s $TEMP ; then
echo "<<get the $DAYS days ago's date is ok..............!>>" | $LOGME
else
echo "<<to get $DAYS days ago's date is fail(connect oracle is fail---------)..............!>>" | $LOGME
#exit $?
fi
else
echo "<<not find the oracle's svrmgrl program or cant not execute and get the days is fail..............!>>" | $LOGME
exit $?
fi

#####################
##
## Gegin to remove the dump file
##
#####################

#TEMPDATE=`cat $TEMP | sed '1d' | sed '$d' | grep -v '^--*'`
TEMPDATE=`cat $TEMP | sed '1d' | sed '$d' | sed '/^--*/d'`
#echo "$TEMPDATE"
rm -f $TEMP
if test ! $? -eq 0 ; then
echo "<<can not to remove the temp file:"$TEMP"..............!>>" | $LOGME
fi
#echo "${DUMP_DIR}/${TEMPDATE}.dmp"
if test -e $DUMP_DIR/$TEMPDATE.dmp -a -f $DUMP_DIR/$TEMPDATE.dmp -a -n $TEMPDATE; then
rm -f $DUMP_DIR/$TEMPDATE.dmp
rm -f $DUMP_DIR/$TEMPDATE.log
rm -f $DUMP_DIR/$TEMPDATE.tar.Z
if test $? -eq 0 ; then
echo "<<remove the "$DUMP_DIR/$TEMPDATE.dmp" is ok..............!>>" | $LOGME
else
echo "<<remove the "$DUMP_DIR/$TEMPDATE.dmp" is fail..............!>>" | $LOGME
#exit $?
fi
else
echo "<<can not to find the file: "$DUMP_DIR/$TEMPDATE.dmp" ..............!>>" |
$LOGME
#exit $?
fi
fi

####################################################
#
#to transfer the dump file to other's host user ftp
#
####################################################

if [ -e $DUMP_DIR/$DUMP_FILE.dmp -a -e $DUMP_DIR/$DUMP_FILE.log ]; then
tar -cvf $DUMP_DIR/$DUMP_FILE.tar $DUMP_DIR/$DUMP_FILE.dmp $DUMP_DIR/$DUMP_FILE.log && compress $DUMP_DIR/$DUMP_FILE.tar | $LOGME
if test $? -eq 0 -a -e $DUMP_DIR/$DUMP_FILE.tar.Z ; then
echo "<<the dump file "$DUMP_FILE.tar.Z" is archive ok.........!>>" | $LOGME
rm -f $DUMP_DIR/$DUMP_FILE.tar
rm -f $DUMP_DIR/$DUMP_FILE.log
rm -f $DUMP_DIR/$DUMP_FILE.dmp
if test ! $? -eq 0 ; then
echo "<<delete the dump file is fail!.........................!>>" | $LOGME
else
echo "<<delete the dump file is ok!.........................!>>" | $LOGME
fi
else
echo "<<the dump file "$DUMP_FILE.tar.Z" is archive fail..........!>>" | $LOGME
exit $?
fi
fi

echo "<<system now is transfering file to ${BACKHOST_IP}..............!>>" | $LOGME

ftp -n $BACKHOST_IP <<! | $LOGME
user $BACKHOST_USERNAME $BACKHOST_PASSWORD
bin
prompt
put $DUMP_DIR/$DUMP_FILE.tar.Z $BACKUP_DIR/$DUMP_FILE.tar.Z
close
bye
!

if test $? -eq 0 ; then
echo "<<Transfer the file:"$DUMP_DIR/$DUMP_FILE.tar.Z" to "$BACKHOST_IP"$BACKUP_DIR is OK.................! >>" | $LOGME
else
echo "<<Transfer the file:"$DUMP_DIR/$DUMP_FILE.tar.Z" to "$BACKHOST_IP"$BACKUP_DIR is fail..................! >>" | $LOGME
exit $?
fi

if test $? -eq 0 ; then
echo "<<the script is execute finish!,please check it!.......................................!>>" | $LOGME
echo "-----------------------------------------------------------------" | $LOGME
exit 0
fi