~gandelman-a/charms/quantal/mysql/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/sh

set -e

ROOTARGS="-uroot -p`cat /var/lib/juju/mysql.passwd`"

# Others can join that service but only the lowest will be the master
# Note that we could be more automatic but for now we will wait for
# service and unit settings to define a "master override" to allow
# migrating slaves selectively
master=`relation-list | head -1`
if [ "$JUJU_REMOTE_UNIT" != "$master" ] ; then
  exit 0;
fi
echo master=$master

for setting in user password hostname port dumpurl; do
    value=`relation-get $setting`
    if [ -z "$value" ] ; then
        echo "$setting is empty! Aborting"
        exit 0
    fi        
    echo $setting=$value
    eval $setting=$value
done

dumpurl=http://$hostname/$dumpurl
echo Stopping slave...
mysql $ROOTARGS -e "STOP SLAVE"

# Normally this will be empty anyway, but it will save our admin
# if he accidentally relates a master with some other master!
backup=/var/backups/alldbs-`date +%Y%m%d%H%M%S`.sql.gz
mysqldump $ROOTARGS --all-databases --single-transaction |gzip> $backup

mysql $ROOTARGS -e "RESET SLAVE"
mysql $ROOTARGS -e "CHANGE MASTER TO MASTER_HOST='$hostname', MASTER_USER='$user', MASTER_PASSWORD='$password', MASTER_PORT=$port"
echo Importing $dumpurl into MySQL
curl --silent --show-error $dumpurl |zcat| mysql $ROOTARGS
# Root pw gets overwritten by import
echo Re-setting Root Pasword -- can use ours because it hasnt been flushed
myrootpw=`cat /var/lib/juju/mysql.passwd`
mysqladmin -uroot -p$myrootpw password $myrootpw
# Debian packages expect debian-sys-maint@localhost to be root privileged and
# configured in /etc/mysql/debian.cnf. we just broke that.. fix it
mysql $ROOTARGS -e "GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '$myrootpw'"
touch /etc/mysql/debian.cnf
chmod 0600 /etc/mysql/debian.cnf
cat > /etc/mysql/debian.cnf <<EOF
[client]
host=localhost
user=debian-sys-maint
password=$myrootpw
socket=/var/run/mysqld/mysqld.sock
[mysql_upgrade]
host=localhost
user=debian-sys-maint
password=$myrootpw
socket=/var/run/mysqld/mysqld.sock
basedir=/usr
EOF

if [ -z "$serverid" ] ; then
  serverid=`echo $user | cut -d/ -f2`
fi
# add 100000 to server_id to avoid conflicts w/ masters
serverid=$(($serverid+100000))
if [ -f /etc/mysql/conf.d/slave.cf ] ; then
    old_hash=`md5sum /etc/mysql/conf.d/slave.cf`
else
    old_hash="xxx"
fi
cat > /etc/mysql/conf.d/binlog.cnf <<EOF
[mysqld]
server-id = $serverid
log_bin = /var/log/mysql/mysql-bin.log
EOF
new_hash=`md5sum /etc/mysql/conf.d/binlog.cnf`
if [ "$new_hash" != "$old_hash" ] ; then
  service mysql stop
  # clear any binlogs
  backupdir=/var/backups/binlogs-`date +%Y%m%d%H%M%S`
  mkdir -p $backupdir
  mv /var/log/mysql/mysql-bin* $backupdir || :
  service mysql start
fi
mysql $ROOTARGS -e "START SLAVE"
mysql $ROOTARGS -e "SHOW SLAVE STATUS"
touch /var/lib/juju/i.am.a.slave