Advertisement
Guest User

Untitled

a guest
Oct 15th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #!/bin/bash
  2. #title : replication-start.sh
  3. #description : This script automates the process of starting a Mysql Replication on 1 master node and N slave nodes.
  4. #author : Nicolas Di Tullio
  5. #date : 20160706
  6. #version : 0.2
  7. #usage : bash mysql_replication_autostart.sh
  8. #bash_version : 4.3.11(1)-release
  9. #=============================================================================
  10.  
  11. #
  12. # Requirements for this script to work:
  13. # * The Mysql user defined by the $USER variable must:
  14. # - Have the same password $PASS on all mysql instances
  15. # - Be able to grant replication privileges
  16. # - All hosts must be able to receive mysql commands remotely from the node executing this script
  17. #
  18.  
  19. DB=djangodb
  20. DUMP_FILE="/tmp/$DB-export-$(date +"%Y%m%d%H%M%S").sql"
  21.  
  22. USER=root
  23. PASS=root
  24.  
  25. MASTER_HOST=192.168.0.201
  26. SLAVE_HOSTS=(192.168.0.202 192.168.0.203)
  27.  
  28. ##
  29. # MASTER
  30. # ------
  31. # Export database and read log position from master, while locked
  32. ##
  33.  
  34. echo "MASTER: $MASTER_HOST"
  35.  
  36. mysql -h $MASTER_HOST "-u$USER" "-p$PASS" $DB <<-EOSQL &
  37. GRANT REPLICATION SLAVE ON *.* TO '$USER'@'%' IDENTIFIED BY '$PASS';
  38. FLUSH PRIVILEGES;
  39. FLUSH TABLES WITH READ LOCK;
  40. DO SLEEP(3600);
  41. EOSQL
  42.  
  43. echo " - Waiting for database to be locked"
  44. sleep 3
  45.  
  46. # Dump the database (to the client executing this script) while it is locked
  47. echo " - Dumping database to $DUMP_FILE"
  48. mysqldump -h $MASTER_HOST "-u$USER" "-p$PASS" --opt $DB > $DUMP_FILE
  49. echo " - Dump complete."
  50.  
  51. # Take note of the master log position at the time of dump
  52. MASTER_STATUS=$(mysql -h $MASTER_HOST "-u$USER" "-p$PASS" -ANe "SHOW MASTER STATUS;" | awk '{print $1 " " $2}')
  53. LOG_FILE=$(echo $MASTER_STATUS | cut -f1 -d ' ')
  54. LOG_POS=$(echo $MASTER_STATUS | cut -f2 -d ' ')
  55. echo " - Current log file is $LOG_FILE and log position is $LOG_POS"
  56.  
  57. # When finished, kill the background locking command to unlock
  58. kill $! 2>/dev/null
  59. wait $! 2>/dev/null
  60.  
  61. echo " - Master database unlocked"
  62.  
  63. ##
  64. # SLAVES
  65. # ------
  66. # Import the dump into slaves and activate replication with
  67. # binary log file and log position obtained from master.
  68. ##
  69.  
  70. for SLAVE_HOST in "${SLAVE_HOSTS[@]}"
  71. do
  72. echo "SLAVE: $SLAVE_HOST"
  73. echo " - Creating database copy"
  74. mysql -h $SLAVE_HOST "-u$USER" "-p$PASS" -e "DROP DATABASE IF EXISTS $DB; CREATE DATABASE $DB;"
  75. scp $DUMP_FILE $SLAVE_HOST:$DUMP_FILE >/dev/null
  76. mysql -h $SLAVE_HOST "-u$USER" "-p$PASS" $DB < $DUMP_FILE
  77.  
  78. echo " - Setting up slave replication"
  79. mysql -h $SLAVE_HOST "-u$USER" "-p$PASS" $DB <<-EOSQL &
  80. STOP SLAVE;
  81. CHANGE MASTER TO MASTER_HOST='$MASTER_HOST',
  82. MASTER_USER='$USER',
  83. MASTER_PASSWORD='$USER',
  84. MASTER_LOG_FILE='$LOG_FILE',
  85. MASTER_LOG_POS=$LOG_POS;
  86. START SLAVE;
  87. EOSQL
  88. # Wait for slave to get started and have the correct status
  89. sleep 2
  90. # Check if replication status is OK
  91. SLAVE_OK=$(mysql -h $SLAVE_HOST "-u$USER" "-p$PASS" -e "SHOW SLAVE STATUS\G;" | grep 'Waiting for master')
  92. if [ -z "$SLAVE_OK" ]; then
  93. echo " - Error ! Wrong slave IO state."
  94. else
  95. echo " - Slave IO state OK"
  96. fi
  97. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement