1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
import os |
5
|
|
|
from db_sync_tool.utility import mode, system, helper, output |
6
|
|
|
from db_sync_tool.database import utility as database_utility |
7
|
|
|
from db_sync_tool.remote import client as remote_client |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
def run_before_script(client): |
11
|
|
|
""" |
12
|
|
|
Executing before_script command |
13
|
|
|
:param client: String |
14
|
|
|
:return: |
15
|
|
|
""" |
16
|
|
|
# Run before_script after successful connection |
17
|
|
View Code Duplication |
if 'before_script' in system.config[client]: |
|
|
|
|
18
|
|
|
output.message( |
19
|
|
|
output.host_to_subject(client), |
20
|
|
|
'Running before_script', |
21
|
|
|
True |
22
|
|
|
) |
23
|
|
|
mode.run_command( |
24
|
|
|
system.config[client]['before_script'], |
25
|
|
|
client |
26
|
|
|
) |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def run_after_script(client): |
30
|
|
|
""" |
31
|
|
|
Executing after_script command |
32
|
|
|
:param client: String |
33
|
|
|
:return: |
34
|
|
|
""" |
35
|
|
|
# Run after_script after successful connection |
36
|
|
View Code Duplication |
if 'after_script' in system.config[client]: |
|
|
|
|
37
|
|
|
output.message( |
38
|
|
|
output.host_to_subject(client), |
39
|
|
|
'Running after_script', |
40
|
|
|
True |
41
|
|
|
) |
42
|
|
|
mode.run_command( |
43
|
|
|
system.config[client]['after_script'], |
44
|
|
|
client |
45
|
|
|
) |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def remove_origin_database_dump(keep_compressed_file=False): |
49
|
|
|
""" |
50
|
|
|
Removing the origin database dump files |
51
|
|
|
:param keep_compressed_file: Boolean |
52
|
|
|
:return: |
53
|
|
|
""" |
54
|
|
|
output.message( |
55
|
|
|
output.Subject.ORIGIN, |
56
|
|
|
'Cleaning up', |
57
|
|
|
True |
58
|
|
|
) |
59
|
|
|
|
60
|
|
|
_file_path = helper.get_dump_dir(mode.Client.ORIGIN) + database_utility.database_dump_file_name |
61
|
|
|
if mode.is_origin_remote(): |
62
|
|
|
sftp = remote_client.ssh_client_origin.open_sftp() |
63
|
|
|
sftp.remove(_file_path) |
64
|
|
|
if not keep_compressed_file: |
65
|
|
|
sftp.remove(f'{_file_path}.tar.gz') |
66
|
|
|
sftp.close() |
67
|
|
|
else: |
68
|
|
|
os.remove(_file_path) |
69
|
|
|
if not keep_compressed_file: |
70
|
|
|
os.remove(f'{_file_path}.tar.gz') |
71
|
|
|
|
72
|
|
|
if keep_compressed_file: |
73
|
|
|
if 'keep_dumps' in system.config[mode.Client.ORIGIN]: |
74
|
|
|
helper.clean_up_dump_dir(mode.Client.ORIGIN, helper.get_dump_dir(mode.Client.ORIGIN) + '*', |
|
|
|
|
75
|
|
|
system.config[mode.Client.ORIGIN]['keep_dumps']) |
76
|
|
|
|
77
|
|
|
output.message( |
78
|
|
|
output.Subject.INFO, |
79
|
|
|
f'Database dump file is saved to: {_file_path}.tar.gz', |
80
|
|
|
True, |
81
|
|
|
True |
82
|
|
|
) |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
def remove_target_database_dump(): |
86
|
|
|
""" |
87
|
|
|
Removing the target database dump files |
88
|
|
|
:return: |
89
|
|
|
""" |
90
|
|
|
_file_path = helper.get_dump_dir(mode.Client.TARGET) + database_utility.database_dump_file_name |
91
|
|
|
|
92
|
|
|
# |
93
|
|
|
# Move dump to specified directory |
94
|
|
|
# |
95
|
|
|
if system.option['keep_dump']: |
96
|
|
|
helper.create_local_temporary_data_dir() |
97
|
|
|
_keep_dump_path = system.default_local_sync_path + database_utility.database_dump_file_name |
98
|
|
|
mode.run_command( |
99
|
|
|
helper.get_command('target', |
100
|
|
|
'cp') + ' ' + _file_path + ' ' + _keep_dump_path, |
101
|
|
|
mode.Client.TARGET |
102
|
|
|
) |
103
|
|
|
output.message( |
104
|
|
|
output.Subject.INFO, |
105
|
|
|
f'Database dump file is saved to: {_keep_dump_path}', |
106
|
|
|
True, |
107
|
|
|
True |
108
|
|
|
) |
109
|
|
|
|
110
|
|
|
# |
111
|
|
|
# Clean up |
112
|
|
|
# |
113
|
|
|
if (not system.option['is_same_client'] and not mode.is_import()): |
114
|
|
|
output.message( |
115
|
|
|
output.Subject.TARGET, |
116
|
|
|
'Cleaning up', |
117
|
|
|
True |
118
|
|
|
) |
119
|
|
|
|
120
|
|
|
if mode.is_target_remote(): |
121
|
|
|
sftp = remote_client.ssh_client_target.open_sftp() |
122
|
|
|
sftp.remove(_file_path) |
123
|
|
|
sftp.remove(f'{_file_path}.tar.gz') |
124
|
|
|
sftp.close() |
125
|
|
|
else: |
126
|
|
|
if os.path.isfile(_file_path): |
127
|
|
|
os.remove(_file_path) |
128
|
|
|
if os.path.isfile(f'{_file_path}.tar.gz'): |
129
|
|
|
os.remove(f'{_file_path}.tar.gz') |
130
|
|
|
|