Use shell scripts to automatically switch between Intranet and Internet for High Availability

Source: Internet
Author: User

First, describe our configuration files in a similar format. If the Intranet is 192.168.0.3 and the Internet is 123.123.123.123, the configuration file is as follows:

$ Db ['salver '] ['hostname'] = '192. 168.0.3: 100 ';
// $ Db ['salver '] ['hostname'] = '2017. 123.123.123: 123 ';

In this case, the Internet is commented. Intranet is used.
The idea is to copy a configuration file and place it in another place. The two copies are set to the Intranet, and the other one is set to the Internet. If the Intranet is disconnected, determine whether the Intranet or Internet is used. If the Intranet is used, copy the internet configuration file to the website file. If the Intranet is connected, copy the Intranet configuration file to the website file. It is equivalent that the Intranet IP address is the Master Address and the Internet IP address is the backup address. Place the script in the scheduled task and execute it once every minute.

Copy codeThe Code is as follows :#! /Bin/bash
# Intranet and Internet IP addresses of the Master/Slave Databases
Master_IP_N =
Master_IP_W =
Salver_IP_N =
Salver_IP_W =
# Mongo Intranet and Internet IP addresses
Pai_ip_w =
Pai_ip_n =
# Redis Master/Slave Intranet/Internet IP Address
Redis_master_IP_W =
Redis_master_IP_N =
Redis_slave_IP_W =
Redis_slave_IP_N =
# Sphsf-'s Intranet and Internet IP addresses
Sphinx_IP_W =
Sphinx_IP_N =
# Website configuration file location
Database_path =/home/caoy/database. php
Export _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/
# Check the currently used Nic
Function check_ip (){
IP_config = 'cat $1 | grep $2 | cut-C1-2'
If ["$ IP_config" = "//"]; then
Ip_status = 1
Else
Ip_status = 0
Fi
}
# Determine whether the Intranet Nic is faulty
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_paiphp $ database_path
Echo "cp database_paiphp is OK! "
Fi
Check_ip $ pai_path $1
If [["$ ip_status" = "0"]; then
Cp-r $ config_path/over_sea_stock_1_php $ pai_path
Echo "cp over_sea_stock_1_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 $ pai_path $1
If [["$ ip_status" = "1"]; then
Cp-r $ config_path/over_sea_stock_n.php $ pai_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 whether all NICs are smooth
Check_network $ master_IP_N
Check_network $ salver_IP_N
Check_network $ pai_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.