1 简介
有N0+组MM节点,如果每次都是手动修复,一次两次还好,次数多了难免有些无用功了,因为建立自动脚本,避免每次都在M库手动备份,手动copy,手动建立连接,手动设置复制信息的过程,我的目的,一个sh run command,所有的事情都准备妥当了,然后就泡一杯雀巢咖啡或者普洱茶,在一边静静的等待或者还可以借此小憩片刻,看着M2修复成功:
2 环境
必须是MM架构,而且一台主库M1是完好无损的,一台主库M2可以根据M1主库来进行重建;如果MS架构,自己可以稍微做一下脚本修改动作,也能使用,架构如下图所示:
3 总体思路,建立主脚本a_build_rep.sh
思路分为13个步骤,如a_build_rep.sh脚本中的备注
#!/bin/bash set -x set -e set -u #(1)准备好目录以及全局变量 BACKUP_FOLDER=/mysqldata/shared/backup2 TEMP_SETUP_FOLDER=/tmp/sharding_setup_1 MYSQL_EXEC=/opt/mysql/product/mysql/bin/mysql #(2)设置参数,$1是要修复的M2库,$2是正在运行良好的M1库 db1=$2 db2=$1 #(3)在这里设置好一些所必用的环境变量,比如备份用户,备份用户密码,以及复制帐号密码,甚至一些super帐号 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$' #(4)由于磁盘空间所限,建立这些目录,如果目录已经存在,则清空这些目录。 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" #(5)将在线备份脚本 create_hot_backup.sh copy到即将要进行在线备份的M1库上面,并且调用此脚本进行在线备份,此备份用来去另一个M2库上进行恢复。 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" #(6)停止M2的MySQL服务 echo "Hotbackup completed. Now restore the hotbackup on db2..." ssh -t $db2 "sudo /etc/init.d/mysql stop" #(7)将restore_hot_backup.sh脚本copy到M2库上,并调用此脚本在M2库上面进行恢复操作。 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" #(8)恢复完,启动M2库。 ssh -t $db2 "sudo /etc/init.d/mysql start" echo "Restore completed. Now rebuild replication between db1 and db2..." #(9)建立M2上面的复制功能,M1为master,M2为slave; 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;" #(10)Check M2库的复制功能状态,是否搭建成功 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 #(11)建立M1上面的复制功能,M2为master,M1为slave; echo "Setup replication from db2 to db1" ./build_replication.sh $db2 $db1 #(12)Check M1库的复制功能状态,是否搭建成功 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 #(13)清除临时目录的备份文件,释放磁盘空间 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详情
#!/bin/bash set -x set -e # !!! This file need to be run in mysql !!! #(1)设置一些基本参数,$1:备份文件;$2:备份用户名;$3:备份用户名密码;$4:是否需要安全模式备份; $5:是否需要rsync信息 BACKUP_FOLDER=$1 HOTBACKUP_USER=$2 HOTBACKUP_PASSWORD=$3 NEED_SAFE_SLAVE=$4 NEED_RSYNC=$5 #(2)参数验证,如果需要,则在接下来的备份命令中加上--safe-slave-backup或者--rsync参数 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 #(3)开始执行备份命令 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库上进行数据恢复操作。
#!/bin/bash set -x set -e #(1)$1:备份数据存放的目录;$2:恢复用户;$3:恢复用户密码; $4:临时文件夹目录; $5:是否需要resync; SHARED_STORAGE_PATH=$1 HOTBACKUP_USER=$2 HOTBACKUP_PASSWORD=$3 TEMP_SETUP_FOLDER=$4 NEED_RSYNC=$5 #(2)是否需要加上resync参数 INNOBACKUP_OPT="" if [[ $NEED_RSYNC == "Y" ]] then INNOBACKUP_OPT="--rsync" fi #(3)把MYSQL_HOME放入环境变量中 export MYSQL_HOME=/opt/mysql/product/mysql #(4)如果目录存在的话,删除这些数据库目录下的旧文件 echo "Delete existing mysql instance..." rm -rf /mysqldata/data rm -rf /mysqldata/shared/restore rm -rf /mysqldata/binlog/* rm -rf /mysqldata/iblogs/* #(5)开始准备数据目录以及备份数据目录,如今不存在就新建,并且赋予linux系统帐号mysql的操作权限。 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) #这个ls的找最新生成的文件的命令很实用,可以借鉴。 #(6)如果为了安全起见可以备份文件数据目录copy到指定的恢复临时目录,然后在临时目录进行--apply-log以及--copy-back等操作,但是我为了效率,就去掉了copy的时间(copy时间大概需要1小时左右),直接在原来的备份数据目录进行--apply-log以及--copy-back操作 # cp -r $SHARED_STORAGE_PATH/${backup_folder}/* /mysqldata/shared/restore/ # innobackupex --user=$HOTBACKUP_USER --password=$HOTBACKUP_PASSWORD --ibbackup xtrabackup --apply-log /mysqldata/shared/restore/ # innobackupex --user=$HOTBACKUP_USER --password=$HOTBACKUP_PASSWORD $INNOBACKUP_OPT --copy-back /mysqldata/shared/restore/ 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}/ #(7)将原来的备份中的复制点信息copy到临时目录下,并赋予对copy后的文件赋予相应的权限。 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 #(8)恢复成功后,删除原来的临时目录的备份数据,释放磁盘空间,如果你磁盘空间足够,这一步可以省略。 echo "Remove backup files..." rm -rf $SHARED_STORAGE_PATH/${backup_folder}/
6 搭建M1上面的复制,M2为master,M1为slave,MM架构。
#!/bin/sh set -x set -u set -e #(1)$1:master主库服务器主机名或者IP地址; $2:slave主库服务器主机名或者IP地址; MASTER_SERVER=$1 SLAVE_SERVER=$2 MYSQL_CNF_DIR='/opt/mysql/product/mysql/etc' MYSQL_EXEC='/opt/mysql/product/mysql/bin/mysql' #(2)从$1主库上面获取复制点信息 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") #(3)执行搭建复制sql命令操作: 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; \""
在根目录下,执行
[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]# tail -f rebuild.log + 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!
刚学会使用shell不久,这是手动写的最长的sh脚本行,比较粗糙,不足的地方,欢迎大家指点,提出宝贵的意见,俺一定虚心接受,嘿嘿:
[备注]:线上100G的M1,运用此脚本搭建M2大概用了2小时20分钟左右。