欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用shell腳本實現(xiàn)自動切換內網和外網實現(xiàn)高可用

 更新時間:2013年06月10日 14:11:08   作者:  
因為公司的服務器這段時間內網網卡老是出現(xiàn)問題,然后就導致了網站的各種問題,因為我們的數據庫鏈接,redis還有sphinx鏈接都是走的內網,,所以就寫了這個腳本,在線上也跑了一段時間了,,也沒出過什么問題

首先說明下我們的配置文件,都是類似格式的,假如內網是192.168.0.3,外網是123.123.123.123,配置文件如下:

$db['salver']['hostname'] = '192.168.0.3:3306';
//$db['salver']['hostname'] = '123.123.123.123:3306';

這個時候就是外網是注釋的狀態(tài)。走的是內網。
說下思路,就是一個配置文件,復制出來2分放到別的地方,這兩份一個里面設置的是內網,一個里面設置的是外網,如果內網不通的情況下,判斷當前用的是內網還是外網,如果是內網的情況下,就把外網的配置文件復制到網站文件里,如果內網通了,就把內網的配置文件再復制到網站文件里。就相當于是內網IP是主,外網IP是備一樣。把這個腳本放在計劃任務里,1分鐘執(zhí)行一次。

復制代碼 代碼如下:

#!/bin/bash
#主從數據庫的內外網IP
master_IP_N=
master_IP_W=
salver_IP_N=
salver_IP_W=
#mongo的內外網IP
mongo_IP_W=
mongo_IP_N=
#redis的主從內外網IP
redis_master_IP_W=
redis_master_IP_N=
redis_slave_IP_W=
redis_slave_IP_N=
#sphinx的內外網IP
sphinx_IP_W=
sphinx_IP_N=
#網站配置文件的位置
database_path=/home/caoy/database.php
mongo_path=/home/caoy/over_sea_stock.php
redis_path=/home/caoy/Rediska_connector.php
sphinx_path=/home/caoy/SphinxConnector.php
config_path=/data/check_ip_config/
#檢測當前使用的網卡
function check_ip(){
IP_config=`cat $1  |grep $2 | cut -c1-2`
if [ "$IP_config" == "http://" ];then
        ip_status=1
else
        ip_status=0
fi
}
#判斷內網網卡是否故障
function check_network(){
nmap -sP $1 | grep "1 host up" >/dev/null || (sleep 5 ; nmap -sP $1) | grep "1 host up" >/dev/null
#ping $1 -c 5 > /dev/null
if [[ "$?" == "1" ]]; then
        check_ip $database_path $1
        if [[ "$ip_status" == "0" ]];then
                cp -r $config_path/database_w.php $database_path
                echo "cp database_w.php is ok!"
        fi
        check_ip $mongo_path $1
        if [[ "$ip_status" == "0" ]];then
                cp -r $config_path/over_sea_stock_w.php $mongo_path
                echo "cp over_sea_stock_w.php is ok!"
        fi
        check_ip $redis_path $1
        if [[ "$ip_status" == "0" ]];then
                cp -r $config_path/Rediska_connector_w.php $redis_path
                echo "cp Rediska_connector_w.php is ok!"
        fi
        check_ip $sphinx_path $1
        if [[ "$ip_status" == "0" ]];then
                cp -r $config_path/SphinxConnector_w.php $sphinx_path
                echo "cp SphinxConnector_w.php is ok!"
        fi
else
        check_ip $database_path $1
        if [[ "$ip_status" == "1" ]];then
                cp -r $config_path/database_n.php $database_path
                echo "cp database_n.php is ok!"
        fi
        check_ip $mongo_path $1
        if [[ "$ip_status" == "1" ]];then
                cp -r $config_path/over_sea_stock_n.php $mongo_path
                echo "cp over_sea_stock_n.php is ok!"
        fi
        check_ip $redis_path $1
        if [[ "$ip_status" == "1" ]];then
                cp -r $config_path/Rediska_connector_n.php $redis_path
                echo "cp Rediska_connector_n.php is ok!"
        fi
        check_ip $sphinx_path $1
        if [[ "$ip_status" == "1" ]];then
                cp -r $config_path/SphinxConnector_n.php $sphinx_path
                echo "cp SphinxConnector_n.php is ok!"
        fi
fi
}
#檢測各個網卡是否通暢
check_network $master_IP_N
check_network $salver_IP_N
check_network $mongo_IP_N
check_network $redis_master_IP_N
check_network $redis_slave_IP_N
check_network $sphinx_IP_N

相關文章

最新評論