1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
2
|
|
|
# -*- coding: utf-8 -*- |
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
|
|
|
'Creating database dump', |
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.option['is_same_client'] and not mode.is_import()): |
44
|
|
|
prepare_target_database_dump() |
45
|
|
|
|
46
|
|
|
if not system.option['keep_dump'] and not system.option['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.option['import'] |
|
|
|
|
57
|
|
|
|
58
|
|
|
utility.check_database_dump(mode.Client.TARGET, _dump_path) |
59
|
|
|
mode.run_command( |
60
|
|
|
helper.get_command('target', 'mysql') + ' ' + utility.generate_mysql_credentials('target') + ' ' + |
|
|
|
|
61
|
|
|
system.config['target']['db']['name'] + ' < ' + _dump_path, |
62
|
|
|
mode.Client.TARGET |
63
|
|
|
) |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
def prepare_origin_database_dump(): |
67
|
|
|
""" |
68
|
|
|
Preparing the origin database dump file by compressing them as .tar.gz |
69
|
|
|
:return: |
70
|
|
|
""" |
71
|
|
|
output.message( |
72
|
|
|
output.Subject.ORIGIN, |
73
|
|
|
'Compressing database dump', |
74
|
|
|
True |
75
|
|
|
) |
76
|
|
|
mode.run_command( |
77
|
|
|
helper.get_command(mode.Client.ORIGIN, 'tar') + ' cfvz ' + helper.get_dump_dir( |
78
|
|
|
mode.Client.ORIGIN) + utility.database_dump_file_name + '.tar.gz -C ' + helper.get_dump_dir( |
|
|
|
|
79
|
|
|
mode.Client.ORIGIN) + ' ' + utility.database_dump_file_name + ' > /dev/null', |
|
|
|
|
80
|
|
|
mode.Client.ORIGIN |
81
|
|
|
) |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
def prepare_target_database_dump(): |
85
|
|
|
""" |
86
|
|
|
Preparing the target database dump by the unpacked .tar.gz file |
87
|
|
|
:return: |
88
|
|
|
""" |
89
|
|
|
output.message(output.Subject.TARGET, 'Extracting database dump', True) |
90
|
|
|
mode.run_command( |
91
|
|
|
helper.get_command('target', 'tar') + ' xzf ' + helper.get_dump_dir( |
92
|
|
|
mode.Client.TARGET) + utility.database_dump_file_name + '.tar.gz -C ' + helper.get_dump_dir( |
|
|
|
|
93
|
|
|
mode.Client.TARGET) + ' > /dev/null', |
|
|
|
|
94
|
|
|
mode.Client.TARGET |
95
|
|
|
) |
96
|
|
|
|