Advertisement
Guest User

Untitled

a guest
Jan 26th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. #!/bin/bash
  2. # Name: mysql-slave-resync
  3. # Purpose: Rebuilds live all Databases of a replication SLAVE based on a good running MASTER
  4. # This rebuilt action is done without stopping the MASTER MySQL server or even locking the tables
  5. # or the destination server (SLAVE)
  6. # Syntax: mysql-slave-resync Master:Port Slave:Port {F|P}
  7. # eg. mysql-slave-resync db1:3306 www1:3306 P
  8. # {F|P} - Dump method F) Uses a full temporary dump file method P) Uses the pipe method
  9. # Note: In some situation where the databases are very big (GB) the Pipe (P) method might not work well
  10. # In these case it is recommended to use the File(F) method
  11. # Assumption:
  12. # * Installed: nmap rsync ssh
  13. # * Both MySQL servers share the same user and password combination
  14. # * SSH access for the same username on slave host in case of rsync synchronization
  15. #-----------------------------------------------------------------------------------
  16.  
  17. # Some constants:
  18. DUMPFILE="/tmp/mydump.sql"
  19.  
  20. # Resync the databases except the following Databases
  21. EXCEPTIONS="information_schema mysql test"
  22.  
  23. #
  24. # Functions
  25. # Syntax: testhost addr port. Returns: hostOK=1
  26. testHost () {
  27. hostOK=0
  28. if (nmap -n --host_timeout 1600 --max_rtt_timeout 1600 -p $2 -P0 $1 2>/dev/null | grep -q "open" &>/dev/null); then
  29. hostOK=1
  30. fi
  31. };
  32.  
  33. #
  34. usage () {
  35. echo "ERROR: Something is wrong with the given arguments"
  36. echo "Syntax: mysql-slave-resync Master:Port Slave:port {F|P}"
  37. echo " eg. mysql-slave-resync master1:3306 slave1:3306 P"
  38. exit 1
  39. }
  40.  
  41. #
  42. # Check the command syntax
  43. if [ $# -ne 3 ]; then
  44. usage
  45. fi
  46.  
  47.  
  48. read -p "Please enter MySQL username: " user
  49. #
  50. # Get the mysql password
  51. read -s -p "Please enter the MySQL ${user} password: " userPasswd
  52.  
  53. #
  54. #- Check the hosts info validity
  55. if ! (echo $1 | grep ':'); then
  56. echo "ERROR: The 2nd parameter(master) must be the combination 'host:port'"
  57. exit 3
  58. fi
  59.  
  60. #
  61. if ! (echo $2 | grep ':'); then
  62. echo "ERROR: The third parameter must be the combination 'host:port'"
  63. exit 4
  64. fi
  65.  
  66. #
  67. method=$3
  68.  
  69. #
  70. # Check the hosts connectivity of master host
  71. MasterHost=$(echo $1 | cut -d: -f1)
  72. MasterPort=$(echo $1 | cut -d: -f2)
  73.  
  74. #
  75. testhost ${MasterHost} ${MasterPort}
  76. if [ ${hostOK} = "0" ]; then
  77. echo "ERROR: The master $MasterHost:$MasterPort does not respond"
  78. exit 5
  79. fi
  80.  
  81. #
  82. # Check the hosts connectivity of slave host
  83. #
  84. SlaveHost=$(echo $2 | cut -d: -f1)
  85. SlavePort=$(echo $2 | cut -d: -f2)
  86.  
  87. #
  88. testhost ${SlaveHost} ${SlavePort}
  89. if [ ${hostOK} = "0" ]; then
  90. echo "ERROR: The slave $SlaveHost:$SlavePort does not respond"
  91. exit 6
  92. fi
  93.  
  94. #
  95. # Stop and reset the slave
  96. echo "STOP SLAVE; RESET SLAVE;" | mysql -h ${SlaveHost} --port=${SlavePort} -u ${user} --password=${userPasswd}
  97.  
  98. #
  99. databases=""
  100. for DB in $(echo "show databases;" | mysql -h ${SlaveHost} --port=${SlavePort} -u ${user} --password=${userPasswd} | grep -v Database) ; do
  101. # Only delete/add the databases that are not in the Exceptions list
  102. if ! (echo ${EXCEPTIONS} | grep -q ${DB}); then
  103. if [ "$databases" == "" ]; then
  104. databases=${DB}
  105. else
  106. databases=${databases}' '${DB}
  107. fi
  108. fi
  109. done
  110.  
  111. echo "FLUSH LOGS;" | mysql --host=${MasterHost} --port=${MasterPort} -u ${user} --password=${userPasswd}
  112. # Not safe and will possibly break other replications
  113. #echo “RESET MASTER;” | mysql –host=$MasterHost –port=$MasterPort -u $user –password=$userPasswd
  114.  
  115. #
  116. # Find out the master binlog file name
  117. MasterLogFile=$(echo "SHOW MASTER STATUS\G;" | mysql --host=${MasterHost} --port=${MasterPort} -u ${user} --password=${userPasswd} | grep "File:" | cut -d: -f2 | cut -d" " -f2)
  118. MasterLogPosition=$(echo "SHOW MASTER STATUS\G;" | mysql --host=${MasterHost} --port=${MasterPort} -u ${user} --password=${userPasswd} | grep "Position:" | cut -d: -f2 | cut -d" " -f2)
  119.  
  120. #
  121. # Now do the transfer
  122. case ${method} in
  123. P) # Transfer all databases from master to slave directly using a pipe(P)
  124. echo "Transferring the all databases from master ${MasterHost}:${MasterPort} into slave ${SlaveHost}:${SlavePort} directly"
  125. mysqldump -h ${MasterHost} --port=${MasterPort} -u ${user} --password=${userPasswd} \
  126. --single-transaction --flush-logs --master-data=2 --skip-lock-tables \
  127. --add-drop-database --delete-master-logs --hex-blob --routines --triggers --databases ${databases} \
  128. | mysql -h ${SlaveHost} --port=${SlavePort} -u ${user} --password=${userPasswd}
  129. ;;
  130. #
  131. F) # Transfer the databases using a dump file
  132. echo "Dumping the all databases from master ${MasterHost}:${MasterPort} via dumpfile ${DUMPFILE}"
  133. mysqldump -h ${MasterHost} --port=${MasterPort} -u ${user} --password=${userPasswd} \
  134. --single-transaction --flush-logs --master-data=2 --skip-lock-tables \
  135. --add-drop-database --delete-master-logs --hex-blob --routines --triggers --databases ${databases} > ${DUMPFILE}
  136. #
  137. echo "Transferring the dump file ${DUMPFILE} from Master ${MasterHost} to slave ${SlaveHost} via compressed rsync"
  138. rsync -vz ${DUMPFILE} ${SlaveHost}:${DUMPFILE}
  139. echo "Importing the dump file (${DUMPFILE}) into slave MySQL server ${SlaveHost}"
  140. ssh ${SlaveHost} "mysql -h ${SlaveHost} --port=${SlavePort} -u ${user} --password=${userPasswd} < ${DUMPFILE}"
  141. ;;
  142. #
  143. *) usage ;;
  144. esac
  145.  
  146. #
  147. # Sync the slave with master binlog position 4
  148. echo "CHANGE MASTER TO master_log_file='${MasterLogFile}',master_log_pos=${MasterLogPosition};" | mysql -h ${SlaveHost} --port=${SlavePort} -u ${user} --password=${userPasswd}
  149.  
  150. #
  151. # Start the replication on slave
  152. echo "START SLAVE;" | mysql -h ${SlaveHost} --port=${SlavePort} -u ${user} --password=${userPasswd}
  153. sleep 3
  154.  
  155. #
  156. # Show slave status to see if all is in sync
  157. echo "SHOW SLAVE STATUS \G;" | mysql -h ${SlaveHost} --port=${SlavePort} -u ${user} --password=${userPasswd}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement