|
1 | 1 | from fabric.api import *
|
| 2 | +from fabric.operations import prompt |
2 | 3 |
|
3 | 4 |
|
4 | 5 | def run_mysql_command(cmd):
|
@@ -40,14 +41,37 @@ def fix_replication_from_slow_query_log_after_upgrade():
|
40 | 41 | @task
|
41 | 42 | def setup_slave_from_master(master):
|
42 | 43 | """
|
43 |
| - Sets up a slave from a master by taking a dump from the master, |
44 |
| - copying it to the slave and then restoring the dump. |
| 44 | + Sets up a slave from a master by: |
| 45 | + - configuring MySQL replication config |
| 46 | + - using the replicate_slave_from_master task to do an initial dump to the slave |
45 | 47 |
|
46 | 48 | Usage: fab environment -H mysql-slave-1.backend mysql.setup_slave_from_master:'mysql-master-1.backend'
|
47 | 49 | """
|
48 | 50 | if len(env.hosts) > 1:
|
49 | 51 | exit('This job is currently only setup to run against one slave at a time')
|
50 | 52 |
|
| 53 | + mysql_master = prompt("Master host (eg 'master.mysql' or 'whitehall-master.mysql'):") |
| 54 | + replication_username = 'replica_user' |
| 55 | + replication_password = prompt("Password for MySQL user {0}:".format(replication_username)) |
| 56 | + |
| 57 | + run_mysql_command("STOP SLAVE;") |
| 58 | + run_mysql_command("CHANGE MASTER TO MASTER_HOST='{0}', MASTER_USER='{1}', MASTER_PASSWORD='{2}';".format( |
| 59 | + mysql_master, replication_username, replication_password)) |
| 60 | + |
| 61 | + replicate_slave_from_master(master) |
| 62 | + |
| 63 | + |
| 64 | +@task |
| 65 | +def replicate_slave_from_master(master): |
| 66 | + """ |
| 67 | + Updates a slave from a master by taking a dump from the master, |
| 68 | + copying it to the slave and then restoring the dump. |
| 69 | +
|
| 70 | + Usage: fab environment -H mysql-slave-1.backend mysql.replicate_slave_from_master:'mysql-master-1.backend' |
| 71 | + """ |
| 72 | + if len(env.hosts) > 1: |
| 73 | + exit('This job is currently only setup to run against one slave at a time') |
| 74 | + |
51 | 75 | with settings(host_string=master):
|
52 | 76 | # The use of `--master-data` here implies `--lock-all-tables` per the
|
53 | 77 | # MySQL reference manual: http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_master-data
|
|
0 commit comments