记录一段无网环境安装双机热备的shell脚本

一、目录结构

      第一层目录如下:安装脚本和资源包在同一层

      第二层是资源包内部,包括编译安装好的keepalived和inotify,还有keepalived配置文件包和rsyncd配置文件包等。
        daemon是安装daemon守护进程用的组件包,keepalived启动的时候要用daemon进行多进程。

 二、shell脚本代码

#!/bin/bash

CURRENT_DIR=$(pwd)
function check_ubuntu_version() {
  lsb_release -d | grep "Ubuntu 22.04" >/dev/null
  return $?
}

# Function to check if keepalived is installed
function check_keepalived_installed() {
  if systemctl status keepalived | grep "Active" >/dev/null; then
    return 0
  fi
  return 1
}

# Function to install required components
function install_components() {
  tar -xf no-network-keepalived.tar
  cd no-network-keepalived
  # Copy and extract inotify-resource.tar and keepalived-resource.tar
  cp inotify-resource.tar /usr/local/
  cp keepalived-resource.tar /usr/local/
  cd /usr/local/
  tar -xf inotify-resource.tar
  tar -xf keepalived-resource.tar
  # Copy and extract rsyncd-config.tar and keepalived-config.tar
  cd $CURRENT_DIR/no-network-keepalived/
  tar -xf rsyncd-config.tar -C /etc/
  tar -xf keepalived-config.tar -C /etc/
  # Copy daemon files and execute dpkg commands
  cd $CURRENT_DIR/no-network-keepalived/daemon/
  cp * /var/cache/apt/archives/
  dpkg -i man-db_2.10.2-1_amd64.deb
  dpkg -i daemon_0.8-1_amd64.deb
  # Reload systemctl and check if daemon command exists
  systemctl daemon-reload
  if ! command -v daemon >/dev/null; then
    echo "daemon process component installation failed."
    return 1
  fi
  return 0
}

# Function to configure keepalived
function configure_keepalived() {
  # Create /etc/sysconfig directory and copy keepalived config
  mkdir -p /etc/sysconfig
  cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

  # Extract keepalived-2.2.7.tar.gz and copy init
  cd $CURRENT_DIR/no-network-keepalived/
  # Create symlink for init functions
  cp keepalived /etc/init.d/
  # Create /etc/rc.d directory and create symlink for init.d
  ln -s /lib/lsb/init-functions /etc/init.d/functions
  mkdir -p /etc/rc.d
  ln -s /etc/init.d /etc/rc.d/
  # Modify the command in /etc/init.d/keepalived
  cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
  sed -i 's/daemon keepalived ${KEEPALIVED_OPTIONS}/daemon -- keepalived ${KEEPALIVED_OPTIONS}/' /etc/init.d/keepalived
  # Reload systemctl
  systemctl daemon-reload
  systemctl enable keepalived
  systemctl start keepalived
  sleep 5
  # Check keepalived status
  systemctl status keepalived
}

function configure_inotify() {
  # Change to /etc/rsyncd directory
  cd /etc/rsyncd
  chmod 777 inotify.sh exclude.list rc.local
  chmod 600 passwd.txt
  # List of files to copy
  declare -a files=("rsyncd.conf" "passwd.txt" "rsync.pass" "rc-local.service")
  # Copy files
  cp "${files[@]}" /etc
  # Change permissions
  # Copy rc-local.service to /etc/systemd/system/
  cp rc-local.service /etc/systemd/system/
  # Reload systemctl
  systemctl daemon-reload
  # Start rc-local and check status
  systemctl status rc-local
  # Enable rc-local for startup
  systemctl enable rsync
}

function check_inotify() {
  if systemctl status rc-local | grep "Active" >/dev/null; then
    echo "rc-local was installed successfully"
    if systemctl status rc-local | grep "Active: active" >/dev/null; then
     return 1
    else
      return 0
    fi
    return 0
  fi
}
# Function to get and set local IP
function get_handler_config() {
  read -p "Please enter the local IP address: " local_ip
  read -p "Please enter the other IP address: " other_ip
  read -p "Please enter the other VIP address: " vip
  sed -i "s|^[ \t]*[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\/[0-9]\+|\t\t$vip/24|" /etc/keepalived/keepalived.conf
  read -p "Please enter the selected Ethernet network interface name: " interface_name
  sed -i "s/^[ \t]*interface.*/\t\tinterface ${interface_name}/" /etc/keepalived/keepalived.conf

  config_ssh $other_ip
  # Validate the IP address (optional, you can add more robust validation if needed)
  if [[ ! $local_ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
    echo "Invalid IP address format. Please try again."
    exit 1
  fi
  #sudo config
  sed -i "s/^# See sudoers(5) for more.*/main    ALL=(ALL) NOPASSWD:ALL/" /etc/sudoers
  # Update the script with the local IP configuration
  sed -i "s/^ip1=.*/ip1=\"$local_ip\"/" /etc/rsyncd/inotify.sh
  sed -i "s/^OTHER_IP=.*/OTHER_IP=\"$other_ip\"/" /etc/keepalived/check_http.sh
  sed -i "s/^OTHER_IP=.*/OTHER_IP=\"$other_ip\"/" /etc/keepalived/test.sh
  sed -i "s/^hosts allow =.*/hosts allow = $other_ip /" /etc/rsyncd/rsyncd.conf
  read -p "Do you want to set this node as the default master? (1 for yes, 0 for no): " choice
  case "$choice" in
  1)
    is_default_master=true
    ;;
  0)
    is_default_master=false
    ;;
  *)
    echo "Invalid choice. Please choose '1' for yes or '0' for no."
    exit 1
    ;;
  esac
  if $is_default_master; then
    hostnamectl set-hostname ha1
    echo "$local_ip ha1" >>/etc/hosts
    echo "$other_ip ha2" >>/etc/hosts
    sed -i 's/^[\t ]*priority.*/\t\tpriority 100/' /etc/keepalived/keepalived.conf
    sed -i 's/^[\t ]*router_id.*/\t\trouter_id ha1/' /etc/keepalived/keepalived.conf
    sed -i 's#^\[app_rsync_*#[app_rsync_server]#' /etc/rsyncd/rsyncd.conf
    sed -i 's#^\comment =.*#comment =app_rsync_server#' /etc/rsyncd/rsyncd.conf
    sudo sed -i 's/^comment.*/comment = app_rsync_server/' /etc/rsyncd/rsyncd.conf
  else
    hostnamectl set-hostname ha2
    echo "$local_ip ha2" >>/etc/hosts
    echo "$other_ip ha1" >>/etc/hosts
    sed -i 's/^[\t ]*priority.*/\t\tpriority 90/' /etc/keepalived/keepalived.conf
    sed -i 's/^[\t ]*router_id.*/\t\trouter_id ha2/' /etc/keepalived/keepalived.conf
    sed -i 's#^\[app_rsync.*#[app_rsync_client]#' /etc/rsyncd/rsyncd.conf
    sudo sed -i 's/^comment.*/comment = app_rsync_client/' /etc/rsyncd/rsyncd.conf
  fi
  echo "Configuration update complete ."
}

function start_all_server() {
  systemctl start rsyncd
  systemctl enable rsyncd
  systemctl status rsyncd
  systemctl start auto-inotify
  systemctl status auto-inotify
  systemctl stop auto-inotify
  systemctl start keepalived
  systemctl enable keepalived
  systemctl status keepalived
  echo "The installation of dual-machine hot standby service is completed"
}

function config_ssh() {
	echo "Start configuring the two-machine ssh communication."
	other_ip=$1
	echo -e "\n" | ssh-keygen -t rsa -q -N ""
  echo "yes" | scp  ~/.ssh/id_rsa.pub $other_ip:/root/.ssh/authorized_keys
}

if check_ubuntu_version; then
  echo "Detected Ubuntu 22.04."
 if install_components; then
      echo "Keepalived components installed successfully."
      configure_keepalived
      if check_keepalived_installed; then
        echo "keepalived installed successfully."
      fi
    else
      echo "Keepalived component installation failed."
    fi
  configure_inotify
  if check_inotify; then
    echo "inotify installed successfully."
  fi
    get_handler_config
    start_all_server
else
  echo "This script is intended for Ubuntu 22.04 only."
  exit 1
fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值