github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/repository/charms/mysql/hooks/slave-relation-changed (about) 1 #!/bin/sh 2 3 set -e 4 5 ROOTARGS="-uroot -p`cat /var/lib/mysql/mysql.passwd`" 6 7 # Others can join that service but only the lowest will be the master 8 # Note that we could be more automatic but for now we will wait for 9 # service and unit settings to define a "master override" to allow 10 # migrating slaves selectively 11 master=`relation-list | head -1` 12 if [ "$JUJU_REMOTE_UNIT" != "$master" ] ; then 13 exit 0; 14 fi 15 echo master=$master 16 17 for setting in user password hostname port dumpurl; do 18 value=`relation-get $setting` 19 if [ -z "$value" ] ; then 20 echo "$setting is empty! Aborting" 21 exit 0 22 fi 23 echo $setting=$value 24 eval $setting=$value 25 done 26 27 dumpurl=http://$hostname/$dumpurl 28 echo Stopping slave... 29 mysql $ROOTARGS -e "STOP SLAVE" 30 31 # Normally this will be empty anyway, but it will save our admin 32 # if he accidentally relates a master with some other master! 33 backup=/var/backups/alldbs-`date +%Y%m%d%H%M%S`.sql.gz 34 mysqldump $ROOTARGS --all-databases --single-transaction |gzip> $backup 35 36 mysql $ROOTARGS -e "RESET SLAVE" 37 mysql $ROOTARGS -e "CHANGE MASTER TO MASTER_HOST='$hostname', MASTER_USER='$user', MASTER_PASSWORD='$password', MASTER_PORT=$port" 38 echo Importing $dumpurl into MySQL 39 curl --silent --show-error $dumpurl |zcat| mysql $ROOTARGS 40 # Root pw gets overwritten by import 41 echo Re-setting Root Pasword -- can use ours because it hasnt been flushed 42 myrootpw=`cat /var/lib/mysql/mysql.passwd` 43 mysqladmin -uroot -p$myrootpw password $myrootpw 44 # Debian packages expect debian-sys-maint@localhost to be root privileged and 45 # configured in /etc/mysql/debian.cnf. we just broke that.. fix it 46 mysql $ROOTARGS -e "GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '$myrootpw'" 47 touch /etc/mysql/debian.cnf 48 chmod 0600 /etc/mysql/debian.cnf 49 cat > /etc/mysql/debian.cnf <<EOF 50 [client] 51 host=localhost 52 user=debian-sys-maint 53 password=$myrootpw 54 socket=/var/run/mysqld/mysqld.sock 55 [mysql_upgrade] 56 host=localhost 57 user=debian-sys-maint 58 password=$myrootpw 59 socket=/var/run/mysqld/mysqld.sock 60 basedir=/usr 61 EOF 62 63 if [ -z "$serverid" ] ; then 64 serverid=`echo $user | cut -d/ -f2` 65 fi 66 # add 100000 to server_id to avoid conflicts w/ masters 67 serverid=$(($serverid+100000)) 68 if [ -f /etc/mysql/conf.d/slave.cf ] ; then 69 old_hash=`md5sum /etc/mysql/conf.d/slave.cf` 70 else 71 old_hash="xxx" 72 fi 73 cat > /etc/mysql/conf.d/binlog.cnf <<EOF 74 [mysqld] 75 server-id = $serverid 76 log_bin = /var/log/mysql/mysql-bin.log 77 EOF 78 new_hash=`md5sum /etc/mysql/conf.d/binlog.cnf` 79 if [ "$new_hash" != "$old_hash" ] ; then 80 service mysql stop 81 # clear any binlogs 82 backupdir=/var/backups/binlogs-`date +%Y%m%d%H%M%S` 83 mkdir -p $backupdir 84 mv /var/log/mysql/mysql-bin* $backupdir || : 85 service mysql start 86 fi 87 mysql $ROOTARGS -e "START SLAVE" 88 mysql $ROOTARGS -e "SHOW SLAVE STATUS" 89 touch /var/lib/juju/i.am.a.slave