标签:
必须是MM架构,而且一台主库M1是完好无损的,一台主库M2可以根据M1主库来进行重建;如果MS架构,自己可以稍微做一下脚本修改动作,也能使用,架构如下图所示:
3 总体思路,建立主脚本a_build_rep.sh
思路分为13个步骤,如a_build_rep.sh脚本中的备注
- set -x
- set -e
- set -u
-
- BACKUP_FOLDER=/mysqldata/shared/backup2
- TEMP_SETUP_FOLDER=/tmp/sharding_setup_1
- MYSQL_EXEC=/opt/mysql/product/mysql/bin/mysql
-
- db1=$2
- db2=$1
-
-
- BACKUP_USER="backupuser"
- BACKUP_PASSWORD="#xx$"
- AGENT_PASSWORD=‘#xx$‘
- REPLICATION_USER=replication
- REPLICATION_PASSWORD=‘#xx$‘
- REPLICA_PASSWORD=‘#xx$‘
- MONITOR_PASSWORD=‘#xx$‘
- WRITER_ETH=‘eth0‘
- SUPER_USER=backupuser
- SUPER_PASSWORD=‘#xx$‘
-
- ssh -t $db1 "rm -rf $TEMP_SETUP_FOLDER && mkdir -p $TEMP_SETUP_FOLDER && chmod 777 $TEMP_SETUP_FOLDER"
- ssh -t $db2 "rm -rf $TEMP_SETUP_FOLDER && mkdir -p $TEMP_SETUP_FOLDER && chmod 777 $TEMP_SETUP_FOLDER"
-
-
- echo "Taking hotbackup on db1..."
- scp create_hot_backup.sh $db1:$TEMP_SETUP_FOLDER/create_hot_backup.sh
-
-
- ssh -t $db1 "sudo -u mysql mkdir -p $BACKUP_FOLDER"
- ssh -t $db1 "sudo -i -u mysql $TEMP_SETUP_FOLDER/create_hot_backup.sh ‘$BACKUP_FOLDER‘ ‘$BACKUP_USER‘ ‘$BACKUP_PASSWORD‘ N Y"
-
- echo "Hotbackup completed. Now restore the hotbackup on db2..."
- ssh -t $db2 "sudo /etc/init.d/mysql stop"
- scp restore_hot_backup.sh $db2:$TEMP_SETUP_FOLDER/restore_hot_backup.sh
- ssh -t $db2 "sudo -i -u mysql $TEMP_SETUP_FOLDER/restore_hot_backup.sh ‘$BACKUP_FOLDER‘ ‘$BACKUP_USER‘ ‘$BACKUP_PASSWORD‘ ‘$TEMP_SETUP_FOLDER‘ Y"
- ssh -t $db2 "sudo /etc/init.d/mysql start"
- echo "Restore completed. Now rebuild replication between db1 and db2..."
- echo "Setup replication from db1 to db2"
- echo "Setup replication from db1 to db2"
- scp $db2:$TEMP_SETUP_FOLDER/xtrabackup_binlog_info.master xtrabackup_binlog_info.db1
- binlog_filename=`cat xtrabackup_binlog_info.db1 | awk ‘{print $1}‘`
- binlog_pos=`cat xtrabackup_binlog_info.db1 | awk ‘{print $2}‘`
- $MYSQL_EXEC -h$db2 -ummm_agent -p"$AGENT_PASSWORD" -e "SLAVE STOP;"
- $MYSQL_EXEC -h$db2 -ummm_agent -p"$AGENT_PASSWORD" -e "CHANGE MASTER TO MASTER_HOST=‘$db1‘, MASTER_PORT=3306, MASTER_USER=‘replication‘, MASTER_PASSWORD=‘$REPLICATION_PASSWORD‘, MASTER_LOG_FILE=‘$binlog_filename‘, MASTER_LOG_POS=$binlog_pos;"
- $MYSQL_EXEC -h$db2 -ummm_agent -p"$AGENT_PASSWORD" -e "SLAVE START;"
-
- echo "Check db2 replication status."
- if ! $MYSQL_EXEC -h$db2 -ummm_agent -p"$AGENT_PASSWORD" -e "SHOW SLAVE STATUS\G" | grep "Slave_SQL_Running: Yes"
- then
- echo "[ERROR] Cannot start slave on db2!"
- exit -1
- fi
-
- echo "Setup replication from db2 to db1"
- ./build_replication.sh $db2 $db1
-
- echo "Check db1 replication status."
- if ! $MYSQL_EXEC -h$db1 -ummm_agent -p"$AGENT_PASSWORD" -e "SHOW SLAVE STATUS\G" | grep "Slave_SQL_Running: Yes"
- then
- echo "[ERROR] Cannot start slave on db1!"
- exit -1
- fi
-
- echo "Clean up the shared storage folder and tmp folder."
- ssh -t $db1 "rm -rf $TEMP_SETUP_FOLDER"
- ssh -t $db2 "rm -rf $TEMP_SETUP_FOLDER"
- ssh -t $db1 "sudo -u mysql rm -rf $BACKUP_FOLDER"
-
-
- echo ‘Rebuild secondary done!‘
4 分脚本(1),M1库上的在线备份脚本create_hot_backup.sh详情
- set -x
- set -e
-
- BACKUP_FOLDER=$1
- HOTBACKUP_USER=$2
- HOTBACKUP_PASSWORD=$3
- NEED_SAFE_SLAVE=$4
- NEED_RSYNC=$5
-
- INNOBACKUP_OPT=""
- if [[ $NEED_SAFE_SLAVE == "Y" ]]
- then
- INNOBACKUP_OPT=$INNOBACKUP_OPT" --safe-slave-backup"
- fi
-
- if [[ $NEED_RSYNC == "Y" ]]
- then
- INNOBACKUP_OPT=$INNOBACKUP_OPT" --rsync"
- fi
-
- echo "Run xtrabackup to take hotbackup..."
- export MYSQL_HOME=/opt/mysql/product/mysql
- innobackupex $INNOBACKUP_OPT --user=$HOTBACKUP_USER --password=$HOTBACKUP_PASSWORD $BACKUP_FOLDER
5 分脚本(2)restore_hot_backup.sh,在M2库上进行数据恢复操作。
- set -x
- set -e
-
-
- SHARED_STORAGE_PATH=$1
- HOTBACKUP_USER=$2
- HOTBACKUP_PASSWORD=$3
- TEMP_SETUP_FOLDER=$4
- NEED_RSYNC=$5
-
- INNOBACKUP_OPT=""
- if [[ $NEED_RSYNC == "Y" ]]
- then
- INNOBACKUP_OPT="--rsync"
- fi
-
- export MYSQL_HOME=/opt/mysql/product/mysql
-
- echo "Delete existing mysql instance..."
- rm -rf /mysqldata/data
- rm -rf /mysqldata/shared/restore
- rm -rf /mysqldata/binlog/*
- rm -rf /mysqldata/iblogs/*
-
- echo "Apply and copy back backup files..."
- mkdir -p /mysqldata/data && chown -R mysql:mysql /mysqldata/data && chmod 700 /mysqldata/data
- mkdir -p /mysqldata/shared/restore
- backup_folder=$(ls -1 $SHARED_STORAGE_PATH | sort -rn | head -n1)
-
-
- innobackupex --user=$HOTBACKUP_USER --password=$HOTBACKUP_PASSWORD --ibbackup xtrabackup --apply-log $SHARED_STORAGE_PATH/${backup_folder}/
- innobackupex --user=$HOTBACKUP_USER --password=$HOTBACKUP_PASSWORD $INNOBACKUP_OPT --copy-back $SHARED_STORAGE_PATH/${backup_folder}/
-
- cp -f $SHARED_STORAGE_PATH/${backup_folder}/xtrabackup_binlog_info $TEMP_SETUP_FOLDER/xtrabackup_binlog_info.master
- chmod 644 $TEMP_SETUP_FOLDER/xtrabackup_binlog_info.master
-
- echo "Remove backup files..."
- rm -rf $SHARED_STORAGE_PATH/${backup_folder}/
6 搭建M1上面的复制,M2为master,M1为slave,MM架构。
- set -x
- set -u
- set -e
- MASTER_SERVER=$1
- SLAVE_SERVER=$2
-
- MYSQL_CNF_DIR=‘/opt/mysql/product/mysql/etc‘
- MYSQL_EXEC=‘/opt/mysql/product/mysql/bin/mysql‘
-
- master_file=$(ssh -t $MASTER_SERVER "$MYSQL_EXEC -u$SUPER_USER -p$SUPER_PASSWORD -e ‘SHOW MASTER STATUS\G‘" | grep "File" | awk ‘{print $2}‘)
- master_file=$(sed -e ‘s/[\r\n]//‘ <<<"$master_file")
- master_pos=$(ssh -t $MASTER_SERVER "$MYSQL_EXEC -u$SUPER_USER -p$SUPER_PASSWORD -e ‘SHOW MASTER STATUS\G‘" | grep "Position" | awk ‘{print $2}‘)
- master_pos=$(sed -e ‘s/[\r\n]//‘ <<<"$master_pos")
-
- ssh -t $SLAVE_SERVER "$MYSQL_EXEC -u$SUPER_USER -p$SUPER_PASSWORD -e \"
- STOP SLAVE;
- RESET SLAVE;
- CHANGE MASTER TO master_host=‘$MASTER_SERVER‘, master_port=3306, master_user=‘$REPLICATION_USER‘,
- master_password=‘$REPLICATION_PASSWORD‘, master_log_file=‘$master_file‘, master_log_pos=$master_pos;
- START SLAVE;
- \""
7 执行操作
在根目录下,执行
[root@xx-control xx]# nohup /home/cc/a_build_rep.sh m2.xx.com m1-.xx.com . > rebuild.log &,放在后台执行,使用nohup以防止屏幕突然失效。
然后用 tail -f rebuild.log 查看进展:
- [root@xx-control xx]
- + set -e
- + BACKUP_FOLDER=/mysqldata/shared/backup
- Run xtrabackup to take hotbackup...
- + HOTBACKUP_USER=backupuser
- + HOTBACKUP_PASSWORD=‘#xxx$‘
- + NEED_SAFE_SLAVE=N
- + NEED_RSYNC=Y
- + INNOBACKUP_OPT=
- + [[ N == \Y ]]
- + [[ Y == \Y ]]
- + INNOBACKUP_OPT=‘ --rsync‘
- + echo ‘Run xtrabackup to take hotbackup...‘
- + export MYSQL_HOME=/opt/mysql/product/mysql
- + MYSQL_HOME=/opt/mysql/product/mysql
- + innobackupex --rsync --user=user‘--password=#xxx$‘ /mysqldata/shared/backup
- .................................................................................
- ................................................................................
- Rebuild secondary done!
[MySQL-MM] 生产环境自动恢复MM中一台M2库的过程,分享从零开始写的自动化重建脚本以及思路 (转)
标签:
原文地址:http://www.cnblogs.com/moss_tan_jun/p/5752489.html