Advertisement
Guest User

Untitled

a guest
Oct 30th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.50 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. source ../config_region1.cfg
  4. source ../config_region2.cfg
  5. source ../lib/functions.sh
  6.  
  7.  
  8. function rebuild_container ()
  9. {
  10.         echocolor "Remove Old MariaDB Container on $1"
  11.         docker rm -f hcm-mariadb-$1
  12.  
  13.         echocolor "Rebuild MariaDB Container on $1"
  14.         sleep 10
  15.         export DB_ROOT_PASSWORD=$MYSQL_PASS
  16.         bash ../files/start-mariadb.sh hcm-mariadb-$1 101
  17.        
  18.         echocolor "Creating tunnel to $2"
  19.         if ! apt-get -qq install byobu; then apt-get install byobu -y; fi
  20.         byobu new-session -d -s ssh_tmp
  21.         byobu new-window -t ssh_tmp:1 -n "controller" "ssh -L 3300:localhost:3306 root@$2 -p $SSH_PORT_HN /bin/bash"
  22.         sleep 30
  23.        
  24.         echocolor "Dumping keystone database from $2"
  25.         mysql -u root -p$MYSQL_PASS -h 127.0.0.1 -P 3300 -e "FLUSH TABLES WITH READ LOCK"
  26.         binlog_file=`mysql -u root -p$MYSQL_PASS -h 127.0.0.1 -P 3300 -e "SHOW MASTER STATUS" | grep binlog | awk '{print $1}'`
  27.         binlog_pos=`mysql -u root -p$MYSQL_PASS -h 127.0.0.1 -P 3300 -e "SHOW MASTER STATUS" | grep binlog | awk '{print $2}'`
  28.  
  29.         mysqldump -u root -p$MYSQL_PASS -h 127.0.0.1 -P 3300 keystone > keystone.sql
  30.  
  31.         echocolor "Restore database to container"
  32.         cat << EOF | mysql -uroot -p$MYSQL_PASS -h 127.0.0.1 -P 3305
  33. CREATE DATABASE keystone;
  34. GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'localhost' IDENTIFIED BY '$KEYSTONE_DBPASS';
  35. GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'%' IDENTIFIED BY '$KEYSTONE_DBPASS';
  36. FLUSH PRIVILEGES;
  37. EOF
  38.         mysql -uroot -p$MYSQL_PASS -h 127.0.0.1 -P 3305 keystone < keystone.sql
  39.         mysql -u root -p$MYSQL_PASS -h 127.0.0.1 -P 3300 -e "UNLOCK TABLES"
  40.  
  41.         echocolor "Start replication"
  42.         cat << EOF | mysql -uroot -p$MYSQL_PASS -h 127.0.0.1 -P 3305
  43. CHANGE MASTER TO
  44.   MASTER_HOST='$2',
  45.   MASTER_USER='repl',
  46.   MASTER_PASSWORD='weascafa221f',
  47.   MASTER_PORT=3306,
  48.   MASTER_LOG_FILE='$binlog_file',
  49.   MASTER_LOG_POS=$binlog_pos,
  50.   MASTER_CONNECT_RETRY=10;
  51.  
  52. START SLAVE;
  53. EOF
  54.         if [ "$?" -eq 0 ]; then echocolor "Start Replicate Success"; fi
  55.         byobu kill-session -t ssh_tmp
  56.  
  57. }    # ----------  end of function create_container  ----------
  58.  
  59.  
  60.  
  61. hostname=`cat /etc/hostname`
  62. if [[ "$hostname" == controller1* ]]; then
  63.     rebuild_container "controller1" "$CTL1_MGNT_IP_HN"
  64. elif [[ "$hostname" == controller2* ]]; then
  65.     rebuild_container "controller2" "$CTL2_MGNT_IP_HN"
  66. else
  67.     echocolor "Run on Controller1 or Controller2"
  68. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement