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