首先說明下我們的配置文件,都是類似格式的,假如內網是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
您可能感興趣的文章:- 分享個簡易版Linux服務器初始化Shell腳本
- Linux shell腳本基礎學習詳細介紹(完整版)
- 利用Shell腳本實現遠程MySQL自動查詢
- linux服務器安全加固shell腳本代碼
- 如何調試Linux shell腳本
- linux下監視進程 崩潰掛掉后自動重啟的shell腳本
- 自動重啟服務的shell腳本代碼
- linux shell腳本基礎知識學習
- 學習shell腳本之前的基礎知識[圖文]
- linux中mysql備份shell腳本代碼
- Linux下使用Shell腳本實現ftp的自動上傳下載的代碼小結
- 獲取兩個日期間隔時間的shell腳本代碼
- 判斷文件是否存在的shell腳本代碼
- kill特定進程的shell腳本代碼
- 查找目錄下同名但不同后綴名文件的shell腳本代碼
- 在指定目錄查找指定后綴文件的shell腳本代碼
- 統計網卡流量的兩段shell腳本(使用ifconfig)
- shell腳本作為保證PHP腳本不掛掉的守護進程實例分享