Last active
May 28, 2024 14:26
-
-
Save CHERTS/fa415b7cef97dde174e76816e1985476 to your computer and use it in GitHub Desktop.
Init MySQL replication after xtrabackup prepare (support gtid with multiline)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
MYSQL_MASTER_HOST=X.X.X.X | |
MYSQL_REPL_USER_NAME="repl" | |
MYSQL_REPL_USER_PASSWORD="bigpassword" | |
XTRABACKUP_INFO=/mnt/backup/xtrabackup_info | |
XTRABACKUP_BINLOG_INFO=/mnt/backup/xtrabackup_binlog_info | |
USE_AUTOPOSITION=1 | |
if [ ! -f "${XTRABACKUP_INFO}" ]; then | |
echo "ERROR: File '${XTRABACKUP_INFO}' not found." | |
exit 1 | |
fi | |
if [ ! -f "${XTRABACKUP_BINLOG_INFO}" ]; then | |
echo "ERROR: File '${XTRABACKUP_BINLOG_INFO}' not found." | |
exit 1 | |
fi | |
echo "Reset MySQL master and slave data..." | |
mysql -e "RESET MASTER;" | |
mysql -e "RESET SLAVE;" | |
mysql -e "RESET SLAVE ALL;" | |
GTID_MULTILINE=$(cat "${XTRABACKUP_INFO}" 2>/dev/null | awk '/innodb_from_lsn/{f=0} f; /lock_time/{f=1}' | wc -l 2>/dev/null) | |
if [ ${GTID_MULTILINE} -ne 1 ]; then | |
GTID_PURGED=$(cat "${XTRABACKUP_INFO}" 2>/dev/null | awk '/innodb_from_lsn/{f=0} f; /lock_time/{f=1}' | sed \"s/^.*change '//\" | tr -d '\n' | tr -d \') | |
echo "GTID position: ${GTID_PURGED}" | |
mysql -e "set global gtid_purged=\"${GTID_PURGED}\";" | |
else | |
GTID_PURGED=$(cat "${XTRABACKUP_INFO}" | grep binlog_pos | awk -F' ' '{print $12}' | tr -d \' 2>/dev/null) | |
echo "GTID position: ${GTID_PURGED}" | |
if [[ -n "${GTID_PURGED}" ]]; then | |
echo "Exec mysql set global gtid_purged..." | |
cat "${XTRABACKUP_INFO}" | grep binlog_pos | awk -F' ' '{print \"set global gtid_purged=\"$12\";\"}' 2>/dev/null | mysql | |
if [ $? -ne 0 ]; then | |
echo "ERROR: MySQL gtid_purged not set. Exit." | |
exit 1 | |
fi | |
else | |
echo "WARN: Gtid position is empty, use fixed binlog file and position" | |
BINLOG_FILE=$(head -1 "${XTRABACKUP_BINLOG_INFO}" | awk {'print $1'} 2>/dev/null) | |
BINLOG_POS=$(head -1 "${XTRABACKUP_BINLOG_INFO}" | awk {'print $2'} 2>/dev/null) | |
USE_AUTOPOSITION=0 | |
fi | |
fi | |
echo >/tmp/slave_init.sql | |
echo "CHANGE MASTER TO MASTER_HOST='${MYSQL_MASTER_HOST}',">>/tmp/slave_init.sql | |
echo "MASTER_USER='${MYSQL_REPL_USER_NAME}',">>/tmp/slave_init.sql | |
echo "MASTER_PASSWORD='${MYSQL_REPL_USER_PASSWORD}',">>/tmp/slave_init.sql | |
if [ ${USE_AUTOPOSITION} -eq 0 ]; then | |
echo "MASTER_LOG_FILE='${BINLOG_FILE}',">>/tmp/slave_init.sql | |
echo "MASTER_LOG_POS=${BINLOG_POS};">>/tmp/slave_init.sql | |
else | |
echo "MASTER_AUTO_POSITION=1;">>/tmp/slave_init.sql | |
fi | |
echo "START SLAVE;">>/tmp/slave_init.sql | |
cat slave_init.sql | mysql | |
echo "SHOW SLAVE STATUS \G;" | mysql | grep -e "Slave_IO_State\|Master_Host\|Slave_IO_Running\|Slave_SQL_Running\|Last_Errno\|Last_Error\|Seconds_Behind_Master\|Slave_SQL_Running_State" 2>/dev/null | |
rm -f /tmp/slave_init.sql |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment