下午部署DG,顺手写了几个DG维护脚本

下午给项目搭建了套DG,没啥好说的,顺手写了几个脚本,后面还需完善完善.
1.主备库开机自动启动(自动判断库运行模式,如果为Primary ,则启动到open状态,如果为Standby,则启动到recover managed standby database状态)

#!/bin/bash
#Author:Kevin.yuan
#Create_Time:2008-12-11
#Description:
#1.The script just put /etc/init.d/rc.local ==> su - oracle -c “/home/oracle/dbstart.sh
#2.The script is used to startup the datbase automatically when the OS is up.If the database is primary role,then “alter database open”, if the role is standby,then “recover managed standby database…”

#This function is used to get the database’s current role (Primary: 1 /Standby 2 Other(error) 3)
get_role()
{
sqlplus -s “/as sysdba” << !
startup mount;
spool /home/oracle/jurge_role.log
set head off
set feedback off
select database_role from v\$database;
spool off
exit;
!
role=`cat /home/oracle/jurge_role.log|tail -1`
if [ $role = "PRIMARY" ]
then
return 1
elif [ $role = "STANDBY" ]
then
return 2
else
return 3
fi
}
#—–main begin——
source /home/oracle/.bash_profile
lsnrctl start
get_role
role_stat=$?
if [ $role_stat -eq 1 ]
then
sqlplus -s “/as sysdba” << !
alter database open;
exit
!
elif [ $role_stat -eq 2 ]
then
sqlplus -s “/as sysdba” << !
alter database recover managed standby database disconnect from session;
exit
!
fi

2.定期自动删除主库/备库端已经在备库上apply过的归档日志,我不太喜欢用OS命令下的rm删除归档,容易出毛病,而且不灵便,还是喜欢调用Rman的delete archivelog命令.(适用于rac环境)

#!/bin/bash
#Create_time:2008.12.12
#Author:Kevin.yuan
#Description:Delete the archived logs which had applied on Standby database.
source /home/oracle/.bash_profile
Dir=/opt/oracle/script/standby
cd $Dir
echo “rman target / log=rman_delete_arch.log” > rman_delete_arch.sh
echo “crosscheck archivelog all;” >> rman_delete_arch.sh
sqlplus -s “sys/sys as sysdba” >> rman_delete_arch.sh << !
set head off
set feedback off
select ‘delete noprompt archivelog until logseq ‘||max(sequence#)||’ thread ‘||thread#||’;’ from v\$log_history where RESETLOGS_CHANGE#=(select resetlogs_change# from v\$database) group by thread#;
exit
!
echo “exit;” >> rman_delete_arch.sh
echo “!” >> rman_delete_arch.sh
sed -i ‘/^$/d’ rman_delete_arch.sh
##delete the archived logs
sh rman_delete_arch.sh

不过还是写了一个利用os的rm命令来删除的脚本.

#!/bin/bash
#Create_time:2008.12.11
#Author:Kevin.yuan
#Description:Delete the archived logs which had applied on Standby database Using os command.
source /home/oracle/.bash_profile
##Here is the primay/standby archive dest
Arch_dest=/archive/test
Sc_dir=/opt/oracle/script/standby
cd $Sc_dir
sqlplus -s “sys/sys@paybilldg as sysdba” > standby.log << !
set head off
set feedback off
select ‘1_’||a.SEQUENCE#||’_'||a.RESETLOGS_ID||’.dbf’ from v$archived_log a where a.APPLIED=’YES’ and first_time>sysdate-8;
exit
!
##
cat /dev/null > rm_arch.sh
for i in `ls -l $Arch_dest|grep ‘1_’|awk ‘{print $NF}’` ; do
for j in `cat standby.log` ; do
if [ $i = $j ]
then
if [ `grep $i rm_arch.sh|wc -l` -eq 0 ]
then
echo “rm -f $Arch_dest/”$i>>rm_arch.sh
fi
fi
done
done
sh rm_arch.sh

bookmark

相关文章 随机文章

5 Comments

zhouyf06月 23rd, 2010 at 18:42

很有帮助!

对于第二个用rman删除归档日志的脚本,我有一个疑问
就是当主库有日志未传送到standby上,就v$archived_log的apply=no时,而这时v$log_history已记录了这个归档日志,那么这时用rman去删 除的话,会把这个未传输的归档日志给删除了,
不知道是不是这样子
盼复!

kevin.yuan06月 24th, 2010 at 01:43

在standby上,已经接收但没有被应用的日志是不会被记录到v$log_history这个视图里面的,v$log_history记录的已经是被apply过的日志记录,所以不存在没有被apply的情况。
依据v$archived_log的apply字段判断,是不准确的

zhouyf06月 24th, 2010 at 17:09

我的环境是10.2.0.4.2
如果备库关闭, 这时主库发生alter system switch logfile ,这个新切换的归档日志,就会记录到v$log_history里。

你方便时可以确认一下。

如文档上看v$archive_log对apply的解释,我觉得用apply=y来判断 应该 是准确的。
--上面讲得我都是针对 自动删除主库的归档日志来说的

kevin.yuan06月 29th, 2010 at 09:50

老兄,v$log_history是指的备库上的,不是主库上的。
你主库上查主库的v$log_history有何意思啊。。。

thompsun07月 16th, 2010 at 16:19

呵呵,不错,赞一个
—西北

Leave a comment

Your comment