恢复脚本

[root@mha-manager ~]# cat recovery_mha.sh
#!/bin/bash
. /etc/init.d/functions
. /root/failover_status

log_file='/etc/mha/logs/manager.log'
mha_conf="/etc/mha/app1.cnf"
repl_pass='123'
mha_user=$(awk -F= '/^user/{print $2}' ${mha_conf})
mha_password=$(awk -F= '/^password/{print $2}' ${mha_conf})
down_master=$(sed -nr 's#^Master (.*)\(.*\).*!$#\1#gp' ${log_file}|tail -1 )

while true;do
if [ ${down_master} == ${last_new_master} ];then
	change_master=$(grep -i 'change master to' ${log_file}|tail -1|awk -F:'{print $4}'|sed "s#xxx#${repl_pass}#g")
	echo "主库修复中..."
	ssh ${down_master} 'systemctl start mysqld'	
	while true;do
		mysqladmin -u${mha_user} -p${mha_password} -h${down_master} ping &>/dev/null
			if [ $? -eq 0 ];then
				mysql -u${mha_user} -p${mha_password} -h${down_master} -e "${change_master};start slave"
       			echo "主库修复成功..."
				break
			fi
	done
	
	echo "正在补全MHA配置文件..."
	cat > ${mha_conf} <<- EOF
	[server default]
	manager_log=/etc/mha/logs/manager.log
	manager_workdir=/etc/mha/app1
	master_binlog_dir=/app/mysql/data
	password=${mha_password}
	ping_interval=2
	repl_password=123
	repl_user=rep
	ssh_port=22
	ssh_user=root
	user=${mha_user}
	
	[server1]
	hostname=172.16.1.51
	port=3306
	
	[server2]
	hostname=172.16.1.52
	port=3306
	
	[server3]
	hostname=172.16.1.53
	port=3306
	
	[server4]
	hostname=172.16.1.54
	port=3306
	EOF
	
	echo "正在启动MHA..."
	systemctl start mha
	while true;do
		masterha_check_status --conf=${mha_conf} &>/dev/null
		if [ $? -eq 0 ];then
			action "MHA manager启动" /bin/true
			exit 0
		fi
	done
else
	continue
fi
done