Using shell script to realize automatic switching of intranet and extranet to achieve high availability _linux shell

Source: Internet
Author: User
Tags redis

The first description of our configuration file, are similar format, if the intranet is 192.168.0.3, the extranet is 123.123.123.123, the configuration file is as follows:

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

This time is the external network is the state of the annotation. Walk is the intranet.
The next idea, is a configuration file, copied out 2 points put to another place, this two part of the inside set is the intranet, a inside set is outside the net, if the intranet is not through the case, judge the current use is intranet or extranet, if is the intranet, the external network configuration file copied to the website file, If the internal Netcom, the intranet configuration files are copied to the Web site files. is equivalent to the intranet IP is the main, extranet IP is prepared the same. Put this script in a scheduled task and execute it 1 minutes.

Copy Code code as follows:

#!/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 $ |GREP $ | Cut-c1-2 '
If ["$IP _config" = "//"];then
Ip_status=1
Else
Ip_status=0
Fi
}
#判断内网网卡是否故障
function Check_network () {
NMAP-SP $ | grep "1 host up" >/dev/null | | (Sleep 5; NMAP-SP $) | grep "1 Host up" >/dev/null
#ping $1-c 5 >/dev/null
if [["$?" = = "1"]]; Then
CHECK_IP $database _path $
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 $
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 $
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 $
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 $
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 $
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 $
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 $
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.