#!/bin/bash
#install expect rsync
color_green="echo -e \033[1;32m"
color_red="echo -e \033[1;31m"
color_end="\033[0m"
for file in /etc/yum.repos.d/*.repo ;do
if [ ! -e ${file} ];then
${color_red}"请配置epel源"${color_end}; exit 1
fi
done
grep 'epel' /etc/yum.repos.d/*.repo && { yum makecache || { ${color_red}"yum不可用"${color_end};exit 1; } } || { echo "epel未配置";exit 1; }
${color_green}"install expect rsync start...."${color_end}
yum -y -q install expect rsync expect >/dev/null || ${color_red}"package install failure "${color_end}
#确认数据库类型
db_type=`rpm -qa |grep -E 'mysql|mariadb' |awk -F"-" '{if(NR == 1)print $1}'`
if [[ $db_type =~ mariadb ]];then
systemctl stop $db_type
else
db_type="mysqld"
systemctl stop mysqld
fi
#创建备份端的备份目录
read -p "mysql-backup the IPaddres:" mysql_backup
read -p "mysql-backup the password:" -s Password_1
read -p "mysql-master the IPaddres:" mysql_master
read -p "mysql-master the password:" -s Password
expect <<EOF
set timeout 20
spawn ssh-keygen
expect {
"Enter" { send "\n";exp_continue }
"Enter" { send "\n"}
"Enter" { send "\n"}
}
expect eof
EOF
expect <<EOF
set timeout 20
spawn scp /root/.ssh/id_rsa.pub ${mysql_backup}:/root/.ssh
expect {
"password" { send "${Password_1}\n" }
}
expect eof
EOF
expect <<EOF
set timeout 20
spawn ssh root@${mysql_backup}
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "${Password_1}\n" }
}
expect "#" { send "mkdir -p /backup/mysql-backup/\n" }
expect "#" { send "cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys\n" }
expect "#" { send "yum -y -q install rsync opensll >/dev/null \n" }
expect "#" { send "ssh-keygen -t rsa -f /root/sshkey -q\n" }
expect "passphrase" { send "\n" }
expect "Enter" { send "\n" }
expect "#" { send "scp /root/sshkey.pub ${mysql_master}:/root\n" }
expect "password" { send "${Password}\n" }
expect "#" { send "exit\n" }
expect eof
EOF
#备份mysql文件至目标端
if [ -e /root/sshkey.pub ];then
cat /root/sshkey.pub |tee /root/.ssh/authorized_keys
rsync -a /etc/my.cnf ${mysql_backup}:/backup/mysql-backup/
rsync -a /etc/my.cnf.d ${mysql_backup}:/backup/mysql-backup/
rsync -a /var/lib/mysql ${mysql_backup}:/backup/mysql-backup/
else
echo "backup failure";exit 1
fi
#重新启动数据库
systemctl start $db_type
mysql冷迁移
于 2022-03-30 22:47:18 首次发布