Skip to content

Commit 6df1243

Browse files
committed
Add a task to setup MySQL replication
This replaces a list of commands in the opsmanual with code: https://github.gds/pages/gds/opsmanual/infrastructure/howto/setup-mysql-replication.html I've used `run('sudo -i ...')` because `sudo()` doesn't appear to support simulating a login shell.
1 parent 86d7adf commit 6df1243

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

mysql.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,36 @@ def fix_replication_from_slow_query_log_after_upgrade():
3636
run_mysql_command("START SLAVE;")
3737
run_mysql_command("SET GLOBAL slow_query_log = 'ON';")
3838
run_mysql_command("show slave status\G;")
39+
40+
41+
@task
42+
def setup_slave_from_master(master):
43+
"""
44+
Sets up a slave from a master by taking a dump from the master,
45+
copying it to the slave and then restoring the dump.
46+
47+
Usage: fab environment -H mysql-slave-1.backend mysql.setup_slave_from_master:'mysql-master-1.backend'
48+
"""
49+
if len(env.hosts) > 1:
50+
print 'This job is currently only setup to run against one slave at a time'
51+
52+
with settings(host_string=master):
53+
# The use of `--master-data` here implies `--lock-all-tables` per the
54+
# MySQL reference manual: http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_master-data
55+
run('sudo -i mysqldump -u root --all-databases --master-data --add-drop-database > dump.sql')
56+
57+
with settings(host_string=master, forward_agent=True):
58+
run('scp dump.sql {0}:~'.format(env.hosts[0]))
59+
60+
with settings(host_string=master):
61+
run('rm dump.sql')
62+
63+
run_mysql_command("STOP SLAVE")
64+
run_mysql_command("SET GLOBAL slow_query_log=OFF")
65+
66+
run('sudo -i mysql -uroot < dump.sql')
67+
68+
run_mysql_command("START SLAVE")
69+
run_mysql_command("SET GLOBAL slow_query_log=ON")
70+
71+
run_mysql_command("SHOW SLAVE STATUS\G")

0 commit comments

Comments
 (0)