commit 3a1f5905d252e0e06bfdad2f10268d2c6445f868 Author: dimti Date: Wed Apr 29 01:29:07 2020 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c3041a7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.log +.*.cnf +.idea diff --git a/_config.sh b/_config.sh new file mode 100644 index 0000000..4a5afdb --- /dev/null +++ b/_config.sh @@ -0,0 +1,3 @@ +#!/bin/bash +log_file=replication.log +databases=( 'database_one' 'another_database' ) diff --git a/dump.sh b/dump.sh new file mode 100644 index 0000000..81caa42 --- /dev/null +++ b/dump.sh @@ -0,0 +1,11 @@ +#!/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 + +# Синхронизация баз (удаление, если базы есть, и создание) +echo "Dump remote databases and pipi to local mariadb" >> $log_file +mysqldump --defaults-extra-file=./.remote.my.cnf --add-drop-database --databases "${databases[@]}" | mysql --defaults-extra-file=./.my.cnf diff --git a/example.my.cnf b/example.my.cnf new file mode 100644 index 0000000..5fbb8ae --- /dev/null +++ b/example.my.cnf @@ -0,0 +1,4 @@ +[client] +user=replicationuser +password=replicationpassword +host=localhost diff --git a/example.remote.my.cnf b/example.remote.my.cnf new file mode 100644 index 0000000..1298f2c --- /dev/null +++ b/example.remote.my.cnf @@ -0,0 +1,4 @@ +[client] +user=replicationuser +password=replicationpassword +host=remotehostip diff --git a/readbinlog.sh b/readbinlog.sh new file mode 100644 index 0000000..4c517de --- /dev/null +++ b/readbinlog.sh @@ -0,0 +1,22 @@ +#!/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 +remote_master_status=`mysql --defaults-extra-file=./.remote.my.cnf -e "SHOW MASTER STATUS;" -N` + +remote_master_log_file=`echo $remote_master_status | cut -d' ' -f1` +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` + +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/start-replication.sh b/start-replication.sh new file mode 100644 index 0000000..3ae8280 --- /dev/null +++ b/start-replication.sh @@ -0,0 +1,27 @@ +#!/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 + +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 ./dump.sh;' + +for i in ${databases[@]} +do + REMOTE_SQL=$REMOTE_SQL' USE '$i'; UNLOCK TABLES;' +done + +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;"