1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
2
|
|
|
# -*- coding: future_fstrings -*- |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
import sys |
6
|
|
|
import datetime |
7
|
|
|
from db_sync_tool.utility import mode, system, helper, output |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
database_dump_file_name = None |
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
def run_database_command(client, command): |
14
|
|
|
""" |
15
|
|
|
Run a database command using the "mysql -e" command |
16
|
|
|
:param client: String |
17
|
|
|
:param command: String database command |
18
|
|
|
:return: |
19
|
|
|
""" |
20
|
|
|
return mode.run_command( |
21
|
|
|
helper.get_command(client, 'mysql') + ' ' + generate_mysql_credentials(client) + ' -e "' + command + '"', |
|
|
|
|
22
|
|
|
client, True) |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def generate_database_dump_filename(): |
26
|
|
|
""" |
27
|
|
|
Generate a database dump filename like "_[name]_[date].sql" or using the give filename |
28
|
|
|
:return: |
29
|
|
|
""" |
30
|
|
|
global database_dump_file_name |
|
|
|
|
31
|
|
|
|
32
|
|
|
if system.config['dump_name'] == '': |
33
|
|
|
# _project-db_20-08-2020_12-37.sql |
34
|
|
|
_now = datetime.datetime.now() |
35
|
|
|
database_dump_file_name = '_' + system.config['origin']['db']['name'] + '_' + _now.strftime( |
36
|
|
|
"%d-%m-%Y_%H-%M") + '.sql' |
37
|
|
|
else: |
38
|
|
|
database_dump_file_name = system.config['dump_name'] + '.sql' |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
def generate_ignore_database_tables(): |
42
|
|
|
""" |
43
|
|
|
Generate the ignore tables options for the mysqldump command by the given table list |
44
|
|
|
# ToDo: Too much conditional nesting |
45
|
|
|
:return: String |
46
|
|
|
""" |
47
|
|
|
_ignore_tables = [] |
48
|
|
|
if 'ignore_table' in system.config: |
49
|
|
|
for table in system.config['ignore_table']: |
50
|
|
|
if '*' in table: |
51
|
|
|
_wildcard_tables = get_database_tables_like('origin', table.replace('*', '')) |
52
|
|
|
if _wildcard_tables: |
53
|
|
|
for wildcard_table in _wildcard_tables: |
54
|
|
|
_ignore_tables = generate_ignore_database_table(_ignore_tables, wildcard_table) |
|
|
|
|
55
|
|
|
else: |
56
|
|
|
_ignore_tables = generate_ignore_database_table(_ignore_tables, table) |
57
|
|
|
return ' '.join(_ignore_tables) |
58
|
|
|
return '' |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
def generate_ignore_database_table(ignore_tables, table): |
62
|
|
|
""" |
63
|
|
|
:param ignore_tables: Dictionary |
64
|
|
|
:param table: String |
65
|
|
|
:return: |
66
|
|
|
""" |
67
|
|
|
ignore_tables.append('--ignore-table=' + system.config['origin']['db']['name'] + '.' + table) |
68
|
|
|
return ignore_tables |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def get_database_tables_like(client, name): |
|
|
|
|
72
|
|
|
""" |
73
|
|
|
Get database table names like the given name |
74
|
|
|
:param client: String |
75
|
|
|
:param name: String |
76
|
|
|
:return: Dictionary |
77
|
|
|
""" |
78
|
|
|
_dbname = system.config[client]['db']['name'] |
79
|
|
|
_tables = run_database_command(client, f'SHOW TABLES FROM {_dbname} LIKE \'%{name}%\';').strip() |
80
|
|
|
if _tables != '': |
81
|
|
|
return _tables.split('\n', 1)[1:] |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
def generate_mysql_credentials(client): |
85
|
|
|
""" |
86
|
|
|
Generate the needed database credential information for the mysql command |
87
|
|
|
:param client: String |
88
|
|
|
:return: |
89
|
|
|
""" |
90
|
|
|
_credentials = '-u\'' + system.config[client]['db']['user'] + '\' -p\'' + system.config[client]['db'][ |
|
|
|
|
91
|
|
|
'password'] + '\'' |
92
|
|
|
if 'host' in system.config[client]['db']: |
93
|
|
|
_credentials += ' -h\'' + system.config[client]['db']['host'] + '\'' |
94
|
|
|
if 'port' in system.config[client]['db']: |
95
|
|
|
_credentials += ' -P\'' + str(system.config[client]['db']['port']) + '\'' |
96
|
|
|
return _credentials |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
def check_database_dump(client, filepath): |
100
|
|
|
""" |
101
|
|
|
Checking the last line of the dump file if it contains "-- Dump completed on" |
102
|
|
|
:param client: String |
103
|
|
|
:param filepath: String |
104
|
|
|
:return: |
105
|
|
|
""" |
106
|
|
|
if system.config['check_dump']: |
107
|
|
|
_line = mode.run_command( |
108
|
|
|
helper.get_command(client, 'tail') + ' -n 1 ' + filepath, |
109
|
|
|
client, |
110
|
|
|
True, |
111
|
|
|
skip_dry_run=True |
112
|
|
|
) |
113
|
|
|
|
114
|
|
|
if not _line: |
115
|
|
|
return |
116
|
|
|
|
117
|
|
|
if "-- Dump completed on" not in _line: |
118
|
|
|
sys.exit( |
119
|
|
|
output.message( |
120
|
|
|
output.Subject.ERROR, |
121
|
|
|
'Dump file is corrupted', |
122
|
|
|
do_print=False |
123
|
|
|
) |
124
|
|
|
) |
125
|
|
|
else: |
126
|
|
|
output.message( |
127
|
|
|
output.host_to_subject(client), |
128
|
|
|
'Dump file is valid', |
129
|
|
|
verbose_only=True |
130
|
|
|
) |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
def count_tables(client, filepath): |
134
|
|
|
""" |
135
|
|
|
Count the reference string in the database dump file to get the count of all exported tables |
136
|
|
|
:param client: String |
137
|
|
|
:param filepath: String |
138
|
|
|
:return: |
139
|
|
|
""" |
140
|
|
|
_reference = 'CREATE TABLE' |
141
|
|
|
_count = mode.run_command( |
142
|
|
|
f'{helper.get_command(client, "grep")} -ao "{_reference}" {filepath} | wc -l | xargs', |
143
|
|
|
client, |
144
|
|
|
True, |
145
|
|
|
skip_dry_run=True |
146
|
|
|
) |
147
|
|
|
|
148
|
|
|
if _count: |
149
|
|
|
output.message( |
150
|
|
|
output.host_to_subject(client), |
151
|
|
|
f'{int(_count)} table(s) exported' |
152
|
|
|
) |
153
|
|
|
|