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 |
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
|
|
|
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) + 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
|
|
|
utility. generate_mysql_credentials('origin') + ' ' + |
28
|
|
|
system.config['origin']['db']['name'] + ' ' + |
29
|
|
|
utility.generate_ignore_database_tables() + |
30
|
|
|
' > ' + _dump_file_path, |
31
|
|
|
mode.Client.ORIGIN |
32
|
|
|
) |
33
|
|
|
|
34
|
|
|
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 not system.config['keep_dump'] and not system.config['is_same_client']: |
47
|
|
|
output.message( |
48
|
|
|
output.Subject.TARGET, |
49
|
|
|
'Importing database dump', |
50
|
|
|
True |
51
|
|
|
) |
52
|
|
|
|
53
|
|
|
if not mode.is_import(): |
54
|
|
|
_dump_path = helper.get_dump_dir(mode.Client.TARGET) + utility.database_dump_file_name |
|
|
|
|
55
|
|
|
else: |
56
|
|
|
_dump_path = system.config['import'] |
|
|
|
|
57
|
|
|
|
58
|
|
|
utility.check_database_dump(mode.Client.TARGET, _dump_path) |
59
|
|
|
|
60
|
|
|
import_database_dump_file(mode.Client.TARGET, _dump_path) |
61
|
|
|
|
62
|
|
|
if 'after_dump' in system.config['target']: |
63
|
|
|
_after_dump = system.config['target']['after_dump'] |
64
|
|
|
output.message( |
65
|
|
|
output.Subject.TARGET, |
66
|
|
|
f'Importing after_dump file {output.CliFormat.BLACK}{_after_dump}{output.CliFormat.ENDC}', |
|
|
|
|
67
|
|
|
True |
68
|
|
|
) |
69
|
|
|
|
70
|
|
|
import_database_dump_file(mode.Client.TARGET, _after_dump) |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
def import_database_dump_file(client, filepath): |
74
|
|
|
""" |
75
|
|
|
Import a database dump file |
76
|
|
|
:param client: String |
77
|
|
|
:param filepath: String |
78
|
|
|
:return: |
79
|
|
|
""" |
80
|
|
|
if helper.check_file_exists(client, filepath): |
81
|
|
|
mode.run_command( |
82
|
|
|
helper.get_command(client, 'mysql') + ' ' + |
83
|
|
|
utility.generate_mysql_credentials(client) + ' ' + |
84
|
|
|
system.config[client]['db']['name'] + ' < ' + filepath, |
85
|
|
|
client |
86
|
|
|
) |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
def prepare_origin_database_dump(): |
90
|
|
|
""" |
91
|
|
|
Preparing the origin database dump file by compressing them as .tar.gz |
92
|
|
|
:return: |
93
|
|
|
""" |
94
|
|
|
output.message( |
95
|
|
|
output.Subject.ORIGIN, |
96
|
|
|
'Compressing database dump', |
97
|
|
|
True |
98
|
|
|
) |
99
|
|
|
mode.run_command( |
100
|
|
|
helper.get_command(mode.Client.ORIGIN, 'tar') + ' cfvz ' + helper.get_dump_dir( |
101
|
|
|
mode.Client.ORIGIN) + utility.database_dump_file_name + '.tar.gz -C ' + helper.get_dump_dir( |
|
|
|
|
102
|
|
|
mode.Client.ORIGIN) + ' ' + utility.database_dump_file_name + ' > /dev/null', |
|
|
|
|
103
|
|
|
mode.Client.ORIGIN |
104
|
|
|
) |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
def prepare_target_database_dump(): |
108
|
|
|
""" |
109
|
|
|
Preparing the target database dump by the unpacked .tar.gz file |
110
|
|
|
:return: |
111
|
|
|
""" |
112
|
|
|
output.message(output.Subject.TARGET, 'Extracting database dump', True) |
113
|
|
|
mode.run_command( |
114
|
|
|
helper.get_command('target', 'tar') + ' xzf ' + helper.get_dump_dir( |
115
|
|
|
mode.Client.TARGET) + utility.database_dump_file_name + '.tar.gz -C ' + helper.get_dump_dir( |
|
|
|
|
116
|
|
|
mode.Client.TARGET) + ' > /dev/null', |
|
|
|
|
117
|
|
|
mode.Client.TARGET |
118
|
|
|
) |
119
|
|
|
|