You are on page 1of 5

#!

/bin/bash
### Script to setup Master-Master replication or Master-Slave replication by Krishan Kumar
### ***** Start Pre-Req Section *****
### Always run this script on the node which you want to be slave for your master, if you do this
then script will take care of everything automatically for you.
###
### In my case user for replication is repl, you can find and replace it in this script as per your
username.
### For mysqldump, creating backup_usr in case you take backup using mysqldump or if you already
have user than find and replace it with your username in this script
### File $SDIR/backup.conf used to store the password for the user which has all privs, in my case
this user name is admin
### Make sure backup.conf exist on both the nodes i.e. Master and Slave
### Format of backup.conf file is
### mysql_admin_user_passwd=userpassword
### When copy this script from pdf file then SOME LINES WILL SPLIT TO MULTIPLE LINES
### SO ALL THOSE LINES MUST BE IN SINGLE LINE no MySQL OR echo command going accros mulitple lines.
### ***** End Pre-Req Section *****

### Setup some variables


GREP="/bin/grep"
DT=`date +%F_%H%M%S`
VMAIL="youremail@yourcomany.com"
BDIR="/datadir/backupsdir/mysqlbackupdir" ##Define the backup dir where mysqldump backup can be
stored for the replication setup
SDIR="/datadir/backupsdir/scriptsdir" ##Define Scripts dir
HNAME=`hostname -s` ##Hostname which is going to be the Slave in replication
topology
MHNAME=`echo $HNAME|sed 's/02/01/'` ##To get the Master host name if you have a pattern in
your hostnames like hostname01 is master and hostname02 is going to be slave
HOSTIP=`hostname -i` ##Slave host IP address

#Check if Backup Dir exists or not


if [ ! -d $BDIR ]; then
mkdir -p $BDIR
fi
###Check for Master-Master Setup or Master-Slave Setup
read -p "Is this Master-Master Setup [Y|N]: " VRESP

echo -e "\nList of replication and backup users on MASTER node"


/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -h$MHNAME
-e "select user,host from mysql.user where (user like 'repl%' or user='backup_usr') order by 1,2;"

###To alter repl user passwd in case it already exists to make sure it has autogenerated password
/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -h$MHNAME
-e "select user,host from mysql.user where user like 'repl%' order by 1,2;" > /tmp/user.txt
$GREP repl /tmp/user.txt
if [ $? -eq 0 ]; then
/usr/bin/mysql -N -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -
h$MHNAME -e "select concat('ALTER USER ''',user,'''@''',host,'''', ' IDENTIFIED BY
''','rep#${HNAME:4:3}#m1n''',';') from mysql.user where user like 'repl%';" > /tmp/user.sql
/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -
h$MHNAME -v < /tmp/user.sql
###Remove /tmp/user.sql
rm /tmp/user.sql
fi

###Creating dbmonitor user in case you need it, uncomment it


##/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -
h$MHNAME -e "CREATE USER 'dbmonitor'@'%' IDENTIFIED BY '`cat ${SDIR}/backup.conf|${GREP}
mysql_dbmonitor_user_passwd|cut -d "=" -f2`';GRANT SELECT, REPLICATION CLIENT, SHOW DATABASES, SHOW
VIEW, PROCESS, CREATE TEMPORARY TABLES on *.* TO 'dbmonitor'@'192.168.%';"

echo -e "\nCheck if replication and backup_usr users exists or not. If not then create it on MASTER
node: $MHNAME"
echo -e "\nUse following to create replication user:-\ncreate user 'repl'@'${HOSTIP%.*}.%'
IDENTIFIED BY 'rep#${HNAME:4:3}#m1n';GRANT SELECT,REPLICATION SLAVE on *.* TO
'repl'@'${HOSTIP%.*}.%';"

echo -e "\nUse following to create backup_usr user:-\ncreate user 'backup_usr'@'localhost'


IDENTIFIED BY '`$GREP mysql_backup_user_passwd $SDIR/backup.conf |cut -d "=" -f2`';GRANT SELECT,
RELOAD, PROCESS, SHOW DATABASES, LOCK TABLES, REPLICATION CLIENT, SHOW VIEW, EVENT, TRIGGER ON *.*
TO 'backup_usr'@'localhost';"

echo -e "\nValue of server_id variable on node $HNAME, ***Make sure this is correct and unique value
per number of slave you have***\n"
/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -h$HNAME
-e "show variables like 'server_id';"

echo -e "\nValue of auto_increment_% variables on node $MHNAME\n"


/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -h$MHNAME
-e "show variables like 'auto_increment_%';"

echo -e "\nValue of auto_increment_% variables on node $HNAME\n"


/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -h$HNAME
-e "show variables like 'auto_increment_%';"

echo -e "\nFor Master-Master setup only. Value of ***auto_increment_increment*** must be 2 on both


nodes and ***auto_increment_offset*** should be 1 on Master node $MHNAME and 2 on Slave node
$HNAME\n"

echo -e "\nMake sure admin user password is same in backup.conf file on both hosts $MHNAME and
$HNAME\n"

/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -h$MHNAME


-e "show variables like '%info_repository';"
echo -e "\nCheck value of %info_repository, it must be TABLE if not then make the change on BOTH
nodes and restart the mysql server...\n"

read -p "If all looks good then press Enter otherwise CTRL C..." resp

##Get the list of databases


/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -h$MHNAME
-e "show databases"|${GREP} -v "information_schema\|performance_schema\|Database"
DT=`date +%F_%H%M%S`
BFNAME=Replication_setup_mysqldump-$DT.sql
###Remove old error log file
echo -e "====>`date`" >> $SDIR/error.log.old
/bin/cat $SDIR/error.log >> $SDIR/error.log.old
/bin/rm -f $SDIR/error.log
echo -e "\n`date`: Starting database backup on Master host $MHNAME for replication setup.\n"
/usr/bin/mysqldump -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -
h$MHNAME --flush-privileges --flush-logs --events --single-transaction --master-data=2 --all-
databases --log-error $SDIR/error.log > $BDIR/$BFNAME
echo -e "\n`date`: Backup on Master host $MHNAME done\n"

if [ -s $SDIR/error.log ]; then
echo -e "\nMaster node Backup failed for the replication setup...\n"
exit 1
else
echo -e "Master node backup SUCCESSFUL on `hostname` moving on to setup the replication"
echo -e "Master node backup SUCCESSFUL on `hostname` moving on to setup the replication"|mailx -s
"Master node backup SUCCESSFUL on `hostname`" $VMAIL
fi

echo -e "\n`date`: Restoring backup on $HNAME...\n"

/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -


hlocalhost < $BDIR/$BFNAME
echo -e "\n`date`: Restoring backup on $HNAME is done...\n"

${GREP} "\-- CHANGE MASTER TO MASTER_LOG_FILE" $BDIR/$BFNAME


echo
echo
read -p "Enter value of MASTER_LOG_FILE from above: " MASTER_LOG_FILE
read -p "Enter value of MASTER_LOG_POS from above: " MASTER_LOG_POS

CHANGE_MASTER="change master to master_host='$MHNAME', master_user='repl',


master_password='rep#${HNAME:4:3}#m1n',MASTER_LOG_FILE='$MASTER_LOG_FILE',
MASTER_LOG_POS=$MASTER_LOG_POS;"
echo -e "Change master command:\n"
echo -e "$CHANGE_MASTER\n"

/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -


hlocalhost -e "$CHANGE_MASTER"
echo -e "\nStarting slave on $HNAME..\n"
/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -
hlocalhost -e "start slave; show slave status\G"

if [ $? -ne 0 ]; then
echo -e "\nChange master command or start slave failed, please check manually."
echo -e "Not executing setting up replication from $HNAME to $MHNAME\n"
exit 1
fi

if [[ ${VRESP} = "Y" || ${VRESP} = "y" ]]; then


echo -e "\n=============================================================="
echo -e "\Now setting up replication from $HNAME to $MHNAME (Making Master-Master setup)"
echo -e "===============================================================\n"
echo

/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -


hlocalhost -e "show master status"
echo
read -p "Enter value of File from above: " MASTER_LOG_FILE
read -p "Enter value of Position from above: " MASTER_LOG_POS
CHANGE_MASTER="change master to master_host='$HNAME', master_user='repl',
master_password='rep#${HNAME:4:3}#m1n',MASTER_LOG_FILE='$MASTER_LOG_FILE',
MASTER_LOG_POS=$MASTER_LOG_POS;"
echo -e "Change master command:\n"
echo -e "$CHANGE_MASTER\n"
/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -
h$MHNAME -e "$CHANGE_MASTER"
echo -e "\nStarting slave on $MHNAME..\n"
/usr/bin/mysql -uadmin -p`$GREP mysql_admin_user_passwd $SDIR/backup.conf |cut -d "=" -f2` -
h$MHNAME -e "start slave; show slave status\G"

echo -e "\n*******Master-Master Replication setup script is completed. Please check the slave
status on both nodes $MHNAME and $HNAME*******\n"
else
echo -e "\n*******Master-Slave Replication setup script is completed. Please check the slave status
on node $HNAME*******\n"
fi

You might also like