主从同步自动化脚本

来源:互联网 发布:js 多个异步请求 编辑:程序博客网 时间:2024/05/16 02:07
#!/bin/bash# AB replication自动配置脚本# 20160901 Booboo Wei# 执行该脚本需要设置无密钥登陆,例如教学环境中,我在workstation上跑该脚本,那么需要在workstation和主服务器mastera上执行一下操作# 无密钥登陆#ssh-keygen#for i in `seq 10 15`;do ssh-copy-id root@172.25.X.$i;doneIPT(){if [[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]then        IP=(${1//./ })        [ ${IP[0]} -gt 0 -a ${IP[0]} -lt 255 ] && [ ${IP[1]} -ge 0 -a ${IP[1]} -lt 255 ] && [ ${IP[2]} -ge 0 -a ${IP[2]} -lt 255 ] && [ ${IP[3]} -gt 0 -a ${IP[3]} -lt 255 ] && echo yes || echo noelse        echo nofi}INSTALL () {#read -p 'plz input slave ip: ' ip#read -p 'plz input software name: ' packif [ `IPT $sip` = yes ]thenssh root@"$sip" "yum install -y $pack"fi}MASTER () {#read -p 'plz input master-ip :' mip#read -p 'plz input slave-ip: ' sip#read -p 'plz input root-password :' passwordif [ `IPT $mip` = yes ]thenssh root@"$mip" "grep server-id /etc/my.cnf" ||  ssh root@"$mip" "sed -i '/\[mysqld\]/aserver-id=1' /etc/my.cnf"ifssh root@"$mip" "grep log-bin /etc/my.cnf" thenecho log-bin okelsessh root@"$mip" "sed -i '/\[mysqld\]/alog-bin=/var/lib/mysql-log/mastera' /etc/my.cnf" && ssh root@"$mip" "mkdir /var/lib/mysql-log/;chown mysql. /var/lib/mysql-log/;systemctl restart mariadb"fissh root@"$mip" "setenforce 0 ;systemctl stop firewalld"ssh root@"$mip" "mysqldump -uroot -p$password -A --single-transaction --master-data=2 --flush-logs > /tmp/`date +%m%d%H`;scp /tmp/`date +%m%d%H` root@$sip:/tmp"cat >/tmp/gg << ENDFgrant replication slave on *.* to slave@$sip identified by 'uplooking';flush privileges;ENDFscp /tmp/gg root@"$mip":/tmpssh root@"$mip" "mysql -uroot -p$password < /tmp/gg" fi}SLAVE(){#read -p 'plz input master-ip :' mip#read -p 'plz input slave ip :' sip#read -p 'plz input root-password :' passwordssh root@"$sip" "systemctl stop mariadb;rm -rf /var/lib/mysql/*"ssh root@"$sip" "sed -n '22p' /tmp/`date +%m%d%H`>/tmp/cc;cut -d ' ' -f 5 /tmp/cc;cut -d ' ' -f 6 /tmp/cc" >/tmp/mlssh root@"$sip" "grep server-id /etc/my.cnf" ||  ssh root@"$sip" "sed -i '/\[mysqld\]/aserver-id=3' /etc/my.cnf"ssh root@"$sip" "setenforce 0 ;systemctl stop firewalld;systemctl restart mariadb;systemctl status mariadb"cat > /tmp/ss <<ENDFchange master to master_host='$mip',master_user='slave',master_password='uplooking',start slave;ENDFsed -i '1r /tmp/ml' /tmp/ssscp /tmp/ss root@"$sip":/tmpssh root@"$sip" "mysql < /tmp/`date +%m%d%H`"ssh root@"$sip" "echo flush privileges|mysql"ssh root@"$sip" "mysql -uroot -p$password < /tmp/ss"ssh root@"$sip" "echo show slave status'\G'|mysql -uroot -p$password"}read -p 'plz input master-ip :' mipread -p 'plz input slave ip :' sipread -p 'plz input root-password :' passwordread -p 'plz input software name: ' packINSTALLMASTERSLAVE

0 0