-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_rds_mysql_sync.py
238 lines (188 loc) · 10.8 KB
/
run_rds_mysql_sync.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#! /usr/bin/python
import os
import sys
import ConfigParser
import pisces.utils.io as io
import re
def run():
clean()
download_rds_backup_extract()
install_xtrabackup()
download_rds_backup_file()
restore_by_xtrabackup()
start_all_local_sync_tomcat()
run_merge_mysql('common','business')
def clean():
os.system('docker stop rds-common')
os.system('docker rm rds-common')
os.system('docker stop rds-business')
os.system('docker rm rds-business')
os.system('docker stop mysql-all')
os.system('docker rm mysql-all')
os.system('rm -rf /opt/mysql/rds-common/*')
os.system('rm -rf /opt/mysql/rds-business/*')
os.system('rm -rf /opt/mysql/mysql-all/*')
os.system('rm -rf /opt/mysql/data/*')
def download_rds_backup_extract():
os.system("wget 'http://oss.aliyuncs.com/aliyunecs/rds_backup_extract.sh?spm=5176.7741817.2.15.DFVwwT&file=rds_backup_extract.sh' -O /root/rds_backup_extract.sh")
def install_xtrabackup():
os.system("wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.2.12/binary/redhat/7/x86_64/percona-xtrabackup-2.2.12-1.el7.x86_64.rpm -O /root/percona-xtrabackup-2.2.12-1.el7.x86_64.rpm")
os.system("yum localinstall percona-xtrabackup-2.2.12-1.el7.x86_64.rpm")
def download_rds_backup_file():
cp = ConfigParser.SafeConfigParser()
cp.read('/opt/app.conf')
db1_name = cp.get('rds-mysql-sync', 'db1.name')
db1_backup_url = cp.get('rds-mysql-sync', 'db1.backup.url')
db2_name = cp.get('rds-mysql-sync', 'db2.name')
db2_backup_url = cp.get('rds-mysql-sync', 'db2.backup.url')
os.system("wget '{0}' -O /root/{1}.tar.gz".format(db1_backup_url,db1_name))
os.system("wget '{0}' -O /root/{1}.tar.gz".format(db2_backup_url, db2_name))
os.system("bash /root/rds_backup_extract.sh -f /root/{0}.tar.gz -C /opt/mysql/rds-{0}".format(db1_name))
os.system("bash /root/rds_backup_extract.sh -f /root/{0}.tar.gz -C /opt/mysql/rds-{0}".format(db2_name))
def restore_by_xtrabackup():
cp = ConfigParser.SafeConfigParser()
cp.read('/opt/app.conf')
db1_name = cp.get('rds-mysql-sync', 'db1.name')
db2_name = cp.get('rds-mysql-sync', 'db2.name')
os.system("innobackupex --defaults-file=/opt/mysql/rds-{0}/backup-my.cnf --apply-log /opt/mysql/rds-{0}".format(db1_name))
os.system("innobackupex --defaults-file=/opt/mysql/rds-{0}/backup-my.cnf --apply-log /opt/mysql/rds-{0}".format(db2_name))
os.system("chown -R mysql:mysql /opt/mysql/rds-{0}".format(db1_name))
os.system("chown -R mysql:mysql /opt/mysql/rds-{0}".format(db2_name))
def start_all_local_sync_tomcat():
cp = ConfigParser.SafeConfigParser()
cp.read('/opt/app.conf')
db1_name = cp.get('rds-mysql-sync', 'db1.name')
db1_serverid = cp.get('rds-mysql-sync', 'db1.server-id')
db1_master_host = cp.get('rds-mysql-sync', 'db1.master.host')
db1_master_user = cp.get('rds-mysql-sync', 'db1.master.user')
db1_master_password = cp.get('rds-mysql-sync', 'db1.master.password')
db2_name = cp.get('rds-mysql-sync', 'db2.name')
db2_serverid = cp.get('rds-mysql-sync', 'db2.server-id')
db2_master_host = cp.get('rds-mysql-sync', 'db2.master.host')
db2_master_user = cp.get('rds-mysql-sync', 'db2.master.user')
db2_master_password = cp.get('rds-mysql-sync', 'db2.master.password')
start_local_sync_tomcat(db1_name, db1_serverid, db1_master_host, db1_master_user, db1_master_password)
start_local_sync_tomcat(db2_name, db2_serverid, db2_master_host, db2_master_user, db2_master_password)
def start_local_sync_tomcat(dbname,serverid,master_host,master_user,master_passwd):
io.replace_str_in_file('/opt/docker/mysql/my.cnf.rds-sync.sample',
{'${server-id}': serverid},
'/opt/docker/mysql/my.cnf')
os.system('docker build -t rds-{0} /opt/docker/mysql'.format(dbname))
os.system('docker run --name rds-{0} -it -d -v /opt/mysql/rds-{0}:/var/lib/mysql -v /opt/mysql/data:/opt/mysql/data rds-{0} /bin/bash'.format(dbname))
os.system('docker exec rds-{0} chown -R mysql:mysql /var/lib/mysql'.format(dbname))
os.system('docker exec -d rds-{0} service mysql start'.format(dbname))
print 'sleep 30 seconds to wait for mysql starting......'
os.system('sleep 30')
os.system('docker exec rds-{0} mysql -uroot -e "truncate table mysql.slave_relay_log_info;"'.format(dbname))
os.system('docker exec rds-{0} mysql -uroot -e "truncate table mysql.slave_master_info;"'.format(dbname))
os.system('docker exec rds-{0} mysql -uroot -e "truncate table mysql.slave_worker_info;"'.format(dbname))
os.system('docker exec rds-{0} mysql -uroot -e "reset slave"'.format(dbname))
os.system('docker exec rds-{0} mysql_upgrade -uroot --force'.format(dbname))
os.system('docker exec -d rds-{0} service mysql restart'.format(dbname))
print 'sleep 30 seconds to wait for mysql starting......'
os.system('sleep 30')
# set @@GLOBAL.GTID_PURGED
s = io.read_file_2_str('/opt/mysql/rds-{0}/xtrabackup_slave_info'.format(dbname))
global_gtid_purged = re.findall(r"'(.+?)'", s)[0]
os.system('docker exec rds-{0} mysql -uroot -e "reset master"'.format(dbname))
os.system('docker exec rds-{0} mysql -uroot -e "SET @@GLOBAL.GTID_PURGED=\'{1}\';"'.format(dbname,global_gtid_purged))
os.system('docker exec rds-{0} mysql -uroot -e "CHANGE MASTER TO MASTER_HOST=\'{1}\',MASTER_USER=\'{2}\', MASTER_PASSWORD=\'{3}\',master_auto_position=1; "'.format(dbname,master_host,master_user,master_passwd))
os.system('docker exec rds-{0} mysql -uroot -e "start slave"'.format(dbname))
# create mysql user for replicator
os.system('docker exec rds-{0} mysql -uroot -e "create user \'replicator\'@\'%\' identified by \'replicator\'"'.format(dbname))
os.system('docker exec rds-{0} mysql -uroot -e "grant replication slave on *.* to \'replicator\'@\'%\' identified by \'replicator\';"'.format(dbname))
os.system('docker exec rds-{0} mysql -uroot -e "flush privileges"'.format(dbname))
# export data of local mysql
os.system('docker exec rds-{0} mysqldump -uroot --master-data=2 --single-transaction --add-drop-database --all-databases > /opt/mysql/data/mysql-{0}-dump.sql'.format(dbname))
def run_merge_mysql(db1_name,db2_name):
cp = ConfigParser.SafeConfigParser()
cp.read('/opt/app.conf')
serverid = cp.get('rds-mysql-sync', 'db-merge.server-id')
io.replace_str_in_file('/opt/docker/mysql/my.cnf.rds-sync.merge.sample',
{'${server-id}': serverid},
'/opt/docker/mysql/my.cnf')
os.system('docker build -t mysql-all /opt/docker/mysql')
os.system('docker run --name mysql-all -it -d -v /opt/mysql/mysql-all:/var/lib/mysql --link=rds-{0}:rds-{0} --link=rds-{1}:rds-{1} -p 3307:3306 mysql-all /bin/bash'.format(db1_name,db2_name))
print 'start to process dump sql file......'
purged_id_list = []
list1 = modify_dump_sql('/opt/mysql/data/mysql-{0}-dump.sql'.format(db1_name))
list1 = extract_gtid_purged(list1)
list2 = modify_dump_sql('/opt/mysql/data/mysql-{0}-dump.sql'.format(db2_name))
list2 = extract_gtid_purged(list2)
purged_id_list.extend(list1)
purged_id_list.extend(list2)
os.system('docker cp /opt/mysql/data/mysql-{0}-dump.sql mysql-all:/root'.format(db1_name))
os.system('docker cp /opt/mysql/data/mysql-{0}-dump.sql mysql-all:/root'.format(db2_name))
print 'start to chown -R mysql:mysql /var/lib/mysql for mysql-all ......'
os.system('docker exec mysql-all chown -R mysql:mysql /var/lib/mysql')
os.system('docker exec -d mysql-all service mysql start')
print 'sleep 30 seconds to wait for mysql starting......'
os.system('sleep 30')
print 'start to execute mysql_upgrade for mysql-all......'
os.system('docker exec -it mysql-all mysql_upgrade -uroot --force')
print 'start to restart mysql-all......'
os.system('docker exec -d mysql-all service mysql restart')
print 'sleep 30 seconds to wait for mysql starting......'
os.system('sleep 10')
print 'rds-common-dump.sql:', purged_id_list
print 'please attach into the container and continue ......'
# print 'start to import {0} dump file to mysql-all......'.format(db1_name)
# os.system('docker exec mysql-all mysql -uroot < /root/mysql-{0}-dump.sql'.format(db1_name))
# print 'start to import {0} dump file to mysql-all......'.format(db2_name)
# os.system('docker exec mysql-all mysql -uroot < /root/mysql-{0}-dump.sql'.format(db2_name))
#
# os.system('docker exec mysql-all mysql -uroot -e "reset master"')
#
# reset_purge_gitd_cmd = "SET @@GLOBAL.GTID_PURGED='{0}';".format(','.join(purged_id_list))
# print '@@GLOBAL.GTID_PURGED=', purged_id_list
# os.system('docker exec mysql-all mysql -uroot -e "{0}"'.format(reset_purge_gitd_cmd))
#
# os.system('docker exec mysql-all mysql -uroot -e "CHANGE MASTER TO MASTER_HOST=\'rds-{0}\',MASTER_USER=\'replicator\', MASTER_PASSWORD=\'replicator\',master_auto_position=1 FOR CHANNEL \'rds-{0}\';"'.format(db1_name))
# os.system('docker exec mysql-all mysql -uroot -e "CHANGE MASTER TO MASTER_HOST=\'rds-{0}\',MASTER_USER=\'replicator\', MASTER_PASSWORD=\'replicator\',master_auto_position=1 FOR CHANNEL \'rds-{0}\';"'.format(db2_name))
#
# os.system('docker exec mysql-all mysql -uroot -e "start slave for channel \'rds-{0}\';"'.format(db2_name))
# os.system('docker exec mysql-all mysql -uroot -e "start slave for channel \'rds-{0}\';"'.format(db2_name))
def modify_dump_sql(dump_file):
lines = []
is_gtid_purged_lines = 0
tmp_file = dump_file + '.tmp'
f2 = open(tmp_file, 'w')
f = open(dump_file)
for line in f:
if line.__contains__('SET @@GLOBAL.GTID_PURGED'):
lines.append(line)
is_gtid_purged_lines = 1
line = '-- ' + line
else:
if is_gtid_purged_lines == 1:
if line.isspace():
is_gtid_purged_lines = 0
else:
lines.append(line)
line = '-- ' + line
f2.write(line)
f.close()
f2.flush()
f2.close()
os.system('rm -f {0}'.format(dump_file))
os.system('mv {0} {1}'.format(tmp_file,dump_file))
return lines
def extract_gtid_purged(list):
# list = ["SET @@GLOBAL.GTID_PURGED='56022958-07ce-11e7-a936-0242ac110002:1-3,\n",
# '6c974343-94d6-11e6-9d67-1051721c39f4:1-39669761,\n', "85d9759c-94d6-11e6-9d67-d89d672a9654:1-4';\n"]
gtid_list = []
for s in list:
if s.__contains__("'") and s.__contains__(','):
index1 = s.index("'")
index2 = s.index(",")
s = s[index1 + 1:index2]
elif s.__contains__(';'):
index = s.index(';')
s = s[0:index - 1]
else:
index = s.index(',')
s = s[0:index]
gtid_list.append(s)
return gtid_list
if __name__ == '__main__':
run()