容災半自動化的實現思路(二)

jeanron100發表於2016-01-29
容災的半自動化的部分,自己寫了下面的指令碼,也算是一個基本實現,因為時間倉促,還是存在一些不足,稍後完善
整個切換的步驟分為三部分,第一部分是備份當前備庫的配置檔案,第二部分是使用dg broker決定是failover還是switchover 第三部分是切換之後開始替換引數檔案。
所以需要的指令碼為dg_pre.sh  dg_post.sh
dg_pre.sh是作為基本的備份,內容如下:
#### validate input parameters
#### validate if ip address is available
#### validate if ip address is the same as standby

#### get current ip address
curr_ipadd=`cat /etc/hosts|grep \`hostname\`|awk '{print $1}'`
echo '###### INFO  Current IP ADDRESS IS : '$curr_ipadd
echo .
ip_std_bak=${curr_ipadd}_std_bak
##get network card name
eth_code=`/sbin/ifconfig |grep -B1 ${curr_ipadd}|head -1|awk '{print $1}'`
echo '###### INFO  Current NETWORK CARD NAME IS : '$eth_code
/sbin/ifconfig|grep  -B1 `cat /etc/hosts|grep \`hostname\`|awk '{print $1}'`
echo .
sleep 2
#### backup all conf files
mkdir -p /home/conf/${ip_std_bak};
ORACLE_HOME=`cat /etc/oratab | tail -1 | awk -F: '{print \$2}' `
echo '###### INFO  Current ORACLE_HOME is : '$ORACLE_HOME
echo .
sleep 1
if [ -n "$ORACLE_HOME" ]; then
cp $ORACLE_HOME/network/admin/tnsnames.ora    /home/conf/${ip_std_bak}
cp $ORACLE_HOME/network/admin/listener.ora    /home/conf/${ip_std_bak}
cp $ORACLE_HOME/network/admin/sqlnet.ora    /home/conf/${ip_std_bak}
cp $ORACLE_HOME/dbs/init*.ora   /home/conf/${ip_std_bak}
cp $ORACLE_HOME/dbs/spfile*.ora   /home/conf/${ip_std_bak}
cp $ORACLE_HOME/dbs/orapw*   /home/conf/${ip_std_bak}
echo '###### INFO  BACKUP DB LEVEL CONF FILES ...  DONE'
fi
sleep 1
echo .
cp /etc/hosts   /home/conf/${ip_std_bak}
cp /etc/sysconfig/iptables   /home/conf/${ip_std_bak}
cp /etc/sysconfig/network   /home/conf/${ip_std_bak}
cp /etc/sysconfig/network-scripts/ifcfg-${eth_code} /home/conf/${ip_std_bak}
cp /etc/sysctl.conf /home/conf/${ip_std_bak}
cp /etc/security/limits.conf /home/conf/${ip_std_bak}
echo '###### INFO  BACKUP OS LEVEL CONF FILES ... DONE'
sleep 1
####

dg_post是作為切換之後的補充,指令碼如下:
#### validate input parameters
new_ip=$1
if [ ! $new_ip ];then
echo 'Target IP ADDRESS is needed ... '
exit;
fi
if [ ! -d /home/conf/$new_ip ];then
echo 'TARGET IP CONF FILES NOT FOUND,SHOULD BE /home/conf/'$new_ip
exit;
fi
#### validate if ip address is available


#### get current ip address
curr_ipadd=`cat /etc/hosts|grep \`hostname\`|awk '{print $1}'`
echo '###### INFO  Current IP ADDRESS IS : '$curr_ipadd
sleep 1 ;
if [ ! -d /home/conf/$curr_ipadd ];then
echo 'SOURCE IP CONF FILES NOT FOUND,SHOULD BE /home/conf/'${curr_ipadd}_std_bak
exit;
fi

#### validate if ip address is the same as standby
#if [[ ${new_ip} -eq ${curr_ipadd} ]];then
#echo 'SOURCE AND TARGET IP ADDRESS SHOULD NOT BE THE SAME ...'
#fi
echo .
ip_std_bak=${curr_ipadd}_std_bak
##get network card name
eth_code=`/sbin/ifconfig |grep -B1 ${curr_ipadd}|head -1|awk '{print $1}'`
echo '###### INFO  Current NETWORK CARD NAME IS : '$eth_code
/sbin/ifconfig|grep  -B1 `cat /etc/hosts|grep \`hostname\`|awk '{print $1}'`
if [ ! $eth_code ];then
echo 'WARNING... NETWORK CARD CODE IS EMPTY ...'
exit
fi
sleep 1 ;
echo .
#### backup all conf files
mkdir -p /home/conf/${ip_std_bak};
ORACLE_HOME=`cat /etc/oratab | tail -1 | awk -F: '{print \$2}' `
echo '###### INFO  Current ORACLE_HOME is : '$ORACLE_HOME
echo .

if [ -n "$ORACLE_HOME" ]; then
echo '###### INFO  RESTORE DB LEVEL CONF FILES'
echo .
cp /home/conf/${new_ip}/tnsnames.ora $ORACLE_HOME/network/admin/tnsnames.ora   
cp /home/conf/${new_ip}/listener.ora $ORACLE_HOME/network/admin/listener.ora    
cp /home/conf/${new_ip}/sqlnet.ora   $ORACLE_HOME/network/admin/sqlnet.ora  > /dev/null   2>&1
fi
sleep 1 ;

echo '###### INFO  RESTORE OS LEVEL CONF FILES'
echo .
cp /home/conf/${new_ip}/hosts /etc/hosts   
cp /home/conf/${new_ip}/iptables /etc/sysconfig/iptables  
cp /home/conf/${new_ip}/network /etc/sysconfig/network  
sleep 1 ;

####change IP address
echo '###### INFO  IP ADDRESS BEFORE CHANGE AS BELOW'
cat /home/conf/${ip_std_bak}/ifcfg-${eth_code} |tee /home/conf/tmp_eth.lst
sed -i "s/${curr_ipadd}/${new_ip}/g"   /home/conf/tmp_eth.lst
sleep 1;
echo .

echo '###### INFO  IP ADDRESS CHANGED AS BELOW'
cat /home/conf/tmp_eth.lst
cp /home/conf/tmp_eth.lst /etc/sysconfig/network-scripts/ifcfg-${eth_code}
cp /home/conf/$new_ip/network /etc/sysconfig/network
sleep 1;
echo .

new_hostname=`cat /etc/hosts|grep $new_ip|awk '{print $2}'`
echo $new_hostname
sleep 1;
hostname $new_hostname
ifdown ${eth_code}
sleep 1;
ifup ${eth_code}
sleep 1;
service network restart

dg_pre.sh執行的基本日誌如下:
###### INFO  Current IP ADDRESS IS : 10.127.133.46
.
###### INFO  Current NETWORK CARD NAME IS : eth0
eth0      Link encap:Ethernet  HWaddr 84:2B:2B:69:EF:F8  
          inet addr:10.127.133.46  Bcast:10.127.133.255  Mask:255.255.255.0
.
###### INFO  Current ORACLE_HOME is : /U01/app/oracle/product/11.2.0.2/db_1
.
###### INFO  BACKUP DB LEVEL CONF FILES ...  DONE
.
###### INFO  BACKUP OS LEVEL CONF FILES ... DONE

dg_post.sh的日誌如下:

# cat dg_post.log
###### INFO  Current IP ADDRESS IS : 10.127.133.46
.
###### INFO  Current NETWORK CARD NAME IS : eth0
eth0      Link encap:Ethernet  HWaddr 84:2B:2B:69:EF:F8  
          inet addr:10.127.133.46  Bcast:10.127.133.255  Mask:255.255.255.0
.
###### INFO  Current ORACLE_HOME is : /U01/app/oracle/product/11.2.0.2/db_1
.
###### INFO  RESTORE DB LEVEL CONF FILES
.
###### INFO  RESTORE OS LEVEL CONF FILES
.
###### INFO  IP ADDRESS BEFORE CHANGE AS BELOW
DEVICE=eth0
BOOTPROTO=static
IPADDR=10.127.133.46
NETMASK=255.255.255.0
#GATEWAY=10.127.133.254
ONBOOT=yes
TYPE=Ethernet
.
###### INFO  IP ADDRESS CHANGED AS BELOW
DEVICE=eth0
BOOTPROTO=static
IPADDR=10.127.133.45
NETMASK=255.255.255.0
#GATEWAY=10.127.133.254
ONBOOT=yes
TYPE=Ethernet
.
BX_133_45
Shutting down interface eth0:  [  OK  ]
Shutting down interface eth1:  [  OK  ]
Shutting down loopback interface:  [  OK  ]
Bringing up loopback interface:  [  OK  ]
Bringing up interface eth0:  [  OK  ]
Bringing up interface eth1:  [  OK  ]
後續繼續完善。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23718752/viewspace-1984190/,如需轉載,請註明出處,否則將追究法律責任。

相關文章