Article From:https://www.cnblogs.com/bobo137950263/p/9970110.html
#!/bin/bash
BASEDIR="/usr/local/mysql"
BIN="$BASEDIR/bin"
MYSQLBINLOG="$BIN/mysqlbinlog"
BINLOG_DIRECTORY="/usr/local/mysql/mysqldata/master_binlog_bak"

declare -A DICT_HOST
declare -A DICT_PORT
declare -A DICT_USER
declare -A DICT_PASSWORD
DICT_HOST=(["data1"]="192.168.1.108" ["data2"]="192.168.1.118" ["data3"]="192.168.1.110" )
DICT_PORT=(["data1"]="1234" ["data2"]="1234" ["data3"]="1234" )
DICT_USER=(["data1"]="replication" ["data2"]="replication" ["data3"]="replication" )
DICT_PASSWORD=(["data1"]="123" ["data2"]="123" ["data3"]="123" )



# get the last file name,input the file directory,get the last file`s name
get_last_file_name(){
    local DATANAME=$1
    LAST_FILE_DIRECTORY="$BINLOG_DIRECTORY/$DATANAME"
    LAST_FILE_NAME_NUMBER=`ls -l $LAST_FILE_DIRECTORY|tail -1|cut -d '.' -f 2`
    LAST_FILE_NAME="mysql-bin.$LAST_FILE_NAME_NUMBER"
    echo $LAST_FILE_NAME
}


# the mysqlbinlog replication function
start_mysqlbinlog(){
    local REMOTE_HOST=$1
    local PORT=$2
    local USER=$3
    local PASSWORD=$4
    local DATANAME=$5
    local LAST_BINLOG_NAME
    local RESULT_FILE_NAME=$6
    
    LAST_BINLOG_NAME=`get_last_file_name $DATANAME`
    #echo "/usr/local/mysql/bin/mysqlbinlog --raw --read-from-remote-server --stop-never --host=$REMOTE_HOST --port=$PORT --user=$USER --password=$PASSWORD $LAST_BINLOG_NAME --result-file=$RESULT_FILE_NAME  &"
    $MYSQLBINLOG --raw --read-from-remote-server --stop-never --host=$REMOTE_HOST --port=$PORT --user=$USER --password=$PASSWORD $LAST_BINLOG_NAME --result-file=$RESULT_FILE_NAME  &
    }


for DATA in {"data1","data2","data3"}
    do
        RESULT_FILE_FORMAT="$BINLOG_DIRECTORY/$DATA/back-"
        [ `ps -ef|grep mysqlbinlog|grep $DATA|wc -l` -eq 0 ] && start_mysqlbinlog `echo ${DICT_HOST["$DATA"]}` `echo ${DICT_PORT["$DATA"]}` `echo ${DICT_USER["$DATA"]}` `echo ${DICT_PASSWORD["$DATA"]}` $DATA $RESULT_FILE_FORMAT
done


        

  In the form of a dictionary, the information of all three instances is stored in different dictionaries, and the synchronization service is started through a loop.

  A dictionary is actually a form of key-value pairs, where values are obtained by keys. Use ${dict [key]}

Leave a Reply

Your email address will not be published. Required fields are marked *