1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
2
|
|
|
# -*- coding: utf-8 -*- |
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.option['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.option['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
|
|
|
|
59
|
|
|
|
60
|
|
|
def generate_ignore_database_table(ignore_tables, table): |
61
|
|
|
""" |
62
|
|
|
:param ignore_tables: Dictionary |
63
|
|
|
:param table: String |
64
|
|
|
:return: |
65
|
|
|
""" |
66
|
|
|
ignore_tables.append('--ignore-table=' + system.config['origin']['db']['name'] + '.' + table) |
67
|
|
|
return ignore_tables |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
def get_database_tables_like(client, name): |
|
|
|
|
71
|
|
|
""" |
72
|
|
|
Get database table names like the given name |
73
|
|
|
:param client: String |
74
|
|
|
:param name: String |
75
|
|
|
:return: Dictionary |
76
|
|
|
""" |
77
|
|
|
_dbname = system.config[client]['db']['name'] |
78
|
|
|
_tables = run_database_command(client, f'SHOW TABLES FROM {_dbname} LIKE \'%{name}%\';').strip() |
79
|
|
|
if _tables != '': |
80
|
|
|
return _tables.split('\n', 1)[1:] |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
def generate_mysql_credentials(client): |
84
|
|
|
""" |
85
|
|
|
Generate the needed database credential information for the mysql command |
86
|
|
|
:param client: String |
87
|
|
|
:return: |
88
|
|
|
""" |
89
|
|
|
_credentials = '-u\'' + system.config[client]['db']['user'] + '\' -p\'' + system.config[client]['db'][ |
|
|
|
|
90
|
|
|
'password'] + '\'' |
91
|
|
|
if 'host' in system.config[client]['db']: |
92
|
|
|
_credentials += ' -h\'' + system.config[client]['db']['host'] + '\'' |
93
|
|
|
if 'port' in system.config[client]['db']: |
94
|
|
|
_credentials += ' -P\'' + str(system.config[client]['db']['port']) + '\'' |
95
|
|
|
return _credentials |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
def check_database_dump(client, filepath): |
99
|
|
|
""" |
100
|
|
|
Checking the last line of the dump file if it contains "-- Dump completed on" |
101
|
|
|
:param client: String |
102
|
|
|
:param filepath: String |
103
|
|
|
:return: |
104
|
|
|
""" |
105
|
|
|
if system.option['check_dump']: |
106
|
|
|
_line = mode.run_command( |
107
|
|
|
helper.get_command(client, 'tail') + ' -n 1 ' + filepath, |
108
|
|
|
client, |
109
|
|
|
True |
110
|
|
|
) |
111
|
|
|
|
112
|
|
|
if "-- Dump completed on" not in _line: |
113
|
|
|
sys.exit(output.message(output.Subject.ERROR, 'Dump file is corrupted', False)) |
114
|
|
|
|