5
ROOTARGS="-uroot -p`cat /var/lib/ensemble/mysql.passwd`"
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 [ "$ENSEMBLE_REMOTE_UNIT" != "$master" ] ; then
17
for setting in user password ip port dumpurl; do
18
value=`relation-get $setting`
19
if [ -z "$value" ] ; then
20
echo "$setting is empty! Aborting"
27
# Normally this will be empty anyway, but it will save our admin
28
# if he accidentally relates a master with some other master!
29
backup=/var/backups/alldbs-`date +%Y%m%d%H%M%S`.sql.gz
30
mysqldump $ROOTARGS --all-databases --single-transaction |gzip> $backup
32
dumpurl=http://$ip/$dumpurl
33
echo Stopping slave...
34
mysql $ROOTARGS -e "STOP SLAVE"
35
echo Importing $dumpurl into MySQL
36
curl --silent --show-error $dumpurl |zcat| mysql $ROOTARGS
37
# Root pw gets overwritten by import
38
echo Re-setting Root Pasword -- can use ours because it hasnt been flushed
39
myrootpw=`cat /var/lib/ensemble/mysql.passwd`
40
mysqladmin -uroot -p$myrootpw password $myrootpw
42
serverid=`augtool get /files/etc/mysql/my.cnf/target[3]/server_id | awk -F" = " '{print $2}'`
43
server_id_name="server_id"
44
if [ -z "$serverid" ] ; then
45
serverid=`augtool get /files/etc/mysql/my.cnf/target[3]/server-id | awk -F" = " '{print $2}'`
46
server_id_name="server-id"
48
if [ -z "$serverid" ] ; then
49
serverid=`echo $user | cut -d/ -f2`
51
if [ -z "$serverid" -o "$serverid" -lt 100000 ] ; then
52
# add 100000 to server_id to avoid conflicts w/ masters
53
serverid=$(($serverid+100000))
54
echo "set /files/etc/mysql/my.cnf/target[3]/$server_id_name $serverid\nsave" | augtool -b
57
binlog=`augtool get /files/etc/mysql/my.cnf/target[3]/log_bin|cut -d' ' -f3`
58
backupdir=/var/backups/binlogs-`date +%Y%m%d%H%M%S`
60
mv $binlog* $backupdir || :
63
mysql $ROOTARGS -e "STOP SLAVE"
64
mysql $ROOTARGS -e "CHANGE MASTER TO MASTER_HOST='$ip', MASTER_USER='$user', MASTER_PASSWORD='$password', MASTER_PORT=$port"
65
mysql $ROOTARGS -e "START SLAVE"
66
mysql $ROOTARGS -e "SHOW SLAVE STATUS"