#!/bin/sh
#multi-instance
#usage sh ws.sh $1 $2
#取备份
lastip=`echo $1|awk -F '.' '{print $3$4}'`
echo $lastip
mkdir -p /app/dbbackup/${lastip}
#ftp -i -in <<EOF
#open 1.82.3.74
#user gt 13
#lcd /app/dbbackup/${SOCK}
#mget $1.rar
#bye
#EOF
#sleep 3
#解压取POS值
cd /app/dbbackup/${lastip}
wget ftp://wol_ta:r7q!rBeMM.gu3@11.21.3.7/data$1.rar
#unrar x $1.rar
#pos=`head -n 30 dump.sql |grep -i "change"|awk -F'TO' '{print $2}'`
hostip=`/sbin/ifconfig eth0|grep -i "inet addr"|awk '{print $2}'|awk -F"." '{print $3$4}'`
echo $hostip
cnf=/etc/my.cnf
#配置my.cnf
if [ -f "$cnf" -a ! -f "${cnf}.bak" ];then
cp $cnf $cnf.bak
fi
multi=`cat /etc/my.cnf|grep -i -w "mysqld_multi"`
if [ "${multi}" = "" ];then
sed -i '4a\[mysqld_multi]\nmysqld = /usr/local/mysql/bin/mysqld_safe\n' $cnf
fi
masterip=$1
sed -i '6a\[mysqld'${lastip}']\nsocket = /tmp/mysql.sock'${lastip}'\nport = '$2'\npid\-file = '${hostip}'.pid'${lastip}'\ndatadir = /usr/local/mysql/var'${lastip}'\nrelay\-log = '${hostip}'\-relay\-bin\-'${lastip}'\n' $cnf
#生成数据文件
if [ -d /app/mysql/var ];then
cd /app/mysql
/app/mysql/bin/mysql_install_db --datadir=/app/mysql/var${lastip} --user=mysql
cd /app/mysql/var${lastip}
tar xvf /root/mysql.tar
unrar x /app/dbbackup/${lastip}/data$1.rar
#/app/mysql/bin/mysql_install_db --datadir=/app/mysql/var${lastip} --user=mysql
chown -R mysql.mysql /app/mysql/var${lastip}
/app/mysql/bin/mysqld_multi start ${lastip}
sleep 5
#/app/mysql/bin/mysql -uroot -S /tmp/mysql.sock${lastip} -pmysql_rqf8ks</app/dbbackup/${lastip}/dump.sql
#mysql -S /tmp/mysql.sock${lastip} -pmysql_ro8ks </app/dbbackup/${lastip}/dump.sql
mysql -S /tmp/mysql.sock${lastip} -pmysql_ros -e "slave stop;CHANGE MASTER TO MASTER_HOST='$1',MASTER_USER='repl',MASTER_PASSWORD='repl_SGa90H1H',MASTER_PORT=63306,MASTER_LOG_FILE='mysql.000001',MASTER_LOG_POS=0;slave start;show slave status\G;"
fi