From 3b8eab46009cdd204848454292f1c2139b060939 Mon Sep 17 00:00:00 2001 From: dimti Date: Fri, 16 Oct 2020 16:54:25 +0300 Subject: [PATCH] * config example rename + master-to-master replication functionality --- .gitignore | 2 +- _activate-master-to-master.sh | 23 +++++++++++++++++++++++ readbinlog.sh => _activate-slave.sh | 6 +++--- dump.sh => _dump.sh | 0 example_config.sh => config.sh-example | 1 + start-replication.sh | 24 ++++++++++++++++++++++-- 6 files changed, 50 insertions(+), 6 deletions(-) create mode 100755 _activate-master-to-master.sh rename readbinlog.sh => _activate-slave.sh (81%) rename dump.sh => _dump.sh (100%) rename example_config.sh => config.sh-example (76%) diff --git a/.gitignore b/.gitignore index 680d39f..eff3250 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.log .*.cnf .idea -/_* +/config.sh diff --git a/_activate-master-to-master.sh b/_activate-master-to-master.sh new file mode 100755 index 0000000..44c0459 --- /dev/null +++ b/_activate-master-to-master.sh @@ -0,0 +1,23 @@ +#!/bin/bash +export DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +CONFIG_FILE=$DIR/_config.sh +[[ ! -r "${CONFIG_FILE}" ]] && { echo "Could not read ${CONFIG_FILE}!"; exit 1; } +set -a +. "${CONFIG_FILE}" +set +a + +# Установка локально и удаленно MASTER-параметров +echo "Get master status from remote server" >> $log_file +local_master_status=`mysql --defaults-extra-file=./.remote.my.cnf -e "SHOW MASTER STATUS; +" -N` + +local_master_log_file=`echo $local_master_status | cut -d' ' -f1` +local_master_log_pos=`echo $local_master_status | cut -d' ' -f2` + +echo "Apply bin log position to remote mariadb db instance" >> $log_file + +replication_user=`cat ./.remote.my.cnf | grep user | cut -d'=' -f2` +replication_password=`cat ./.remote.my.cnf | grep password | cut -d'=' -f2` +replication_host=$localdatabasepublicip + +mysql --defaults-extra-file=./.remote.my.cnf -e "CHANGE MASTER TO MASTER_HOST = '$replication_host', MASTER_USER = '$replication_user', MASTER_PASSWORD = '$replication_password', MASTER_LOG_FILE = '$remote_master_log_file', MASTER_LOG_POS = $remote_master_log_pos;" diff --git a/readbinlog.sh b/_activate-slave.sh similarity index 81% rename from readbinlog.sh rename to _activate-slave.sh index 5950d4c..e6b59dc 100755 --- a/readbinlog.sh +++ b/_activate-slave.sh @@ -16,8 +16,8 @@ remote_master_log_pos=`echo $remote_master_status | cut -d' ' -f2` echo "Apply bin log position to local mariadb db instance" >> $log_file -replication_user=`cat ./.remote.my.cnf | grep user | cut -d'=' -f2` -replication_password=`cat ./.remote.my.cnf | grep password | cut -d'=' -f2` -replication_host=`cat ./.remote.my.cnf | grep host | cut -d'=' -f2` +replication_user=`cat ./.my.cnf | grep user | cut -d'=' -f2` +replication_password=`cat ./.my.cnf | grep password | cut -d'=' -f2` +replication_host=`cat ./.my.cnf | grep host | cut -d'=' -f2` mysql --defaults-extra-file=./.my.cnf -e "CHANGE MASTER TO MASTER_HOST = '$replication_host', MASTER_USER = '$replication_user', MASTER_PASSWORD = '$replication_password', MASTER_LOG_FILE = '$remote_master_log_file', MASTER_LOG_POS = $remote_master_log_pos;" diff --git a/dump.sh b/_dump.sh similarity index 100% rename from dump.sh rename to _dump.sh diff --git a/example_config.sh b/config.sh-example similarity index 76% rename from example_config.sh rename to config.sh-example index be4fd54..80408d9 100644 --- a/example_config.sh +++ b/config.sh-example @@ -1,3 +1,4 @@ #!/bin/bash log_file=replication.log databases=( 'database1' 'database2' ) +localdatabasepublicip= diff --git a/start-replication.sh b/start-replication.sh index 3ae8280..af1eeca 100755 --- a/start-replication.sh +++ b/start-replication.sh @@ -6,14 +6,16 @@ set -a . "${CONFIG_FILE}" set +a +REMOTE_SQL= + for i in ${databases[@]} do REMOTE_SQL=$REMOTE_SQL' USE '$i'; FLUSH TABLES WITH READ LOCK;' done -REMOTE_SQL=$REMOTE_SQL' system ./readbinlog.sh;' +REMOTE_SQL=$REMOTE_SQL' system ./_activate-slave.sh;' -REMOTE_SQL=$REMOTE_SQL' system ./dump.sh;' +REMOTE_SQL=$REMOTE_SQL' system ./_dump.sh;' for i in ${databases[@]} do @@ -25,3 +27,21 @@ echo "Start execution all commands on remote server" > $log_file mysql --defaults-extra-file=./.remote.my.cnf -e "$REMOTE_SQL" mysql --defaults-extra-file=./.my.cnf -e "START SLAVE;" + +if [ "$1" -eq "master-to-master" ]; then + LOCAL_SQL= + + for i in ${databases[@]} + do + LOCAL_SQL=LOCAL_SQL' USE '$i'; FLUSH TABLES WITH READ LOCK;' + done + + LOCAL_SQL=LOCAL_SQL' system ./_activate-master-to-master.sh;' + + for i in ${databases[@]} + do + LOCAL_SQL=LOCAL_SQL' USE '$i'; UNLOCK TABLES;' + done +fi + +