当前位置:网站首页>Shell script for MySQL real-time synchronization of binlog
Shell script for MySQL real-time synchronization of binlog
2022-06-24 07:39:00 【Big Hedgehog】
mysqlbinlog Can be disguised as slave Go to the main server to get binlog. This script encapsulates this function .
This script is better used with scheduled tasks .
1. Script download address
github: https://github.com/ddcw/ddcw/blob/master/shells/mysqlBinlogSYNC.sh
Or see the end of the text
2. Instructions
This script does not support positional parameters , Only the parameters in the script can be modified
REMOTE_HOST="172.17.0.10"
REMOTE_PORT="3311"
REMOTE_USER="repuser"
REMOTE_PASSWORD="repuser"
# Empty words , Will be first LOCAL_PRENAME Find the latest binlog, And then from that Binlog Start syncing .
# If you don't get it under this directory , From the current login query lsn Start
# If specified , From the designated Binlog Start , Do not check whether the name is correct
BEGIN_BINLOG=""
# That is, to preserve binlog The directory prefix for , such as
#/data/bin1 Words , Namely /data/bin1binlog.000002 such , Very ugly , Add... To all the directories / ending , This variable is not formatted , What you set is what you set
#/data/bin1/ In this way, it is /data/bin1/binlog.000002
# If it is empty by default , It's the current directory
LOCAL_PRENAME=""
3. Examples of use
Configure the corresponding server first IP Account and password
Just execute the script directly ( It is recommended to put it in the background , It is better to add a scheduled task , If the script hangs, execute it again )
Test data
Check the current binlog Of lsn Whether the
A scheduled task can be written like this ( Change the path yourself )
* * * * * [[ -d /proc$(cat .mysqlBinlogSYNC.pid) ]] || sh mysqlBinlogSYNC.sh
4. Script
The script is as follows :
#!/bin/env bash
#write by ddcw at 2021.06.29
# Backup to a remote location is not supported , You can run this script on the server you want to save
#mysqlbinlog and mysql The command needs to be configured in the environment variable
REMOTE_HOST="172.17.0.10"
REMOTE_PORT="3311"
REMOTE_USER="repuser"
REMOTE_PASSWORD="repuser"
# Empty words , Will be first LOCAL_PRENAME Find the latest binlog, And then from that Binlog Start syncing .
# If you don't get it under this directory , From the current login query lsn Start
# If specified , From the designated Binlog Start , Do not check whether the name is correct
BEGIN_BINLOG=""
# That is, to preserve binlog The directory prefix for , such as
#/data/bin1 Words , Namely /data/bin1binlog.000002 such , Very ugly , Add... To all the directories / ending , This variable is not formatted , What you set is what you set
#/data/bin1/ In this way, it is /data/bin1/binlog.000002
# If it is empty by default , It's the current directory
LOCAL_PRENAME=""
# Add one PID file , Convenient scheduled tasks , Judge , such as : * * * * * [[ -d /proc$(cat .mysqlBinlogSYNC.pid) ]] || sh mysqlBinlogSYNC.sh
PIDFILE=.mysqlBinlogSYNC.pid
echo $$ > ${PIDFILE}
#run this function and exit with $2
function exits(){
echo -e "[`date +%Y%m%d-%H:%M:%S`] \033[31;40m$1\033[0m"
[ -z $2 ] && exit $2
exit 1
}
function echo_color() {
case $1 in
green)
echo -e "\033[32;40m$2\033[0m"
;;
red)
echo -e "\033[31;40m$2\033[0m"
;;
error|erro|ERROR|E|e)
echo -e "[\033[1;5;41;33mERROR\033[0m `date +%Y%m%d-%H:%M:%S`] \033[1;41;33m$2\033[0m"
;;
redflicker)
echo -e "\033[1;5;41;33m$2\033[0m"
;;
info|INFO|IF|I|i)
echo -e "[\033[32;40mINFO\033[0m `date +%Y%m%d-%H:%M:%S`] \033[32;40m$2\033[0m"
;;
highlightbold)
echo -e "\033[1;41;33m$2\033[0m"
;;
warn|w|W|WARN|warning)
echo -e "[\033[31;40mWARNNING\033[0m `date +%Y%m%d-%H:%M:%S`] \033[31;40m$2\033[0m"
;;
*)
echo "INTERNAL ERROR: echo_color KEY VALUE"
;;
esac
}
LOCAL_CURRENT_BINLOG=${BEGIN_BINLOG}
# Judge ctrl+c
trap 'WhenCtrlC' INT
function WhenCtrlC () {
binlog_file=$(ls ${LOCAL_PRENAME}${MASTER_BINLOG_PRE}* 2>/dev/null | grep -v \.index | sort | tail -1)
lsn=$(mysqlbinlog ${binlog_file} | grep end_log_pos | grep server | grep CRC32 | awk -F end_log_pos '{print $2}' | awk '{print $1}' | tail -1)
echo_color info " This program is kill 了 , Current and current binlog yes :${binlog_file} lsn: ${lsn}"
echo_color info " The next startup will automatically start the synchronization ( If you don't delete the latest binlog, And don't move )"
rm -rf ${PIDFILE} >/dev/null 2>&1
exit 1
}
function init_() {
# Check to see if there is any mysql and mysqlbinlog command
which mysql >/dev/null 2>&1 || exits "no command mysql in env"
which mysqlbinlog >/dev/null 2>&1 || exits "no command mysqlbinlog in env"
# Check whether it can be connected normally
cat /dev/null > /tmp/.${0}_msyqltmpbyddcw.tmp
mysql -s -h ${REMOTE_HOST} -P ${REMOTE_PORT} -u ${REMOTE_USER} -p${REMOTE_PASSWORD} -e "show master status;" 2>/dev/null > /tmp/.${0}_msyqltmpbyddcw.tmp || exits "connect failed or exec 'show master status;' failed"
#echo "/tmp/.${0}_msyqltmpbyddcw.tmp"
export MASTER_CURRENT_BINLOG=$(awk '{print $1}' /tmp/.${0}_msyqltmpbyddcw.tmp)
export MASTER_CURRENT_LSN=$(awk '{print $2}' /tmp/.${0}_msyqltmpbyddcw.tmp)
export MASTER_BINLOG_PRE=$(echo ${MASTER_CURRENT_BINLOG} | awk -F \. '{print $1}')
[[ -z ${LOCAL_CURRENT_BINLOG} ]] && LOCAL_CURRENT_BINLOG=$(ls ${LOCAL_PRENAME}${MASTER_BINLOG_PRE}* 2>/dev/null | grep -v \.index | sort | tail -1)
[[ -z ${LOCAL_CURRENT_BINLOG} ]] && export LOCAL_CURRENT_BINLOG=${MASTER_CURRENT_BINLOG}
[[ -z ${LOCAL_CURRENT_BINLOG} ]] && exits " Please set the enabled binlog, Auto get failed "
[[ -f ${LOCAL_PRENAME}${LOCAL_CURRENT_BINLOG} ]] && export LOCAL_LSN=$(mysqlbinlog ${LOCAL_PRENAME}${LOCAL_CURRENT_BINLOG} | grep end_log_pos | grep server | grep CRC32 | awk -F end_log_pos '{print $2}' | awk '{print $1}' | tail -1)
[[ -z ${LOCAL_LSN} ]] && export LOCAL_LSN=${MASTER_CURRENT_LSN}
}
function main_() {
echo_color info " Start syncing binlog"
echo_color info " Current binlog: ${LOCAL_CURRENT_BINLOG} LSN: ${LOCAL_LSN}"
if [[ -z ${LOCAL_PRENAME} ]];then
while :
do
mysqlbinlog --read-from-remote-server --raw --host=${REMOTE_HOST} --port=${REMOTE_PORT} --user=${REMOTE_USER} --password=${REMOTE_PASSWORD} --stop-never ${LOCAL_CURRENT_BINLOG} 2>/dev/null
echo_color warn "mysqlbinlog stopd with some error, and begin after 10s"
sleep 10
done
else
while :
do
mysqlbinlog --read-from-remote-server --raw --host=${REMOTE_HOST} --port=${REMOTE_PORT} --user=${REMOTE_USER} --password=${REMOTE_PASSWORD} --stop-never ${LOCAL_CURRENT_BINLOG} --result-file=${LOCAL_PRENAME} 2>/dev/null
echo_color warn "mysqlbinlog stopd with some error, and begin after 10s"
sleep 10
done
fi
}
init_
main_
rm -rf ${PIDFILE} >/dev/null 2>&1边栏推荐
- Deploy loglistener in tke container to collect logs to CLS
- [signal recognition] signal modulation classification based on deep learning CNN with matlab code
- Selector (>, ~, +, [])
- 自动化测试是什么?什么软件项目适合自动化测试?
- [learn FPGA programming from scratch -41]: vision chapter - Moore's era and Moore's law and the arrival of the post Moore Era
- Virtual machine security disaster recovery construction
- Counter attack from outsourcing to big factories! Android has been developed for 5 years, and after a year of dormancy, it has tried to become an offer harvester. Tencent has a fixed salary of 20*15
- 捏脸师: 炙手可热的元宇宙造型师
- 阿里云全链路数据治理
- 【Cnpm】使用教程
猜你喜欢

(CVE-2020-11978)Airflow dag中的命令注入漏洞复现【vulhub靶场】

JVM debugging tool -jmap

MaxCompute远程连接,上传、下载数据文件操作

More than 60 million shovel excrement officials, can they hold a spring of domestic staple food?
![[GUET-CTF2019]zips](/img/79/22ff5d4a3cdc3fa9e0957ccc9bad4b.png)
[GUET-CTF2019]zips
![[image fusion] image fusion based on NSST and PCNN with matlab code](/img/b4/61a5adde0d0bfc5a339ef8ab948d43.png)
[image fusion] image fusion based on NSST and PCNN with matlab code

RDD basic knowledge points

get_ started_ 3dsctf_ two thousand and sixteen
![[image feature extraction] image feature extraction based on pulse coupled neural network (PCNN) including Matlab source code](/img/b3/26cfa385aa357c3a7a77e9db47e94c.png)
[image feature extraction] image feature extraction based on pulse coupled neural network (PCNN) including Matlab source code

前缀和专题训练
随机推荐
软件性能测试分析与调优实践之路-JMeter对RPC服务的性能压测分析与调优-手稿节选
Counter attack from outsourcing to big factories! Android has been developed for 5 years, and after a year of dormancy, it has tried to become an offer harvester. Tencent has a fixed salary of 20*15
Maxcompute remote connection, uploading and downloading data files
自动化测试是什么?什么软件项目适合自动化测试?
Deploy L2TP in VPN (medium)
MySQL - three tables (student, course, score) to query the name, number and score of students whose course is mathematics
[TS] function type
相机标定(标定目的、原理)
阿里云全链路数据治理
PCL 计算多边形的面积
Global and Chinese market of digital fryer 2022-2028: Research Report on technology, participants, trends, market size and share
[learn FPGA programming from scratch -41]: vision chapter - Moore's era and Moore's law and the arrival of the post Moore Era
Obtain the package name, application name, icon, etc. of the uninstalled APK through packagemanager. There is a small message
Virtual machine security disaster recovery construction
MySQL case: analysis of full-text indexing
Win11 points how to divide disks? How to divide disks in win11 system?
The initial user names and passwords of Huawei devices are a large collection that engineers involved in Huawei business should keep in mind and collect!
【TS】函数类型
Buuctf misc grab from the doll
Huawei experimental topology set, learning methods are attached at the end of the article!