用shell指令碼實現自動切換內網和外網實現高可用_linux shell

來源:互聯網
上載者:User

首先說明下我們的設定檔,都是類似格式的,假如內網是192.168.0.3,外網是123.123.123.123,設定檔如下:

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

這個時候就是外網是注釋的狀態。走的是內網。
說下思路,就是一個設定檔,複製出來2分放到別的地方,這兩份一個裡面設定的是內網,一個裡面設定的是外網,如果內網不通的情況下,判斷當前用的是內網還是外網,如果是內網的情況下,就把外網的設定檔複製到網站檔案裡,如果內網通了,就把內網的設定檔再複製到網站檔案裡。就相當於是內網IP是主,外網IP是備一樣。把這個指令碼放在計劃任務裡,1分鐘執行一次。

複製代碼 代碼如下:

#!/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" == "//" ];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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.