vitess.io/vitess@v0.16.2/examples/compose/fix_replication.sh (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2019 The Vitess Authors.
     4  # 
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  # 
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  # 
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # This is a helper script to sync replicas for mysql.
    18  # It handles the special case where the primary has purged bin logs that the replica requires.
    19  # To use it place a mysql dump of the database on the same directory as this script.
    20  # The name of the dump must be $KEYSPACE.sql. The script can also download the mysqldump for you.
    21  # Replication is fixed by restoring the mysqldump and resetting replication.
    22  # https://dev.mysql.com/doc/refman/5.7/en/replication-mode-change-online-disable-gtids.html
    23  # https://www.percona.com/blog/2013/02/08/how-to-createrestore-a-slave-using-gtid-replication-in-mysql-5-6/
    24  
    25  cd "$(dirname "${BASH_SOURCE[0]}")"
    26  
    27  function get_replication_status() {
    28      # Get replication status
    29      STATUS_LINE=$(mysql -u$DB_USER -p$DB_PASS -h 127.0.0.1 -e "SHOW SLAVE STATUS\G")
    30      LAST_ERRNO=$(grep "Last_IO_Errno:" <<< "$STATUS_LINE" | awk '{ print $2 }')
    31      SLAVE_SQL_RUNNING=$(grep "Slave_SQL_Running:" <<< "$STATUS_LINE" | awk '{ print $2 }')
    32      SLAVE_IO_RUNNING=$(grep "Slave_IO_Running:" <<< "$STATUS_LINE" | awk '{ print $2 }')
    33      MASTER_HOST=$(grep "Master_Host:" <<< "$STATUS_LINE" | awk '{ print $2 }')
    34      MASTER_PORT=$(grep "Master_Port:" <<< "$STATUS_LINE" | awk '{ print $2 }')
    35  
    36      echo "Slave_SQL_Running: $SLAVE_SQL_RUNNING"
    37      echo "Slave_IO_Running: $SLAVE_IO_RUNNING"
    38      echo "Last_IO_Errno: $LAST_ERRNO"
    39  }
    40  
    41  function reset_replication() {
    42      # Necessary before sql file can be imported
    43      echo "Importing MysqlDump: $KEYSPACE.sql"
    44      mysql -u$DB_USER -p$DB_PASS -h 127.0.0.1 -e "RESET MASTER;STOP SLAVE;CHANGE MASTER TO MASTER_AUTO_POSITION = 0;source $KEYSPACE.sql;START SLAVE;"
    45      # Restore Master Auto Position
    46      echo "Restoring Master Auto Setting"
    47      mysql -u$DB_USER -p$DB_PASS -h 127.0.0.1 -e "STOP SLAVE;CHANGE MASTER TO MASTER_AUTO_POSITION = 1;START SLAVE;"
    48  }
    49  
    50  # Retrieve replication status
    51  get_replication_status
    52  
    53  # Exit script if called with argument 'status'
    54  [ ${1:-''} != 'status' ] || exit 0;
    55  
    56  # Check if IO_Thread is running
    57  if [[ $SLAVE_IO_RUNNING = "No" && $LAST_ERRNO = 1236 ]]; then
    58      
    59      echo "Primary has purged bin logs that replica requires. Sync will require restore from mysqldump"
    60      if [[ -f $KEYSPACE.sql ]] ; then
    61          echo "mysqldump file $KEYSPACE.sql exists, attempting to restore.."
    62          echo "Resetting replication.."
    63          reset_replication
    64      else
    65          echo "Starting mysqldump. This may take a while.."
    66          # Modify flags to user's requirements
    67          if mysqldump -h $MASTER_HOST -P $MASTER_PORT -u$DB_USER -p$DB_PASS --databases $KEYSPACE \
    68              --triggers --routines --events --hex-blob  --master-data=1 --quick --order-by-primary \
    69              --no-autocommit --skip-comments --skip-add-drop-table --skip-add-locks \
    70              --skip-disable-keys --single-transaction --set-gtid-purged=on --verbose > $KEYSPACE.sql; then
    71              echo "mysqldump complete for database $KEYSPACE"
    72              echo "Resetting replication.."
    73              reset_replication
    74          else
    75              echo "mysqldump failed for database $KEYSPACE"
    76          fi
    77      fi
    78  
    79  else
    80  
    81      echo "No Actions to perform"
    82  
    83  fi