国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Linux Shell > 正文

用shell腳本實現自動切換內網和外網實現高可用

2020-07-27 19:23:00
字體:
來源:轉載
供稿:網友

首先說明下我們的配置文件,都是類似格式的,假如內網是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" == "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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平江县| 贵州省| 宁陕县| 广水市| 灵石县| 鸡西市| 乐安县| 新巴尔虎右旗| 库车县| 广东省| 宾川县| 玉山县| 枣阳市| 读书| 龙川县| 灌南县| 东至县| 荣昌县| 平乐县| 罗田县| 宜黄县| 高台县| 贺兰县| 合阳县| 临泉县| 泾源县| 华容县| 昌江| 建宁县| 温泉县| 深泽县| 东海县| 乐安县| 临城县| 盐山县| 台北市| 池州市| 湖州市| 金秀| 大埔区| 二连浩特市|