- 浏览: 53215 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
宋建勇:
那我要实现三个输入框,至少有一个不能为空,怎么验证?!
DWZ验证表单规则一览 -
xy_arrow:
能把包 也发下吗 ? 744743291@qq.com
基于注解的SpringMVC整合JPA
Oracle数据库备份是每个商业项目中DBA必须要执行的工作,备份数据库工作周期长,内容单调。
现在,通过运行挂载在crontab进程当中的backup.sh文件的方法,实现了定时自动备份数据库,大大缩减了DBA的工作量,同时保证了夜间闲时备份数据库。闲话少说,代码如下。注意,拷贝代码后一定要看后面的注意内容。
通过INI文件设置初始化信息。第一个文件:
#################################################################
#This is a system ini file :
# CopyRight By Davidhan 2008
# the '#' is ingnor :
#the format style is xxxx=value
#################################################################
#Oracle cionfig
[oracle]
username=system //备份的帐户
password=manager //密码
con_alias=testsid //Oracle_Sid
#the main machine config
[mainhost]
myhost_ip=192.168.10.XX //本地服务器IP
myhost_username=root //本地登录帐户名
myhost_password=123456 //登录帐户密码
dump_dir=/home/oracle/oraclebackup //备份文件存储路径
#the backup machine config
[backuphost]
backhost_ip=192.168.10.XX //备份FTP服务器IP
backhost_username=XXXXXX //FTP服务器帐号
backhost_password=XXXXXX //密码
backup_dir=/cncfile/oraclebackup //存放备份文件的路径
#delete the dump file x days ago
[deletedumpfile]
delete_days=2 //删除DB服务器几日前备份文件
#the Oracle database's back method
#如果start_tactic=Y 则采用增量备份,否则按每天完全导出
#如果采用增量备份,则必须给SYSTEM用户的口令
[backmethod]
start_tactic=N
system_password=manager
#the recode the system execute's log!
[systemlog]
sys_log=/home/oracle/backupLog/backup.log // 执行Log
#end
通过backup.sh文件备份数据库。第二个文件:
#!/bin/sh ############################################################################# # #Get the all ini file config #CopyRight By Davidhan 2008-2009? #copyright (C) 2007-2008 Davidhan #All Rights Reserved. #program date 2008-6-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: test connectiong status of oracle #2: from ini.txt get information of shell start paramater #3: auto export oracle dump everyday #4: auto delete X days ago dump files that setted in config file #5: auto realese and send backup file to FTP server #6: log info #7: optical backup ############################################################################## mypwd='pwd' inifile=/home/backupOracle/init.txt oratab=/etc/oratab TEMP=temp.$$ DUMP_FILE=`date +%Y%m%d`
initme () { #read from the config file 'init.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. }
#run the init parameter test ! -e $inifile && echo "not find the init file:"$inifile"!" && exit 0
initme
echo "=========$LOGS=========" echo "=========$USERNAME=========" echo "=========$PASSWORD=========" echo "=========$MY_ALIAS=========" echo "=========$MYHOST_IP=========" echo "=========$MYHOST_USERNAME=========" echo "=========$MYHOST_PASSWORD========="
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
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 ORACLE_BASE=/home/oracle/oracle export ORACLE_BASE export ORACLE_SID export ORACLE_HOME PATH=$PATH:$ORACLE_HOME/bin export PATH LIBPATH=$LIBPATH:$ORACLE_HOME/lib export LIBPATH ORA_NLS33=$ORACLE_HOME/nls/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 trap "" 1 2 3 15
if [ "$TACTIC" != "Y" -o "$TACTIC" != "y" ]; then echo "<<system will run at the way of user!..............................>>" | $LOGME $ORACLE_HOME/bin/exp $USERNAME/$PASSWORD@$MY_ALIAS buffer=40960 file=$DUMP_DIR/$DUMP_FILE.dmp full=y Log=$DUMP_DIR/$DUMP_FILE.log
#optical backup
else echo "<<system will run at the way of optical user!..............................>>" | $LOGME MYWEEK='date | awk '{print $1}'' case $MYWEEK in #1 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 ;; #2 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 ;; #3 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 ;; #4 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 ;; #5 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 ;; #6 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 ;; #7 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
#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/sqlplus ]; then sqlplus $USERNAME/$PASSWORD@$MY_ALIAS<<EOF 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 sqlplus program or cant not execute and get the days is fail..............!>>" | $LOGME exit $? fi
##################### ## ## Begin to remove the dump file ## #####################
TEMPDATE=`cat $TEMP | sed '1d' | sed '$d' | sed '/^--*/d'` echo "<<date:"$TEMPDATE"..............!>>" | $LOGME 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_DIR/$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
代码部分结束。
可按照init文件中的内容设定工作目录和工作的文件内容。
现在是操作手顺:
1. 在将backup.sh和init.txt文件拷贝到 /home/backupOracle文件夹内。
说明:/home/backupOracle目录不存在可运行 #mkdir /home/backupOracle命令创建。
2. 创建工作目录。 工作日志文件存放目录:#mkdir /home/oracle/backupLog
生成备份文件存放目录:#mkdir /home/oracle/oraclebackup
3. 创建定时工作表。
l #vi confile
l 进入定时工作表编辑器
l 点击键盘insert按键,调整编辑器状态至插入
l 输入:10 4 * * * /home/backupOracle/backup.sh(意为备份任务执行的时间为每天4点10分)
l 点击键盘ESC按键推出编辑状态
l Shift +z+z(键盘输入两个大写Z)保存退出编辑器
l #crontab confile(将任务注入到crontab进程)。此时可在 /var/spool/cron文件夹中生成名为root的工作表
l #crontab -l 可查看到工作表的内容
通过以上步骤执行,我们为系统定制了每天4点10分执行的备份任务。工具采用全局备份,导出除ORDSYS,MDSYS,CTXSYS,ORDPLUGINS,LBACSYS 这些系统用户之外的所有用户的数据。在oraclebackup文件夹下生成命名为当日时间(格式为YYYYDDMM)的dmp文件的tar压缩文件,并在指定远程ftp备份主机中保留备份文件,工具可在执行时检测备份本地备份文件夹,自动删除2日前的备份的文件。这些都在init.txt文件中设定。
还原数据库操作手顺
l #cd /home/oracle/oraclebackup
l #tar zxvf YYYYDDMM.tar.z
l #imp user/password@oracle_sid file=/home/oracle/oraclebackup/home/ oracle/oraclebackup/YYYYDDMM.dmp full=y ignore=y
以上是备份的全部内容,实现了备份数据库,定时,ftp远程备份。
注意:没有linux基础的同学不太了解linux下的命令组成
提醒一点,不要把“`”看成“'”,一个是键盘上ESC对应那个符号。这个在linux中指命令域。
现在,通过运行挂载在crontab进程当中的backup.sh文件的方法,实现了定时自动备份数据库,大大缩减了DBA的工作量,同时保证了夜间闲时备份数据库。闲话少说,代码如下。注意,拷贝代码后一定要看后面的注意内容。
通过INI文件设置初始化信息。第一个文件:
#################################################################
#This is a system ini file :
# CopyRight By Davidhan 2008
# the '#' is ingnor :
#the format style is xxxx=value
#################################################################
#Oracle cionfig
[oracle]
username=system //备份的帐户
password=manager //密码
con_alias=testsid //Oracle_Sid
#the main machine config
[mainhost]
myhost_ip=192.168.10.XX //本地服务器IP
myhost_username=root //本地登录帐户名
myhost_password=123456 //登录帐户密码
dump_dir=/home/oracle/oraclebackup //备份文件存储路径
#the backup machine config
[backuphost]
backhost_ip=192.168.10.XX //备份FTP服务器IP
backhost_username=XXXXXX //FTP服务器帐号
backhost_password=XXXXXX //密码
backup_dir=/cncfile/oraclebackup //存放备份文件的路径
#delete the dump file x days ago
[deletedumpfile]
delete_days=2 //删除DB服务器几日前备份文件
#the Oracle database's back method
#如果start_tactic=Y 则采用增量备份,否则按每天完全导出
#如果采用增量备份,则必须给SYSTEM用户的口令
[backmethod]
start_tactic=N
system_password=manager
#the recode the system execute's log!
[systemlog]
sys_log=/home/oracle/backupLog/backup.log // 执行Log
#end
通过backup.sh文件备份数据库。第二个文件:
#!/bin/sh ############################################################################# # #Get the all ini file config #CopyRight By Davidhan 2008-2009? #copyright (C) 2007-2008 Davidhan #All Rights Reserved. #program date 2008-6-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: test connectiong status of oracle #2: from ini.txt get information of shell start paramater #3: auto export oracle dump everyday #4: auto delete X days ago dump files that setted in config file #5: auto realese and send backup file to FTP server #6: log info #7: optical backup ############################################################################## mypwd='pwd' inifile=/home/backupOracle/init.txt oratab=/etc/oratab TEMP=temp.$$ DUMP_FILE=`date +%Y%m%d`
initme () { #read from the config file 'init.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. }
#run the init parameter test ! -e $inifile && echo "not find the init file:"$inifile"!" && exit 0
initme
echo "=========$LOGS=========" echo "=========$USERNAME=========" echo "=========$PASSWORD=========" echo "=========$MY_ALIAS=========" echo "=========$MYHOST_IP=========" echo "=========$MYHOST_USERNAME=========" echo "=========$MYHOST_PASSWORD========="
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
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 ORACLE_BASE=/home/oracle/oracle export ORACLE_BASE export ORACLE_SID export ORACLE_HOME PATH=$PATH:$ORACLE_HOME/bin export PATH LIBPATH=$LIBPATH:$ORACLE_HOME/lib export LIBPATH ORA_NLS33=$ORACLE_HOME/nls/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 trap "" 1 2 3 15
if [ "$TACTIC" != "Y" -o "$TACTIC" != "y" ]; then echo "<<system will run at the way of user!..............................>>" | $LOGME $ORACLE_HOME/bin/exp $USERNAME/$PASSWORD@$MY_ALIAS buffer=40960 file=$DUMP_DIR/$DUMP_FILE.dmp full=y Log=$DUMP_DIR/$DUMP_FILE.log
#optical backup
else echo "<<system will run at the way of optical user!..............................>>" | $LOGME MYWEEK='date | awk '{print $1}'' case $MYWEEK in #1 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 ;; #2 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 ;; #3 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 ;; #4 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 ;; #5 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 ;; #6 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 ;; #7 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
#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/sqlplus ]; then sqlplus $USERNAME/$PASSWORD@$MY_ALIAS<<EOF 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 sqlplus program or cant not execute and get the days is fail..............!>>" | $LOGME exit $? fi
##################### ## ## Begin to remove the dump file ## #####################
TEMPDATE=`cat $TEMP | sed '1d' | sed '$d' | sed '/^--*/d'` echo "<<date:"$TEMPDATE"..............!>>" | $LOGME 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_DIR/$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
代码部分结束。
可按照init文件中的内容设定工作目录和工作的文件内容。
现在是操作手顺:
1. 在将backup.sh和init.txt文件拷贝到 /home/backupOracle文件夹内。
说明:/home/backupOracle目录不存在可运行 #mkdir /home/backupOracle命令创建。
2. 创建工作目录。 工作日志文件存放目录:#mkdir /home/oracle/backupLog
生成备份文件存放目录:#mkdir /home/oracle/oraclebackup
3. 创建定时工作表。
l #vi confile
l 进入定时工作表编辑器
l 点击键盘insert按键,调整编辑器状态至插入
l 输入:10 4 * * * /home/backupOracle/backup.sh(意为备份任务执行的时间为每天4点10分)
l 点击键盘ESC按键推出编辑状态
l Shift +z+z(键盘输入两个大写Z)保存退出编辑器
l #crontab confile(将任务注入到crontab进程)。此时可在 /var/spool/cron文件夹中生成名为root的工作表
l #crontab -l 可查看到工作表的内容
通过以上步骤执行,我们为系统定制了每天4点10分执行的备份任务。工具采用全局备份,导出除ORDSYS,MDSYS,CTXSYS,ORDPLUGINS,LBACSYS 这些系统用户之外的所有用户的数据。在oraclebackup文件夹下生成命名为当日时间(格式为YYYYDDMM)的dmp文件的tar压缩文件,并在指定远程ftp备份主机中保留备份文件,工具可在执行时检测备份本地备份文件夹,自动删除2日前的备份的文件。这些都在init.txt文件中设定。
还原数据库操作手顺
l #cd /home/oracle/oraclebackup
l #tar zxvf YYYYDDMM.tar.z
l #imp user/password@oracle_sid file=/home/oracle/oraclebackup/home/ oracle/oraclebackup/YYYYDDMM.dmp full=y ignore=y
以上是备份的全部内容,实现了备份数据库,定时,ftp远程备份。
注意:没有linux基础的同学不太了解linux下的命令组成
提醒一点,不要把“`”看成“'”,一个是键盘上ESC对应那个符号。这个在linux中指命令域。
发表评论
-
Linux redhat4中Oracle10g自动备份设置
2012-08-15 14:17 9631.运行环境 Linux redhat4,Oracle10g ... -
linux 下定时备份Oracle数据库
2012-08-02 10:03 1485备份Oracle数据库 Linux: cron ... -
创建的定时任务为什么没有按时执行呢?
2012-08-01 10:10 10821.确保Oracle的工作模式允许启动任务队列管理器 Ora ... -
Oracle Job 语法和时间间隔的设定
2012-08-01 10:08 849-------------------------- 初始化 ... -
Oracle截取字符串的函数substr
2011-10-29 14:49 1407导读]截取字符串的初始位置, Number型,start_po ...
相关推荐
### Linux下Oracle数据库自动备份方案详解 #### 一、背景介绍 随着企业信息化程度的不断提高,数据的安全性和稳定性显得尤为重要。对于使用Oracle数据库的企业来说,定期进行数据备份是确保业务连续性和数据安全的...
【Linux下ORACLE数据库自动备份】的实现方法主要涉及以下几个关键知识点: 1. **环境准备**:在Linux系统,如Redhat或CentOS等64位环境下,首先需要确保Oracle数据库服务正常运行,并且拥有足够的磁盘空间进行备份...
Linux下Oracle数据库自动备份迁移 通过linux下Crontab实现定时任务,结合oracle自动备份脚本实现oracle数据库的备份操作,结合linux下Scp服务实现备份文件的异地存储,从而减少了人为备份的繁琐工作和服务器损坏造成...
Linux下实现Oracle数据库自动备份的脚本
在Linux环境下,远程定时备份Oracle数据库是一项至关重要的任务,它能确保数据的安全性和业务的连续性。Oracle数据库作为企业级的数据库管理系统,其备份与恢复策略必须严谨且可靠。以下将详细阐述如何在没有安装...
"oracle数据库自动备份上传至FTP服务器" Oracle 数据库自动备份上传至 FTP 服务器是指通过 Shell 脚本来实现对 Oracle 数据库的自动备份,并将备份文件上传至 FTP 服务器上,以防服务器出现故障,导致数据库无法...
Linux 环境下 Oracle 数据库的备份和迁移 Oracle 数据库是当前最流行的关系数据库管理系统之一,而 Linux 环境下 Oracle 数据库的备份和迁移是非常重要的维护工作。本文将详细介绍 Linux 环境下 Oracle 数据库的...
Oracle数据库备份脚本文件在Linux平台下的实现 在 Linux 操作系统中,Oracle 数据库的备份是至关重要的一步骤,以确保数据的安全和完整性。为实现 Oracle 数据库的备份,我们可以使用脚本文件来自动化备份过程。在...
### Linux环境下Oracle数据库备份 #### 1. 备份用户数据 手动备份Oracle数据库的一个基本步骤是备份用户数据。下面是一系列详细的步骤: - **步骤1**:使用Linux系统下的数据库管理员账号连接到Linux终端。 - **...
"oracle数据库自动备份脚本带说明"的标题表明这是一个关于如何自动化执行Oracle数据库备份的脚本,这对于系统管理员来说是非常实用的工具。下面我们将详细探讨Oracle数据库的备份策略、自动备份脚本的工作原理以及...
Linux 下 Oracle 11g 数据库冷备份异机还原是指在 Linux 操作系统下,对 Oracle 11g 数据库进行冷备份,并将备份的数据文件还原到另外一台主机上,以便在源主机上进行数据库升级或维护时,能够快速恢复数据库服务。...
本资料"oracle数据库自动备份与恢复.zip"包含了在Linux和Windows操作系统下,对Oracle 11g R2数据库进行自动备份与恢复的方法。 首先,我们来看Linux环境下的Oracle数据库自动备份。在Linux系统中,通常使用RMAN...
### Linux 下 Oracle 11g 数据库冷备份与异机还原详解 #### 实验背景及目的 在进行系统维护或升级之前,为了保障数据的安全性和业务连续性,通常需要对现有生产环境中的数据库进行备份。当涉及到Oracle 11g数据库的...
Linux 下 Oracle 的数据定时自动备份 Linux 下 Oracle 的数据定时自动备份是系统管理员每天都在做着同一样的工作。为避免数据丢失,需要进行数据备份。本文将结合实践经验谈一谈 UNIX 环境下 Oracle 数据库的自动...
在Oracle数据库备份中,exp工具是一种逻辑备份方法,它导出数据库对象的SQL语句,生成dmp文件。这种方法适用于较小规模的数据备份,或者当只关心特定对象时。与物理备份(如RMAN)相比,逻辑备份占用的空间较大,但...
首先,理解Oracle数据库备份的基本概念是必要的。Oracle提供多种备份方式,包括物理备份(如数据文件、控制文件、重做日志文件的拷贝)和逻辑备份(如使用SQL*Plus的EXPDP和IMPDP工具进行数据导出导入)。在Java环境...
Centos7.6 下 Oracle 定时自动备份和 sudo 设置 本文将详细介绍 Centos7.6 下 Oracle 定时自动备份和 sudo 设置的过程。该过程包括编写数据库备份脚本文件、设置定时、sudo 设置等多个方面。 编写数据库备份脚本...
在Oracle数据库管理中,"Linux Oracle数据库设置自动归档"是一个关键操作,它涉及到数据库的安全性和高可用性。归档日志(Archived Logs)是Oracle数据库中的一个重要组成部分,它们记录了事务处理的历史,用于在...
标题“Oracle数据库自动备份压缩bat”表明我们正在讨论一个用于自动化Oracle数据库备份并将其压缩成BAT(批处理)脚本的方案。Oracle数据库是企业级的关系型数据库管理系统,确保其数据的安全性和可恢复性至关重要。...
Linux-oracle数据库的备份与恢复.pdf