用shell腳本實(shí)現(xiàn)自動(dòng)切換內(nèi)網(wǎng)和外網(wǎng)實(shí)現(xiàn)高可用
首先說(shuō)明下我們的配置文件,都是類似格式的,假如內(nèi)網(wǎng)是192.168.0.3,外網(wǎng)是123.123.123.123,配置文件如下:
$db['salver']['hostname'] = '192.168.0.3:3306';
//$db['salver']['hostname'] = '123.123.123.123:3306';
這個(gè)時(shí)候就是外網(wǎng)是注釋的狀態(tài)。走的是內(nèi)網(wǎng)。
說(shuō)下思路,就是一個(gè)配置文件,復(fù)制出來(lái)2分放到別的地方,這兩份一個(gè)里面設(shè)置的是內(nèi)網(wǎng),一個(gè)里面設(shè)置的是外網(wǎng),如果內(nèi)網(wǎng)不通的情況下,判斷當(dāng)前用的是內(nèi)網(wǎng)還是外網(wǎng),如果是內(nèi)網(wǎng)的情況下,就把外網(wǎng)的配置文件復(fù)制到網(wǎng)站文件里,如果內(nèi)網(wǎng)通了,就把內(nèi)網(wǎng)的配置文件再?gòu)?fù)制到網(wǎng)站文件里。就相當(dāng)于是內(nèi)網(wǎng)IP是主,外網(wǎng)IP是備一樣。把這個(gè)腳本放在計(jì)劃任務(wù)里,1分鐘執(zhí)行一次。
#!/bin/bash
#主從數(shù)據(jù)庫(kù)的內(nèi)外網(wǎng)IP
master_IP_N=
master_IP_W=
salver_IP_N=
salver_IP_W=
#mongo的內(nèi)外網(wǎng)IP
mongo_IP_W=
mongo_IP_N=
#redis的主從內(nèi)外網(wǎng)IP
redis_master_IP_W=
redis_master_IP_N=
redis_slave_IP_W=
redis_slave_IP_N=
#sphinx的內(nèi)外網(wǎng)IP
sphinx_IP_W=
sphinx_IP_N=
#網(wǎng)站配置文件的位置
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/
#檢測(cè)當(dāng)前使用的網(wǎng)卡
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
}
#判斷內(nèi)網(wǎng)網(wǎng)卡是否故障
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
}
#檢測(cè)各個(gè)網(wǎng)卡是否通暢
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
- 分享個(gè)簡(jiǎn)易版Linux服務(wù)器初始化Shell腳本
- Linux shell腳本基礎(chǔ)學(xué)習(xí)詳細(xì)介紹(完整版)
- 利用Shell腳本實(shí)現(xiàn)遠(yuǎn)程MySQL自動(dòng)查詢
- linux服務(wù)器安全加固shell腳本代碼
- 如何調(diào)試Linux shell腳本
- linux下監(jiān)視進(jìn)程 崩潰掛掉后自動(dòng)重啟的shell腳本
- 自動(dòng)重啟服務(wù)的shell腳本代碼
- linux shell腳本基礎(chǔ)知識(shí)學(xué)習(xí)
- 學(xué)習(xí)shell腳本之前的基礎(chǔ)知識(shí)[圖文]
- linux中mysql備份shell腳本代碼
- Linux下使用Shell腳本實(shí)現(xiàn)ftp的自動(dòng)上傳下載的代碼小結(jié)
- 獲取兩個(gè)日期間隔時(shí)間的shell腳本代碼
- 判斷文件是否存在的shell腳本代碼
- kill特定進(jìn)程的shell腳本代碼
- 查找目錄下同名但不同后綴名文件的shell腳本代碼
- 在指定目錄查找指定后綴文件的shell腳本代碼
- 統(tǒng)計(jì)網(wǎng)卡流量的兩段shell腳本(使用ifconfig)
- shell腳本作為保證PHP腳本不掛掉的守護(hù)進(jìn)程實(shí)例分享
相關(guān)文章
淺談Linux 二進(jìn)制包安裝MySQL的一些問(wèn)題
下面小編就為大家?guī)?lái)一篇淺談Linux 二進(jìn)制包安裝MySQL的一些問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03通過(guò)短信發(fā)送LOG歸類號(hào)碼發(fā)送情況的shell腳本
通過(guò)短信發(fā)送LOG歸類號(hào)碼發(fā)送情況的shell腳本,有需要的朋友可以參考下2013-02-02java應(yīng)用啟動(dòng)與停止的shell腳本分享
這篇文章主要和大家分享三個(gè)shell腳本,分別是jvm啟動(dòng)參數(shù)相關(guān)的setjvment.sh,利用java命令啟動(dòng)進(jìn)程的start.sh和停止java進(jìn)程的stop.sh,有需要的可以參考下2023-10-10Shell eval通過(guò)變量獲取環(huán)境變量的方法實(shí)現(xiàn)
這篇文章主要介紹了Shell eval通過(guò)變量獲取環(huán)境變量的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12shell?數(shù)組?${array[@]}?${array[*]}的使用及區(qū)別解析
shell中用括號(hào)來(lái)表示數(shù)組,數(shù)組元素間使用空格隔開(kāi),這篇文章主要介紹了shell?數(shù)組?${array[@]}?${array[*]}的使用及區(qū)別,需要的朋友可以參考下2023-06-06