Advertisement
Guest User

Untitled

a guest
Jan 9th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # Name: MySQL_REBUILD_LIVE_SLAVE_from_MASTER
  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_REBUILD_LIVE_SLAVE_from_MASTER Master:Port Slave:Port {F|P}
  7. # eg. MySQL_REBUILD_LIVE_SLAVE_from_MASTER 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. # Changes: 05 Dec 2008 First implementation of script
  12. # 10 Oct 2013 Added the File(F) transfer method, added --skip-lock-tables to the dump command, added the manual entry of root PW
  13. # Notes 1: In the case of using the Dump file method, the file will be transferred via rsync to the slave for importing.
  14. # Therefore the key of the user running the script must be already installed in slave before running the script
  15. #-----------------------------------------------------------------------------------
  16. # Some constants:
  17. DUMPFILE="/tmp/mydump.sql"
  18. # Resync the databases except the following Databases
  19. EXCEPTIONS="information_schema mysql test"
  20. #
  21. # Functions
  22. # Syntax: testhost addr port. Returns: hostOK=1
  23. testhost () {
  24. hostOK=0
  25. if (nmap -n --unprivileged --host-timeout 1600ms --max_rtt_timeout 1600ms -p $2 -P0 $1 2>/dev/null | grep -q "open" &>/dev/null); then
  26. hostOK=1
  27. fi
  28. };
  29. #
  30. usage () {
  31. echo "ERROR: Somethig is wrong with the given arguments"
  32. echo "Syntax: MySQL_REBUILD_LIVE_SLAVE_from_MASTER Master:Port Slave:port {F|P}"
  33. echo " eg. MySQL_REBUILD_LIVE_SLAVE_from_MASTER master1:3306 slave1:3306 P"
  34. exit 1
  35. }
  36. #
  37. # Check the command syntax
  38. if [ $# -ne 3 ]; then
  39. usage
  40. fi
  41. #
  42. # Get the mysql root password
  43. read -s -p "Please enter the MySQL root password: " rootpw
  44. #
  45. #- Check the hosts info validity
  46. if ! (echo $1 | grep ':'); then
  47. echo "ERROR: The 2nd parameter(master) must be the combination 'host:port'"
  48. exit 3
  49. fi
  50. #
  51. if ! (echo $2 | grep ':'); then
  52. echo "ERROR: The third parameter must be the combination 'host:port'"
  53. exit 4
  54. fi
  55. #
  56. method=$3
  57. #
  58. # Check the hosts connectivity of master host
  59. Mhost=$(echo $1 | cut -d: -f1)
  60. Mport=$(echo $1 | cut -d: -f2)
  61. #
  62. testhost $Mhost $Mport
  63. if [ $hostOK = "0" ]; then
  64. echo "ERROR: The master $Mhost:$Mport does not respond"
  65. exit 5
  66. fi
  67. #
  68. # Check the hosts connectivity of slave host
  69. #
  70. Shost=$(echo $2 | cut -d: -f1)
  71. Sport=$(echo $2 | cut -d: -f2)
  72. #
  73. testhost $Shost $Sport
  74. if [ $hostOK = "0" ]; then
  75. echo "ERROR: The slave $Shost:$Sport does not respond"
  76. exit 6
  77. fi
  78. #
  79. # Stop and reset the slave
  80. echo "STOP SLAVE; RESET SLAVE;" | mysql --host=$Shost --port=$Sport --user=root --password=$rootpw
  81. #
  82. databases=""
  83. for DB in $(echo "show databases;" | mysql --host=$Shost --port=$Sport --user=root --password=$rootpw | grep -v Database) ; do
  84. # Only delete/add the databases that are not in the Exceptions list
  85. if ! (echo $EXCEPTIONS | grep -q $DB); then
  86. # here I was deleting the databases one by one before recreating them in slave
  87. # I replaced this by the option --add-drop-database in mysqldump
  88. #echo "Deleting database $DB on Slave $Shost:$Sport"
  89. #echo "DROP DATABASE $DB;" | mysql -h $Shost --port=$Sport -u root --password=$rootpw
  90. # Corrected. Thanks to Dorian Kavaja
  91. if [ "$databases" == "" ]; then
  92. databases=$DB
  93. else
  94. databases="$databases $DB"
  95. fi
  96. fi
  97. done
  98. # The following lines is thanks to Dorian Kavaja
  99. #because sometimes the log file on the master was changed before the dump was finished
  100. echo "FLUSH LOGS; RESET MASTER;" | mysql -–host=$Mhost –-port=$Mport --user=root –password=$rootpw
  101. #
  102. # Now do the transfer
  103. # Added “–routines –triggers” to the mysqldump command, so triggers and functions were included in the dump.
  104. # Thanks to Dorian Kavaja
  105. case $method in
  106. P) # Transfer all databases from master to slave directly using a pipe(P)
  107. echo "Transfering the all databases from master $Mhost:$Mport into slave $Shost:$Sport directly"
  108. mysqldump --host=$Mhost --port=$Mport --user=root --password=$rootpw \
  109. --single-transaction --flush-logs --master-data=2 --skip-lock-tables \
  110. --add-drop-database --delete-master-logs --hex-blob –-routines –-triggers --databases $databases \
  111. | mysql --host=$Shost --port=$Sport --user=root --password=$rootpw
  112. ;;
  113. #
  114. F) # Transfer the databases using a dump file
  115. echo "Dumping the all databases from master $Mhost:$Mport into file $DUMPFILE"
  116. mysqldump --host=$Mhost --port=$Mport --user=root --password=$rootpw \
  117. --single-transaction --flush-logs --master-data=2 --skip-lock-tables \
  118. --add-drop-database --delete-master-logs --hex-blob –-routines –-triggers --databases $databases > $DUMPFILE
  119. #
  120. echo "Transferring the dump file $DUMPFILE from Master $Mhost to slave $Shost via compressed rsync"
  121. rsync -vz $DUMPFILE $Shost:$DMPFILE
  122. echo "Importing the dump file ($DUMPFILE) into slave MySQL server $Shost"
  123. ssh $Shost "mysql --host=$Shost --port=$Sport --user=root --password=$rootpw < $DUMPFILE"
  124. ;;
  125. #
  126. *) usage ;;
  127. esac
  128. #
  129. # Find out the master binlog file name
  130. masterlogfile=$(echo "SHOW MASTER STATUS\G;" | mysql --host=$Mhost --port=$Mport --user=root --password=$rootpw | grep "File:" | cut -d: -f2 | cut -d" " -f2)
  131. #
  132. # Sync the slave with master binlog position 4
  133. echo "CHANGE MASTER TO master_log_file='$masterlogfile',master_log_pos=4;" | mysql --host=$Shost --port=$Sport --user=root --password=$rootpw
  134. #
  135. # Start the replication on slave
  136. echo "START SLAVE;" | mysql --host=$Shost --port=$Sport --user=root --password=$rootpw
  137. sleep 3
  138. #
  139. # Show slave status to see if all is in sync
  140. echo "SHOW SLAVE STATUS \G;" | mysql --host=$Shost --port=$Sport --user=root --password=$rootpw
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement