佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 用shell腳本實現(xiàn)自動切換內(nèi)網(wǎng)和外網(wǎng)實現(xiàn)高可用

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

熱門標簽:鐵路電話系統(tǒng) 呼叫中心市場需求 網(wǎng)站排名優(yōu)化 AI電銷 地方門戶網(wǎng)站 百度競價排名 服務外包 Linux服務器

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

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

復制代碼 代碼如下:

#!/bin/bash
#主從數(shù)據(jù)庫的內(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/
#檢測當前使用的網(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
}
#檢測各個網(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

您可能感興趣的文章:
  • 分享個簡易版Linux服務器初始化Shell腳本
  • Linux shell腳本基礎學習詳細介紹(完整版)
  • 利用Shell腳本實現(xiàn)遠程MySQL自動查詢
  • linux服務器安全加固shell腳本代碼
  • 如何調(diào)試Linux shell腳本
  • linux下監(jiān)視進程 崩潰掛掉后自動重啟的shell腳本
  • 自動重啟服務的shell腳本代碼
  • linux shell腳本基礎知識學習
  • 學習shell腳本之前的基礎知識[圖文]
  • linux中mysql備份shell腳本代碼
  • Linux下使用Shell腳本實現(xiàn)ftp的自動上傳下載的代碼小結
  • 獲取兩個日期間隔時間的shell腳本代碼
  • 判斷文件是否存在的shell腳本代碼
  • kill特定進程的shell腳本代碼
  • 查找目錄下同名但不同后綴名文件的shell腳本代碼
  • 在指定目錄查找指定后綴文件的shell腳本代碼
  • 統(tǒng)計網(wǎng)卡流量的兩段shell腳本(使用ifconfig)
  • shell腳本作為保證PHP腳本不掛掉的守護進程實例分享

標簽:蘭州 銅川 崇左 湖南 黃山 仙桃 湘潭 衡水

巨人網(wǎng)絡通訊聲明:本文標題《用shell腳本實現(xiàn)自動切換內(nèi)網(wǎng)和外網(wǎng)實現(xiàn)高可用》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    金昌市| 湖口县| 辉南县| 罗定市| 灵武市| 漳平市| 遂溪县| 信阳市| 宜宾县| 秭归县| 澄城县| 灵寿县| 连山| 新绛县| 隆尧县| 马山县| 信丰县| 循化| 鞍山市| 怀远县| 惠安县| 富阳市| 南漳县| 江口县| 山阳县| 多伦县| 裕民县| 常德市| 邵东县| 三门县| 武隆县| 永善县| 湖南省| 白玉县| 武陟县| 都安| 多伦县| 申扎县| 宁蒗| 武穴市| 酉阳|