3
# Master side of master/slave replication
7
. /usr/share/charm-helper/sh/net.sh
9
ROOTARGS="-uroot -p`cat /var/lib/mysql/mysql.passwd`"
10
snapdir=/var/www/snaps
12
apt-get -y install apache2
13
# disable wide-open access (restrict to each db IP)
16
for r in $(relation-ids master) ; do
17
for runit in $(relation-list -r $r) ; do
18
rhost=$(relation-get -r $r hostname $runit)
19
if [ -n "$rhost" ] ; then
20
rhosts=${rhosts},$rhost
21
if [ "$runit" = "$JUJU_REMOTE_UNIT" ] ; then
23
if ! ch_is_ip $remote_ip ; then
24
remote_ip=$(ch_get_ip $remote_ip)
30
# remove any leading comma
33
# if there are no hosts, we simply disable all access
34
target=/etc/apache2/sites-available/mysql-dumps
41
if [ -n "$rhosts" ] ; then
52
service apache2 reload
55
# TODO: settings.. make mmin tunable as it is highly subjective
56
recent_backup=`find $snapdir -name 'replication_seed.*.sql.gz' -mmin -60|head -1`
57
if [ -z "$recent_backup" ] ; then
58
name=replication_seed.`date +%Y%m%d%H%M%S`.sql.gz
59
echo `date`: Creating snapshot $recent_backup
60
# Assumes transactional storage -- MyISAM please GO AWAY
61
mysqldump $ROOTARGS --add-drop-database --all-databases --single-transaction --master-data |gzip>$snapdir/$name
62
# Make sure webserver can serve it
64
name=`basename $recent_backup`
66
chown -v -R www-data.www-data /var/www
68
user=${JUJU_REMOTE_UNIT%%/*}
70
action=${action##master-relation-}
73
mysql $ROOTARGS -e "GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO \`$user\`@\`$remote_ip\` IDENTIFIED BY '$pass'"
74
relation-set dumpurl=/snaps/$name \
77
hostname=`unit-get private-address` \
81
mysql $ROOTARGS -e "REVOKE REPLICATION SLAVE, REPLICATION CLIENT ON *.* FROM `$user`@`$remote_ip`"
84
# XXX Need some way to tie users to relation ID