-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_mysqlbackup.py
executable file
·129 lines (94 loc) · 3.48 KB
/
test_mysqlbackup.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
#!/usr/bin/env python
# coding: utf-8
import logging
import sys
import time
import MySQLdb
import mysqlbackup
logger = logging.getLogger(__name__)
if __name__ == "__main__":
mysqlbackup.init_logger()
conf = {
'mysql_base': '/usr/local/mysql-5.7.13',
'host': '127.0.0.1',
'data_base': '/data1',
'backup_base': '/data1/backup',
'port': 3309,
'instance_id': '1',
# optional, for test only
'sql_test_insert': ('insert into `xp2`.`heartbeat`'
' (`key`, `value`, `ts`)'
' values'
' ("test-backup", "{v}", "{v}")'),
'sql_test_get_2': 'select `value` from `xp2`.`heartbeat` order by `_id` desc limit 2',
# optional, for backup to remote storage
's3_host': '127.0.0.1',
's3_bucket': 'mysql-backup',
's3_access_key': '3buylpa47d26skigwhm1',
's3_secret_key': 'BPM5MIkmnb8n9ScE+DW80BK3GBZ1hekRPXFOW5hv',
'date_str': '2017_03_03',
}
mb = mysqlbackup.MysqlBackup(conf)
if sys.argv[1] == 'b':
try:
mb.backup()
except mysqlbackup.MysqlBackupError as e:
print e[0]
print e[1]
for l in e[2].split('\\n'):
print l
for l in e[3].split('\\n'):
print l
elif sys.argv[1] == 'r':
try:
mb.restore()
except mysqlbackup.MysqlBackupError as e:
print e[0]
print e[1]
for l in e[2].split('\\n'):
print l
for l in e[3].split('\\n'):
print l
elif sys.argv[1] == 't':
# test connectivity
mb.mysql_get_last_2_test_value()
mb.backup_data()
# Writes another row after backing up data, and before backing up binlog.
# Emulates write that happens when backing up is running
value_after_data = str(time.time())
mb.mysql_insert_test_value(value_after_data)
logger.info('inserted: {v} after data backup'.format(
v=value_after_data))
mb.backup_binlog()
mb.calc_checksum()
mb.upload_backup()
# write after all backup, test restoring from remote binlog
value_after_binlog = str(time.time())
mb.mysql_insert_test_value(value_after_binlog)
logger.info('inserted: {v} after binlog backup'.format(
v=value_after_binlog))
logger.info('wait for binlog to sync')
time.sleep(1)
logger.info('kill mysql')
mysqlbackup._shell_run(mb.render('kill $(cat {mysql_pid_path})'))
while True:
try:
mb.list_binlog_fns()
time.sleep(0.1)
continue
except MySQLdb.OperationalError as e:
break
logger.info('remove mysql data dir')
mysqlbackup._shell_run(
mb.render('rm -rf {mysql_data_dir} {backup_data_dir} {backup_binlog_dir}'))
mb.restore()
# check data
proc = mb.start_tmp_mysql()
rst = mb.mysql_get_last_2_test_value()
assert rst[0].values()[
0] == value_after_binlog, 'write after binlog backup should be seen: ' + str(value_after_binlog)
assert rst[1].values()[
0] == value_after_data, 'write after data backup should be seen: ' + str(value_after_data)
mb.stop_tmp_mysql(proc)
else:
raise ValueError('invalid command: ' + sys.argv[1])