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
|
#!/bin/sh
#
# Master side of master/slave replication
set -e
ROOTARGS="-uroot -p`cat /var/lib/juju/mysql.passwd`"
snapdir=/var/www/snaps
mkdir -p $snapdir
apt-get -y install apache2
# disable wide-open access (restrict to each db IP)
allowline=`augtool get /files/etc/apache2/sites-available/default/VirtualHost/Directory[2]/directive[4]/arg[2]`
if [ "$allowline" = "/files/etc/apache2/sites-available/default/VirtualHost/Directory[2]/directive[4]/arg[2] = all" ] ; then
# disable allow from all
augtool -b <<EOF
defvar webroot /files/etc/apache2/sites-available/default/VirtualHost/Directory[2]
rm \$webroot/directive[4]
save
EOF
service apache2 reload
fi
# TODO: settings.. make mmin tunable as it is highly subjective
recent_backup=`find $snapdir -name 'replication_seed.*.sql.gz' -mmin -60|head -1`
if [ -z "$recent_backup" ] ; then
name=replication_seed.`date +%Y%m%d%H%M%S`.sql.gz
echo `date`: Creating snapshot $recent_backup
# Assumes transactional storage -- MyISAM please GO AWAY
mysqldump $ROOTARGS --all-databases --single-transaction --master-data |gzip>$snapdir/$name
# Make sure webserver can serve it
else
name=`basename $recent_backup`
fi
chown -v -R www-data.www-data /var/www
remote_host=`relation-get hostname`
test -n "$remote_host" || exit 0
remote_ip=`dig +short $remote_host`
pass=`pwgen -s 16`
augtool -b <<EOF
defvar webroot /files/etc/apache2/sites-available/default/VirtualHost/Directory[2]
set \$webroot/directive[last()+1] allow
set \$webroot/directive[last()]/arg[1] from
set \$webroot/directive[last()]/arg[2] $remote_ip
save
EOF
service apache2 reload
user=`echo $JUJU_REMOTE_UNIT | sed -e 's,/,-,'`
mysql $ROOTARGS -e "GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO \`$user\`@\`$remote_ip\` IDENTIFIED BY '$pass'"
relation-set dumpurl=/snaps/$name \
user=$user \
password=$pass \
hostname=`unit-get private-address` \
port=3306
|