~charmers/charms/oneiric/mediawiki/trunk

« back to all changes in this revision

Viewing changes to formulas/mysql/hooks/slave-relation-changed

  • Committer: Clint Byrum
  • Date: 2011-05-11 12:54:21 UTC
  • mfrom: (35.2.5 add-mysql-replication)
  • Revision ID: clint@ubuntu.com-20110511125421-f8jqbf1ihxd15j4a
Merging mysql one-way-replication relations

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
 
 
3
set -e
 
4
 
 
5
ROOTARGS="-uroot -p`cat /var/lib/ensemble/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 [ "$ENSEMBLE_REMOTE_UNIT" != "$master" ] ; then
 
13
  exit 0;
 
14
fi
 
15
echo master=$master
 
16
 
 
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"
 
21
        exit 0
 
22
    fi        
 
23
    echo $setting=$value
 
24
    eval $setting=$value
 
25
done
 
26
 
 
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
 
31
 
 
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
 
41
 
 
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"
 
47
fi
 
48
if [ -z "$serverid" ] ; then
 
49
  serverid=`echo $user | cut -d/ -f2`
 
50
fi
 
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
 
55
  service mysql stop
 
56
  # clear binlogs
 
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`
 
59
  mkdir -p $backupdir
 
60
  mv $binlog* $backupdir || :
 
61
  service mysql start
 
62
fi
 
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"