1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
2
|
|
|
# -*- coding: future_fstrings -*- |
3
|
|
|
|
4
|
|
|
from db_sync_tool.utility import parser, mode, system, helper, output |
5
|
|
|
from db_sync_tool.database import utility as database_utility |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
def create_origin_database_dump(): |
9
|
|
|
""" |
10
|
|
|
Creating the origin database dump file |
11
|
|
|
:return: |
12
|
|
|
""" |
13
|
|
|
if not mode.is_import(): |
14
|
|
|
parser.get_database_configuration(mode.Client.ORIGIN) |
15
|
|
|
database_utility.generate_database_dump_filename() |
16
|
|
|
helper.check_and_create_dump_dir(mode.Client.ORIGIN, helper.get_dump_dir(mode.Client.ORIGIN)) |
|
|
|
|
17
|
|
|
|
18
|
|
|
_dump_file_path = helper.get_dump_dir(mode.Client.ORIGIN) + database_utility.database_dump_file_name |
|
|
|
|
19
|
|
|
|
20
|
|
|
output.message( |
21
|
|
|
output.Subject.ORIGIN, |
22
|
|
|
f'Creating database dump {output.CliFormat.BLACK}{_dump_file_path}{output.CliFormat.ENDC}', |
|
|
|
|
23
|
|
|
True |
24
|
|
|
) |
25
|
|
|
mode.run_command( |
26
|
|
|
helper.get_command('origin', 'mysqldump') + ' --no-tablespaces ' + |
27
|
|
|
database_utility. generate_mysql_credentials('origin') + ' ' + |
28
|
|
|
system.config['origin']['db']['name'] + ' ' + |
29
|
|
|
database_utility.generate_ignore_database_tables() + |
30
|
|
|
' > ' + _dump_file_path, |
31
|
|
|
mode.Client.ORIGIN |
32
|
|
|
) |
33
|
|
|
|
34
|
|
|
database_utility.check_database_dump(mode.Client.ORIGIN, _dump_file_path) |
35
|
|
|
prepare_origin_database_dump() |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
def import_database_dump(): |
39
|
|
|
""" |
40
|
|
|
Importing the selected database dump file |
41
|
|
|
:return: |
42
|
|
|
""" |
43
|
|
|
if not system.config['is_same_client'] and not mode.is_import(): |
44
|
|
|
prepare_target_database_dump() |
45
|
|
|
|
46
|
|
|
if system.config['clear_database']: |
47
|
|
|
output.message( |
48
|
|
|
output.Subject.TARGET, |
49
|
|
|
'Clearing database before import', |
50
|
|
|
True |
51
|
|
|
) |
52
|
|
|
clear_database(mode.Client.TARGET) |
53
|
|
|
|
54
|
|
|
if not system.config['keep_dump'] and not system.config['is_same_client']: |
55
|
|
|
output.message( |
56
|
|
|
output.Subject.TARGET, |
57
|
|
|
'Importing database dump', |
58
|
|
|
True |
59
|
|
|
) |
60
|
|
|
|
61
|
|
|
if not mode.is_import(): |
62
|
|
|
_dump_path = helper.get_dump_dir(mode.Client.TARGET) + database_utility.database_dump_file_name |
|
|
|
|
63
|
|
|
else: |
64
|
|
|
_dump_path = system.config['import'] |
|
|
|
|
65
|
|
|
|
66
|
|
|
if not system.config['yes']: |
67
|
|
|
_host_name = helper.get_ssh_host_name(mode.Client.TARGET, True) if mode.is_remote(mode.Client.TARGET) else 'local' |
|
|
|
|
68
|
|
|
|
69
|
|
|
helper.confirm( |
70
|
|
|
output.message( |
71
|
|
|
output.Subject.TARGET, |
72
|
|
|
f'Are you sure, you want to import the dump file into {_host_name} database?', |
73
|
|
|
False |
74
|
|
|
), |
75
|
|
|
True |
76
|
|
|
) |
77
|
|
|
|
78
|
|
|
database_utility.check_database_dump(mode.Client.TARGET, _dump_path) |
79
|
|
|
|
80
|
|
|
import_database_dump_file(mode.Client.TARGET, _dump_path) |
81
|
|
|
|
82
|
|
|
if 'after_dump' in system.config['target']: |
83
|
|
|
_after_dump = system.config['target']['after_dump'] |
84
|
|
|
output.message( |
85
|
|
|
output.Subject.TARGET, |
86
|
|
|
f'Importing after_dump file {output.CliFormat.BLACK}{_after_dump}{output.CliFormat.ENDC}', |
|
|
|
|
87
|
|
|
True |
88
|
|
|
) |
89
|
|
|
|
90
|
|
|
import_database_dump_file(mode.Client.TARGET, _after_dump) |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
def import_database_dump_file(client, filepath): |
94
|
|
|
""" |
95
|
|
|
Import a database dump file |
96
|
|
|
:param client: String |
97
|
|
|
:param filepath: String |
98
|
|
|
:return: |
99
|
|
|
""" |
100
|
|
|
if helper.check_file_exists(client, filepath): |
101
|
|
|
mode.run_command( |
102
|
|
|
helper.get_command(client, 'mysql') + ' ' + |
103
|
|
|
database_utility.generate_mysql_credentials(client) + ' ' + |
104
|
|
|
system.config[client]['db']['name'] + ' < ' + filepath, |
105
|
|
|
client |
106
|
|
|
) |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
def prepare_origin_database_dump(): |
110
|
|
|
""" |
111
|
|
|
Preparing the origin database dump file by compressing them as .tar.gz |
112
|
|
|
:return: |
113
|
|
|
""" |
114
|
|
|
output.message( |
115
|
|
|
output.Subject.ORIGIN, |
116
|
|
|
'Compressing database dump', |
117
|
|
|
True |
118
|
|
|
) |
119
|
|
|
mode.run_command( |
120
|
|
|
helper.get_command(mode.Client.ORIGIN, 'tar') + ' cfvz ' + helper.get_dump_dir( |
121
|
|
|
mode.Client.ORIGIN) + database_utility.database_dump_file_name + '.tar.gz -C ' + helper.get_dump_dir( |
|
|
|
|
122
|
|
|
mode.Client.ORIGIN) + ' ' + database_utility.database_dump_file_name + ' > /dev/null', |
|
|
|
|
123
|
|
|
mode.Client.ORIGIN |
124
|
|
|
) |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
def prepare_target_database_dump(): |
128
|
|
|
""" |
129
|
|
|
Preparing the target database dump by the unpacked .tar.gz file |
130
|
|
|
:return: |
131
|
|
|
""" |
132
|
|
|
output.message(output.Subject.TARGET, 'Extracting database dump', True) |
133
|
|
|
mode.run_command( |
134
|
|
|
helper.get_command('target', 'tar') + ' xzf ' + helper.get_dump_dir( |
135
|
|
|
mode.Client.TARGET) + database_utility.database_dump_file_name + '.tar.gz -C ' + helper.get_dump_dir( |
|
|
|
|
136
|
|
|
mode.Client.TARGET) + ' > /dev/null', |
|
|
|
|
137
|
|
|
mode.Client.TARGET |
138
|
|
|
) |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
def clear_database(client): |
142
|
|
|
""" |
|
|
|
|
143
|
|
|
Clearing the database by dropping all tables |
144
|
|
|
https://www.techawaken.com/drop-tables-mysql-database/ |
145
|
|
|
|
146
|
|
|
{ mysql -hHOSTNAME -uUSERNAME -pPASSWORD -Nse 'show tables' DB_NAME; } | ( while read table; do if [ -z ${i+x} ]; |
|
|
|
|
147
|
|
|
then echo 'SET FOREIGN_KEY_CHECKS = 0;'; fi; i=1; echo "drop table \`$table\`;"; done; |
148
|
|
|
echo 'SET FOREIGN_KEY_CHECKS = 1;' ) | awk '{print}' ORS=' ' | mysql -hHOSTNAME -uUSERNAME -pPASSWORD DB_NAME; |
|
|
|
|
149
|
|
|
|
150
|
|
|
:param client: String |
151
|
|
|
:return: |
152
|
|
|
""" |
153
|
|
|
mode.run_command( |
154
|
|
|
'{ ' + helper.get_command(client, 'mysql') + ' ' + |
155
|
|
|
database_utility.generate_mysql_credentials(client) + |
156
|
|
|
' -Nse \'show tables\' ' + |
157
|
|
|
system.config[client]['db']['name'] + '; }' + |
158
|
|
|
' | ( while read table; do if [ -z ${i+x} ]; then echo \'SET FOREIGN_KEY_CHECKS = 0;\'; fi; i=1; ' + |
|
|
|
|
159
|
|
|
'echo "drop table \\`$table\\`;"; done; echo \'SET FOREIGN_KEY_CHECKS = 1;\' ) | awk \'{print}\' ORS=' ' | ' + |
|
|
|
|
160
|
|
|
helper.get_command(client, 'mysql') + ' ' + |
161
|
|
|
database_utility.generate_mysql_credentials(client) + ' ' + |
162
|
|
|
system.config[client]['db']['name'], |
163
|
|
|
client |
164
|
|
|
) |
165
|
|
|
|