下午部署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

相关文章 随机文章

    10 Responses to “下午部署DG,顺手写了几个DG维护脚本”


    1. 1zhouyf

      很有帮助!

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

    2. 2kevin.yuan

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

    3. 3zhouyf

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

      你方便时可以确认一下。

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

    4. 4kevin.yuan

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

    5. 5thompsun

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

    6. 6majuthink

      这个脚本运行到
      [oracle@ora2 ~]$ ./rman_script.sh
      RMAN>
      就停住了,要向手动退出,并报如下错误,但在rman中执行执行如下指令却没有问题,望kevin.yuan 指正
      RMAN> exit
      rman_delete_arch.sh: line 2: crosscheck: command not found
      rman_delete_arch.sh: line 3: delete: command not found

    7. 7jackyhu999

      楼上兄弟,把“”替换成 “就可以了

    8. 8jackyhu999

      上面显示不出汉字的双引号,另外我机器执行的时候还有加上set trimspool on这个参数去掉spool所产生的拖尾空格,还有就是v$database的返回值,我的是”PHYSICAL STANDBY”

    9. 9jackyhu999

      majuthink :
      你的问题我试验了一下,用这个可以通过。
      source /home/oracle/.bash_profile
      Dir=/home/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 <> rman_delete_arch.sh
      echo “>> EOF;” >> rman_delete_arch.sh
      echo “!” >> rman_delete_arch.sh
      sed -i ‘/^$/d’ rman_delete_arch.sh #应该是要删rman的最后一行,用sed -i ‘$d’ rman_delete_arch.sh
      ##delete the archived logs
      sh rman_delete_arch.sh

    10. 10jackyhu999

      kevin.yuan 大侠你好。感谢你的blog,让我学到很多。但是很多复制上去的脚本丢失了字符,上面的就丢失了
      echo “rman target / log=rman_delete_arch.log 这句话后面有两个》加EOF 没有显示出来。

      rman命令执行下面的语句要用EOF包含起来。

    Leave a Reply