首页 > 数据库 > Oracle > 通过传送归档日志在备库恢复
公司由于网络原因,不能使用oracle提供的主备库配置同步主库备库数据,前辈通过操作系统层面定期传送归档日志解决了此问题,对然脚本有瑕疵,但也很稳定。
首先在10.*.*.*6登录oracle用户 执行:
$ crontab –l;
17 * * * * /home/oracle/standby/gzip_ftp_standby.sh > /dev/null 2>&1
注: > /dev/null 2>&1 /dev/null 为linux下的回收站,> 默认时把标准输出重定向
2>&1 时把出错输出也定向到标准输出
$ cat gzip_ftp_standby.sh ;
#! /bin/bash
. /home/oracle/.bash_profile
set –x #开启脚本调试,把下面的命令打印到屏幕上
cd /u01/app/oracle/archive
ARCHTOTAL = ls *.dbf | wc –l
If[[ $ARCHTOTAL –lt 3 ]] #为啥归档总数小于3就退出?
Then
exit 0
fi
cd /u01/app/oracle/archive/zip/
ls –l|grep dbf|wc –l|read UNZPFILE
if [[ $UNZPFILE –gt 0 ]]
then
exit 0
fi
ps –ef|grep ftp_standby_arch.sh|grep –v grep|wc –l|read SHFTP
if [[ $SHFTP –gt 0 ]]
then
exit 0
fi
ps –ef|grep gzip|grep –v grep|wc –l |read GZFILE
if[[ $GZFILE –gt 0 ]]
then
exit 0
fi
mv *.gz /u01/app/pracle/archive/gz
cd /u01/app/oracle/archive
ARCHMV = ‘expr $ARCHTOTAL – 3 ‘ #总共的归档日志减去3要干嘛
mv ‘ls –rt *.dbf | head -$ARCHMV ’ /u01/app/oracle/archive/zip/
cd /u01/app/oracle/archive/zip/
gzip 1_*.dbf
/home/oracle/standby/ftp_standby_arch.sh
$ cat /home/oracle/standby/ftp_standby_arch.sh
#! /bin/sh
. /home/oracle/.bash_profile
LOG=/home/oracle/standby/autoftp.log
Bak_Server_IP=10.*.*.*
ftpuname=*
ftppwd=*
remotedir = /data/app/oracle/archive
localdir = /u01/app/oracle/archive/zip
starttime= ‘date +%y-%m-%d-%T’
echo “………………………..FTP DELETE And Translate to $BAK_SERVER_IP ……………” >$log
echo “………………………..start $starttime…………………………………………………………..” >>$log
ftp
–d –I –n >> $LOG 2>>$LOG <
open $BAK_SERVE_IP
user $ftpuname $ftppwd
bi #将文档传送类型配置为二进制
cd $remotedir
lcd $localdir #更改本地电脑的工作目录
mput *.gz
bye
EOF
endtime= ‘date +%y-%m-%d-%T’
echo “………………………..end $endtime…………………………………………………………..”>>$log
0 4,12,23 * * * /home/oracle/scrpt/del_archive.log
$ cat del_archive.sh // 怎样确保删除的是正确的?
./home/oracle/.bash_profile
cd /u01/app/oracle/archive
find . –mitme +3 | xargs rm #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
在备库端:*** 端执行crontab –l
0,9,20 * * * /home/oracle/standby/recover_standby.sh
$ cat /home/oracle/standby/recover_standby.sh
#! /bin/sh
./home/oracle/.bash_profile
set –x
cd /data/app/oracle/archive
mypid=$$ # $$ 是脚本运行的当前进程ID号
ps –ef|grep recovery_standby_sh|grep –v grep|grep –v $mypid
if [ $? –eq 0 ]; then #$? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误
# echo “recover_standby.sh is running”
Exit
Fi
cnt = ’ls *.gz|wc –l’
#echo $cnt
If [ $cnt –gt 0 ]; then
gunzip –f *.gz
sqlplus /nolog << ~
conn / as sysdba
recovery automatic standby database;
cancel
exit
/home/oracle/standby/del_arch.sh
fi
unset cnt
$cat /home/oracle/standby/del_arch.sh
#!/bin/sh
. /home/oracle/.bash_profile
echo “select max(sequence #) from v\$log_history where thread#=1;” > /home/oracle/standby/last_seq.sql #为啥thread#=1
ARCH_DEST=/data/app/oracle/archive
FILE_LIST=/home/oracle/standby/file_list
Sqlplus –s /nolog << !
conn / as sysdba
set heading off
set echo off
spool /home/oracle/standby/del_last.log
@/home/oracle/standby/last_sql.sql
Spool off
exit
!
STANDBY_LAST_SEQ=’ cat /home/oracle/standby/del_last.log |grep [0123456789] ’
cd $ARCH_DEST
ls 1_*.dbf > $FILE_LIST
cat $FILE_LIST | while read LINE
do
FILE_NAME=$LINE
FILE_NO=’ echo $FILE_NAME|awk –F. ‘{pirnt $1}’ | awk –F_ ‘{pirnt $2}’ ‘
If [ $FILE_NO –lt STANDBY_LAST_SEQ ]; then rm $FILE_NAME
Fi
done
exit
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29844843/viewspace-1328501/ ,如需转载,请注明出处,否则将追究法律责任。