使用shell脚本快速得到主备关系(r9笔记第93天)

时间:2022-05-04
本文章向大家介绍使用shell脚本快速得到主备关系(r9笔记第93天),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

对于备库的使用,尤其是一主多备的环境,一直以来有一点感觉不大给力,那就是主备库的关系,总是感觉会少一点什么。 尤其是在做月度404审计的时候,总是要反复确认备库的IP。如果是手工管理的场景中,基本就是查看log_archive_config的配置,也还需要解析里面的TNS配置。如果配置了DG Broker,可能情况会好些,输出的关系是还是比较清楚的。 Configuration - dg_test Protection Mode: MaxPerformance Databases: test - Primary database stest1 - Physical standby database stest2 - Physical standby database Fast-Start Failover: DISABLED Configuration Status: SUCCESS 但是这里有个问题就是备库的IP是哪个,可能tnsping的结果是主机名的情况,那么还需要再转义一次,到/etc/hosts里面去匹配。 这样下来,其实这些工作有多难,还真没有,但是重复的手工操作太多,实在让人感觉有种鸡肋的味道。 既然搭建Data Guard都能实现半自动化了,这个功能不实现还是有些说不过去。 这个输出应该把备库的主机名,IP都罗列出来,如果只是配置了IP,就显示IP和数据库的db_unique_name并列 这样一个数据库的大体情况就会一目了然,包括一些必备的参数等。 而运行脚本只需要输入主库的IP即可 比如sh checkdb2.sh 10.127.xxx.xx 改进后的输出类似下面的形式: 10.127.xxx.xx . Databases: test - Primary database stest1 - Physical standby database stest2 - Physical standby database . 10.127.133.16 stest1.cyou.com stest1 10.127.2.3 stest2.cyou.com stest2 NAME DB_UNIQUE_NAME DATABASE_ROLE FORCE_LOGG -------------------- -------------------- ---------------- ---------- TEST test PRIMARY YES NAME VALUE ------------------------------ -------------------------------------------------- db_file_name_convert db_name test db_unique_name test dg_broker_start TRUE local_listener TEST log_file_name_convert standby_file_management AUTO checkdb2.sh的脚本内容如下: base_dir=/U01/oracle/auto_dg ip=$1 . ${base_dir}/autodg.cnf echo $ip ORACLE_HOME=`ssh $ip "cat /etc/oratab | tail -1 | awk -F: '{print \$2}'" 2>&1 ` ssh $ip "mkdir -p /home/oracle/auto_dg/scripts" scp -r ${base_dir}/scripts/* $ip:/home/oracle/auto_dg/scripts >/dev/null 2>&1 ssh $ip " sh /home/oracle/auto_dg/scripts/getdbinfo.sh" 2>&1 |grep -v stty 这个脚本本身逻辑很简单,关键是两个辅助脚本。 一个是getdbinfo.sh,脚本在/home/oracle/auto_dg/scripts下 内容如下: . /home/oracle/.bash_profile chown -R oracle:oinstall /home/oracle/auto_dg sudo su -l oracle <<eof echo . dgmgrl -silent / " show configuration" |grep -i database echo . sh /home/oracle/auto_dg/scripts/get_dg_host.sh EOF sudo su -l oracle <<eof sqlplus -s / as sysdba <<eos col name format a20 col DB_UNIQUE_NAME format a20 col force_logging format a10 col value format a50 set linesize 150 set feedback off select name,db_unique_name,database_role,force_logging from v\$database; col name format a30 select name,value from v\$parameter where upper(name) in ('DB_NAME','DB_UNIQUE_NAME','LOCAL_LISTENER','DG_BROKER_START','STANDBY_FILE_MANAGEMENT','DB_FILE_NAME_CONVERT','LOG_FILE_NAME_CONVERT') order by name; EOS echo . tnsping $1|grep ADDRESS|sed 's/Attempting to contact //' > /home/oracle/auto_dg/tns_port.lst EOF 而最为关键的脚本则是get_dg_host.sh,脚本内容如下: for tmp_ins in `dgmgrl -silent / " show configuration" |grep -i "Physical standby database"|sed 's/Physical standby database//g'|sed 's/-//g'` do tmp_host=`tnsping ${tmp_ins}|grep ADDRESS|sed 's/Attempting to contact //'|sed 's/(/n/g'|sed 's/)/n/g'|grep -i 'HOST|PORT|SERVICE_NAME|SID_NAME' |grep -i HOST| awk -F= '{print $2}'` #echo ${tmp_host} tmp_port=`tnsping ${tmp_ins}|grep ADDRESS|sed 's/Attempting to contact //'|sed 's/(/n/g'|sed 's/)/n/g'|grep -i 'HOST|PORT|SERVICE_NAME|SID_NAME' |grep -i PORT| awk -F= '{print $2}'` #echo ${tmp_port} tmp_con_chk=`nc -w 1 -v ${tmp_host} ${tmp_port}` #echo ${tmp_con_chk} if [[ ${tmp_con_chk} =~ "succ" ]]; then echo `grep ${tmp_host} /etc/hosts|awk '{print $1}'`" " ${tmp_host} " " ${tmp_ins} else echo 'NEED TO CHECK HOST '${tmp_host} fi done 这个脚本里面会做复杂的校验,最终能够生成我们期望的数据行。