1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: future_fstrings -*- |
3
|
|
|
|
4
|
|
|
""" |
5
|
|
|
Process script |
6
|
|
|
""" |
7
|
|
|
import semantic_version |
8
|
|
|
|
9
|
|
|
from db_sync_tool.utility import parser, mode, system, helper, output |
10
|
|
|
from db_sync_tool.database import utility as database_utility |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
def create_origin_database_dump(): |
14
|
|
|
""" |
15
|
|
|
Creating the origin database dump file |
16
|
|
|
:return: |
17
|
|
|
""" |
18
|
|
|
if not mode.is_import(): |
19
|
|
|
parser.get_database_configuration(mode.Client.ORIGIN) |
20
|
|
|
database_utility.generate_database_dump_filename() |
21
|
|
|
helper.check_and_create_dump_dir(mode.Client.ORIGIN, |
22
|
|
|
helper.get_dump_dir(mode.Client.ORIGIN)) |
23
|
|
|
|
24
|
|
|
_dump_file_path = helper.get_dump_dir( |
25
|
|
|
mode.Client.ORIGIN) + database_utility.database_dump_file_name |
26
|
|
|
|
27
|
|
|
_database_version = database_utility.get_database_version(mode.Client.ORIGIN) |
28
|
|
|
output.message( |
29
|
|
|
output.Subject.ORIGIN, |
30
|
|
|
f'Creating database dump {output.CliFormat.BLACK}{_dump_file_path}{output.CliFormat.ENDC}', |
|
|
|
|
31
|
|
|
True |
32
|
|
|
) |
33
|
|
|
|
34
|
|
|
_mysqldump_options = '--no-tablespaces ' |
35
|
|
|
# Remove --no-tablespaces option for mysql < 5.6 |
36
|
|
|
# @ToDo: Better option handling |
37
|
|
|
if not _database_version is None: |
38
|
|
|
if _database_version[0] == database_utility.DatabaseSystem.MYSQL and \ |
39
|
|
|
semantic_version.Version(_database_version[1]) < semantic_version.Version('5.6.0'): |
|
|
|
|
40
|
|
|
_mysqldump_options = '' |
41
|
|
|
|
42
|
|
|
# Adding additional where clause to sync only selected rows |
43
|
|
|
if system.config['where'] != '': |
44
|
|
|
_where = system.config['where'] |
45
|
|
|
_mysqldump_options = _mysqldump_options + f'--where=\'{_where}\' ' |
46
|
|
|
|
47
|
|
|
# Adding additional mysqldump options |
48
|
|
|
# see https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html#mysqldump-option-summary |
49
|
|
|
if system.config['additional_mysqldump_options'] != '': |
50
|
|
|
_additional = system.config['additional_mysqldump_options'] |
51
|
|
|
_mysqldump_options = _mysqldump_options + f'{_additional} ' |
52
|
|
|
|
53
|
|
|
# Run mysql dump command, e.g. |
54
|
|
|
# mysqldump --no-tablespaces -u'db' -p'db' -h'db1' -P'3306' 'db' > /tmp/_db_08-10-2021_07-00.sql |
|
|
|
|
55
|
|
|
mode.run_command( |
56
|
|
|
helper.get_command(mode.Client.ORIGIN, 'mysqldump') + ' ' + _mysqldump_options + |
57
|
|
|
database_utility.generate_mysql_credentials(mode.Client.ORIGIN) + ' \'' + |
58
|
|
|
system.config[mode.Client.ORIGIN]['db']['name'] + '\' ' + |
59
|
|
|
database_utility.generate_ignore_database_tables() + |
60
|
|
|
database_utility.get_database_tables() + |
61
|
|
|
' > ' + _dump_file_path, |
62
|
|
|
mode.Client.ORIGIN, |
63
|
|
|
skip_dry_run=True |
64
|
|
|
) |
65
|
|
|
|
66
|
|
|
database_utility.check_database_dump(mode.Client.ORIGIN, _dump_file_path) |
67
|
|
|
database_utility.count_tables(mode.Client.ORIGIN, _dump_file_path) |
68
|
|
|
prepare_origin_database_dump() |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def import_database_dump(): |
72
|
|
|
""" |
73
|
|
|
Importing the selected database dump file |
74
|
|
|
:return: |
75
|
|
|
""" |
76
|
|
|
if not system.config['is_same_client'] and not mode.is_import(): |
77
|
|
|
prepare_target_database_dump() |
78
|
|
|
|
79
|
|
|
if system.config['clear_database']: |
80
|
|
|
output.message( |
81
|
|
|
output.Subject.TARGET, |
82
|
|
|
'Clearing database before import', |
83
|
|
|
True |
84
|
|
|
) |
85
|
|
|
clear_database(mode.Client.TARGET) |
86
|
|
|
|
87
|
|
|
database_utility.truncate_tables() |
88
|
|
|
|
89
|
|
|
if not system.config['keep_dump'] and not mode.is_dump(): |
90
|
|
|
|
91
|
|
|
database_utility.get_database_version(mode.Client.TARGET) |
92
|
|
|
|
93
|
|
|
output.message( |
94
|
|
|
output.Subject.TARGET, |
95
|
|
|
'Importing database dump', |
96
|
|
|
True |
97
|
|
|
) |
98
|
|
|
|
99
|
|
|
if not mode.is_import(): |
100
|
|
|
_dump_path = helper.get_dump_dir( |
101
|
|
|
mode.Client.TARGET) + database_utility.database_dump_file_name |
102
|
|
|
else: |
103
|
|
|
_dump_path = system.config['import'] |
104
|
|
|
|
105
|
|
|
if not system.config['yes']: |
106
|
|
|
_host_name = helper.get_ssh_host_name(mode.Client.TARGET, True) if mode.is_remote( |
107
|
|
|
mode.Client.TARGET) else 'local' |
108
|
|
|
|
109
|
|
|
_input = helper.confirm( |
110
|
|
|
output.message( |
111
|
|
|
output.Subject.TARGET, |
112
|
|
|
f'Are you sure, you want to import the dump file into {_host_name} database?', |
113
|
|
|
False |
114
|
|
|
), |
115
|
|
|
True |
116
|
|
|
) |
117
|
|
|
|
118
|
|
|
if not _input: return |
|
|
|
|
119
|
|
|
|
120
|
|
|
database_utility.check_database_dump(mode.Client.TARGET, _dump_path) |
121
|
|
|
|
122
|
|
|
import_database_dump_file(mode.Client.TARGET, _dump_path) |
123
|
|
|
|
124
|
|
|
if 'after_dump' in system.config['target']: |
125
|
|
|
_after_dump = system.config['target']['after_dump'] |
126
|
|
|
output.message( |
127
|
|
|
output.Subject.TARGET, |
128
|
|
|
f'Importing after_dump file {output.CliFormat.BLACK}{_after_dump}{output.CliFormat.ENDC}', |
|
|
|
|
129
|
|
|
True |
130
|
|
|
) |
131
|
|
|
import_database_dump_file(mode.Client.TARGET, _after_dump) |
132
|
|
|
|
133
|
|
|
if 'post_sql' in system.config['target']: |
134
|
|
|
output.message( |
135
|
|
|
output.Subject.TARGET, |
136
|
|
|
f'Running addition post sql commands', |
|
|
|
|
137
|
|
|
True |
138
|
|
|
) |
139
|
|
|
for _sql_command in system.config['target']['post_sql']: |
140
|
|
|
database_utility.run_database_command(mode.Client.TARGET, _sql_command, True) |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
def import_database_dump_file(client, filepath): |
144
|
|
|
""" |
145
|
|
|
Import a database dump file |
146
|
|
|
:param client: String |
147
|
|
|
:param filepath: String |
148
|
|
|
:return: |
149
|
|
|
""" |
150
|
|
|
if helper.check_file_exists(client, filepath): |
151
|
|
|
mode.run_command( |
152
|
|
|
helper.get_command(client, 'mysql') + ' ' + |
153
|
|
|
database_utility.generate_mysql_credentials(client) + ' \'' + |
154
|
|
|
system.config[client]['db']['name'] + '\' < ' + filepath, |
155
|
|
|
client, |
156
|
|
|
skip_dry_run=True |
157
|
|
|
) |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
def prepare_origin_database_dump(): |
161
|
|
|
""" |
162
|
|
|
Preparing the origin database dump file by compressing them as .tar.gz |
163
|
|
|
:return: |
164
|
|
|
""" |
165
|
|
|
output.message( |
166
|
|
|
output.Subject.ORIGIN, |
167
|
|
|
'Compressing database dump', |
168
|
|
|
True |
169
|
|
|
) |
170
|
|
|
mode.run_command( |
171
|
|
|
helper.get_command(mode.Client.ORIGIN, 'tar') + ' cfvz ' + helper.get_dump_dir( |
172
|
|
|
mode.Client.ORIGIN) + database_utility.database_dump_file_name + '.tar.gz -C ' + |
173
|
|
|
helper.get_dump_dir(mode.Client.ORIGIN) + ' ' + |
174
|
|
|
database_utility.database_dump_file_name + ' > /dev/null', |
175
|
|
|
mode.Client.ORIGIN, |
176
|
|
|
skip_dry_run=True |
177
|
|
|
) |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
def prepare_target_database_dump(): |
181
|
|
|
""" |
182
|
|
|
Preparing the target database dump by the unpacked .tar.gz file |
183
|
|
|
:return: |
184
|
|
|
""" |
185
|
|
|
output.message(output.Subject.TARGET, 'Extracting database dump', True) |
186
|
|
|
mode.run_command( |
187
|
|
|
helper.get_command('target', 'tar') + ' xzf ' + helper.get_dump_dir(mode.Client.TARGET) + |
188
|
|
|
database_utility.database_dump_file_name + '.tar.gz -C ' + |
189
|
|
|
helper.get_dump_dir(mode.Client.TARGET) + ' > /dev/null', |
190
|
|
|
mode.Client.TARGET, |
191
|
|
|
skip_dry_run=True |
192
|
|
|
) |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
def clear_database(client): |
196
|
|
|
""" |
|
|
|
|
197
|
|
|
Clearing the database by dropping all tables |
198
|
|
|
https://www.techawaken.com/drop-tables-mysql-database/ |
199
|
|
|
|
200
|
|
|
{ mysql -hHOSTNAME -uUSERNAME -pPASSWORD -Nse 'show tables' DB_NAME; } | |
201
|
|
|
( while read table; do if [ -z ${i+x} ]; then echo 'SET FOREIGN_KEY_CHECKS = 0;'; fi; i=1; |
202
|
|
|
echo "drop table \`$table\`;"; done; |
203
|
|
|
echo 'SET FOREIGN_KEY_CHECKS = 1;' ) | |
204
|
|
|
awk '{print}' ORS=' ' | mysql -hHOSTNAME -uUSERNAME -pPASSWORD DB_NAME; |
205
|
|
|
|
206
|
|
|
:param client: String |
207
|
|
|
:return: |
208
|
|
|
""" |
209
|
|
|
mode.run_command( |
210
|
|
|
'{ ' + helper.get_command(client, 'mysql') + ' ' + |
211
|
|
|
database_utility.generate_mysql_credentials(client) + |
212
|
|
|
' -Nse \'show tables\' \'' + |
213
|
|
|
system.config[client]['db']['name'] + '\'; }' + |
214
|
|
|
' | ( while read table; do if [ -z ${i+x} ]; then echo \'SET FOREIGN_KEY_CHECKS = 0;\'; fi; i=1; ' + |
|
|
|
|
215
|
|
|
'echo "drop table \\`$table\\`;"; done; echo \'SET FOREIGN_KEY_CHECKS = 1;\' ) | awk \'{print}\' ORS=' ' | ' + |
|
|
|
|
216
|
|
|
' ' + |
217
|
|
|
helper.get_command(client, 'mysql') + ' ' + |
218
|
|
|
database_utility.generate_mysql_credentials(client) + ' ' + |
219
|
|
|
system.config[client]['db']['name'], |
220
|
|
|
client, |
221
|
|
|
skip_dry_run=True |
222
|
|
|
) |
223
|
|
|
|
This check looks for lines that are too long. You can specify the maximum line length.