#准备工作:创建wei账号,授权replication权限 grant replication slave on *.* to wei@'10.0.0.%' identified by '123'; #查看当前二进制文件 db01 [(none)]>show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql-bin.000002 | 1019 | | | 5dfe203f-5edc-11ea-a789-000c291f7357:1-3 | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec)
[root@db01 ~]# mkdir /data/backup [root@db01 ~]# cd /data/backup [root@db01 /data/backup]# mysqlbinlog -R --host=10.0.0.51 --user=wei --password=123 \ --raw -r /data/backup/ --stop-never mysql-bin.000001 & [1] 7647 [root@db01 /data/backup]# mysqlbinlog: [Warning] Using a password on the command line interface can be insecure.
db01 [(none)]>flush binary logs; Query OK, 0 rows affected (0.00 sec) db01 [(none)]>flush binary logs; Query OK, 0 rows affected (0.01 sec) db01 [(none)]>flush binary logs; Query OK, 0 rows affected (0.00 sec) db01 [(none)]>flush binary logs; Query OK, 0 rows affected (0.01 sec) db01 [(none)]>flush binary logs; Query OK, 0 rows affected (0.00 sec) db01 [(none)]>show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql-bin.000009 | 194 | | | 5dfe203f-5edc-11ea-a789-000c291f7357:1-3 | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec)
[root@db01 /data/backup]# ll total 36 -rw-r----- 1 root root 177 Mar 5 23:40 mysql-bin.000001 -rw-r----- 1 root root 1066 Mar 5 23:40 mysql-bin.000002 -rw-r----- 1 root root 217 Mar 5 23:40 mysql-bin.000003 -rw-r----- 1 root root 241 Mar 5 23:40 mysql-bin.000004 -rw-r----- 1 root root 241 Mar 5 23:40 mysql-bin.000005 -rw-r----- 1 root root 241 Mar 5 23:40 mysql-bin.000006 -rw-r----- 1 root root 241 Mar 5 23:40 mysql-bin.000007 -rw-r----- 1 root root 241 Mar 5 23:40 mysql-bin.000008 -rw-r----- 1 root root 194 Mar 5 23:40 mysql-bin.000009
思路
#!/bin/sh BACKUP_BIN=/usr/bin/mysqlbinlog LOCAL_BACKUP_DIR=/backup/binlog/ BACKUP_LOG=/backup/binlog/backuplog REMOTE_HOST=192.168.244.145 REMOTE_PORT=3306 REMOTE_USER=repl REMOTE_PASS=repl FIRST_BINLOG=mysql-bin.000001 #time to wait before reconnecting after failure SLEEP_SECONDS=10 ##create local_backup_dir if necessary mkdir -p ${LOCAL_BACKUP_DIR} cd ${LOCAL_BACKUP_DIR} ## 运行while循环,连接断开后等待指定时间,重新连接 while : do if [ `ls -A "${LOCAL_BACKUP_DIR}" |wc -l` -eq 0 ];then LAST_FILE=${FIRST_BINLOG} else LAST_FILE=`ls -l ${LOCAL_BACKUP_DIR} | grep -v backuplog |tail -n 1 |awk '{print $9}'` fi ${BACKUP_BIN} --raw --read-from-remote-server --stop-never --host=${REMOTE_HOST} --port=${REMOTE_PORT} --user=${REMOTE_USER} --password=${REMOTE_PASS} ${LAST_FILE} echo "`date +"%Y/%m/%d %H:%M:%S"` mysqlbinlog停止,返回代码:$?" | tee -a ${BACKUP_LOG} echo "${SLEEP_SECONDS}秒后再次连接并继续备份" | tee -a ${BACKUP_LOG} sleep ${SLEEP_SECONDS} done
来源:https://www.cnblogs.com/Mercury-linux/p/12423973.html