PostgreSQL双节点流复制图形化设置工具

最近看了个gtk+工具zenity,顺便写了个图形化配置pg流复制的脚本。

使用方法:

注:该脚本可以在主节点、备节点或另外一台相同的机器上运行,但需要首先设置ssh免密码登录认证,保证 脚本节点--》主节点(root)   脚本节点--》备节点(root)  主节点(root)--》备节点(root)免密码可登录,配置过程略。

启动脚本程序:

$ chmod +x stream.sh
$ ./stream.sh

执行之后出现如下对话框:

PostgreSQL双节点流复制图形化设置工具_第1张图片

勾选同意,点击“OK”


在下面对话框中填写上主备节点的相关信息,包括IP、数据库管理员用户、数据库端口号、数据目录、数据库软件安装地址:

PostgreSQL双节点流复制图形化设置工具_第2张图片

填写完成后点击“OK”


PostgreSQL双节点流复制图形化设置工具_第3张图片

点击“Yes”开始配置双机流复制


PostgreSQL双节点流复制图形化设置工具_第4张图片

PostgreSQL双节点流复制图形化设置工具_第5张图片

点击“OK”完成配置,此时双机流复制环境已经配置成功,接着就可以测试同步性了。

脚本代码:

#!/bin/sh
function license(){
  cat <<EOF >LICENSE
              #### LICENSE ####
Version: 1.0
zenity >= 3.4.0
EOF
  zenity --text-info \
         --title="License" \
         --filename=LICENSE \
         --checkbox="I read and accept the terms."
  case $? in
      0)
          conninfo
          #echo "Start installation!"
   # next step
       ;;
      1)
          #echo "Stop installation!"
          zenity --info --text="Stop installation\!"
   ;;
     -1)
          #echo "An unexpected error has occurred."
          zenity --error --text="An unexpected error has occurred\!"
   ;;
  esac
}

function conninfo(){
  INFO=`zenity --forms --width=400 --title="NODE'S INFORMATION" \
 --text="Enter information about nodes." \
 --add-entry="Master IP:"  \
        --add-entry="Master DBUser:" \
        --add-entry="Master PORT:" \
        --add-entry="Master DATA:" \
        --add-entry="Master INSTALL:" \
        --add-entry="Slave IP:" \
        --add-entry="Slave DBUser:" \
        --add-entry="Slave PORT:" \
        --add-entry="Slave DATA:" \
        --add-entry="Slave INSTALL:" `
  case $? in
    0)
       IP_M=`echo "$INFO" | cut -d "|" -f1`
       IP_S=`echo "$INFO" | cut -d "|" -f6`
       USER_M=`echo "$INFO" | cut -d "|" -f2`
       USER_S=`echo "$INFO" | cut -d "|" -f7`
       PORT_M=`echo "$INFO" | cut -d "|" -f3`
       PORT_S=`echo "$INFO" | cut -d "|" -f8`
       DATA_M=`echo "$INFO" | cut -d "|" -f4`
       DATA_S=`echo "$INFO" | cut -d "|" -f9`
       INSTALL_M=`echo "$INFO" | cut -d "|" -f5`
       INSTALL_S=`echo "$INFO" | cut -d "|" -f10`
  
       # test ssh
       ssh -o ConnectTimeout=3 root@$IP_M date &>/dev/null
       if [ $? -ne 0 ];then
         zenity --error --text="Can't connect to Master\!" 
         exit
       fi
       ssh -o ConnectTimeout=3 root@$IP_S date &>/dev/null
       if [ $? -ne 0 ];then
         zenity --error --text="Can't connect to Slave\!"         
         exit
       fi
       ssh -o ConnectTimeout=3 root@$IP_M "ssh -o ConnectTimeout=3 root@$IP_S date" &>/dev/null
       if [ $? -ne 0 ];then
         zenity --error --text="Can't connect to Slave from Master\!"
         exit
       fi
       
       # test directory
       ssh -o ConnectTimeout=3 root@$IP_M "test -d $DATA_M"
       if [ $? -ne 0 ];then
         zenity --error --text="Data dir dosen't exists on Master\!"
         exit
       fi
       ssh -o ConnectTimeout=3 root@$IP_S "test -d $DATA_S"
       if [ $? -ne 0 ];then
         zenity --error --text="Data dir dosen't exists on Slave\!"
         exit
       fi
       # start configure
       zenity --question --text="Are you sure you wish to start configure?"
       if [ $? -eq 0 ];then
         startconf
       else
         zenity --error --text="Stop installation\!"
         exit
       fi
       ;;
    1)
       zenity --info --text="Stop installation\!"
       ;;
    -1)
       zenity --error --text="An unexpected error has occurred\!"
       ;;
  esac
}
function startconf(){
  (
  config_post
  echo "10" ; sleep 1
  echo "# Configuring pg_hba.conf..." ; sleep 1
  config_hba
  echo "20" ; sleep 1
  echo "# Restarting master..." ; sleep 1
  restart_m
  echo "30" ; sleep 1
  echo "# Copying data from master to slave... " ; sleep 1
  copy_data
  echo "80" ; sleep 1
  echo "# Configuring recovery.conf..." ; sleep 1
  config_reco
  echo "90" ; sleep 1
  echo "# Starting slave..." ; sleep 1
  start_s
  echo "100" ; sleep 1
  echo "# Complete." ; sleep 1
  ) |
  zenity --progress  --width=500 \
         --title="Configure Streaming" \
         --text="Configuring postgresql.conf..." \
         --percentage=0
}
function config_post(){
  ssh -o ConnectTimeout=3 root@$IP_M "test -f $DATA_M/postgresql.conf"
  if [ $? -ne 0 ];then
    zenity --error --text="$DATA_M/postgresql.conf dosen't exists on Master\!"
    exit
  fi
  ssh root@$IP_M "sed -i '/listen_addresses =/c listen_addresses = '\''*'\''' $DATA_M/postgresql.conf"
  ssh root@$IP_M "sed -i '/port =/c port = $PORT_M' $DATA_M/postgresql.conf"
  ssh root@$IP_M "sed -i '/wal_level =/c wal_level = hot_standby' $DATA_M/postgresql.conf"
  ssh root@$IP_M "sed -i '/max_wal_senders =/c max_wal_senders = 1' $DATA_M/postgresql.conf"
  ssh root@$IP_M "sed -i '/wal_keep_segments =/c wal_keep_segments = 50' $DATA_M/postgresql.conf"
  ssh root@$IP_M "sed -i '/hot_standby =/c hot_standby = on' $DATA_M/postgresql.conf"
}
function config_hba(){
  ssh -o ConnectTimeout=3 root@$IP_M "test -f $DATA_M/pg_hba.conf"
  if [ $? -ne 0 ];then
    zenity --error --text="$DATA_M/pg_hba.conf dosen't exists on Master\!"
    exit
  fi
  ssh root@$IP_M "echo 'host    replication    all             $IP_S/32              trust' >>$DATA_M/pg_hba.conf"
  ssh root@$IP_M "sed -i 's/ md5/trust/g' $DATA_M/pg_hba.conf"
}
function restart_m(){
  ssh -o ConnectTimeout=3 root@$IP_M "su - $USER_M -c 'LD_LIBRARY_PATH=$INSTALL_M/lib $INSTALL_M/bin/pg_ctl -w stop -D $DATA_M -m f' &>/dev/null"
  ssh -o ConnectTimeout=3 root@$IP_M "su - $USER_M -c 'LD_LIBRARY_PATH=$INSTALL_M/lib $INSTALL_M/bin/pg_ctl -w start -D $DATA_M' &>/dev/null"
}
function copy_data(){
  ssh -o ConnectTimeout=3 root@$IP_S "su - $USER_S -c 'LD_LIBRARY_PATH=$INSTALL_S/lib $INSTALL_S/bin/pg_ctl -w stop -D $DATA_S -m f' &>/dev/null"
  ssh -o ConnectTimeout=3 root@$IP_S "rm -rf $DATA_S/*"
  ssh -o ConnectTimeout=3 root@$IP_M "LD_LIBRARY_PATH=$INSTALL_M/lib $INSTALL_M/bin/psql -U $USER_M -p $PORT_M -c 'select pg_start_backup('basebak')' &>/dev/null"
  ssh -o ConnectTimeout=3 root@$IP_M "scp -r $DATA_M/* root@$IP_S:$DATA_S/ &>/dev/null"
  ssh -o ConnectTimeout=3 root@$IP_M "LD_LIBRARY_PATH=$INSTALL_M/lib $INSTALL_M/bin/psql -U $USER_M -p $PORT_M -c 'select pg_stop_backup()' &>/dev/null"
}
function config_reco(){
  ssh -o ConnectTimeout=3 root@$IP_S "cat <<EOF >$DATA_S/recovery.conf
standby_mode = 'on'
primary_conninfo = 'host=$IP_M user=$USER_M port=$PORT_M'
EOF"
}
function start_s(){
  ssh -o ConnectTimeout=3 root@$IP_S "chown -R $USER_S $DATA_S"
  ssh -o ConnectTimeout=3 root@$IP_S "su - $USER_S -c 'LD_LIBRARY_PATH=$INSTALL_S/lib $INSTALL_S/bin/pg_ctl -w start -D $DATA_S' &>/dev/null"
}

license
#conninfo


你可能感兴趣的:(PostgreSQL,图形化,流复制,zenity)