|
1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
|
|
2
|
|
|
# -*- coding: future_fstrings -*- |
|
3
|
|
|
|
|
4
|
|
|
from db_sync_tool.utility import mode, system, output, helper |
|
5
|
|
|
from db_sync_tool.remote import client as remote_client |
|
6
|
|
|
from file_sync_tool.transfer import utility |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
def transfer_files(): |
|
10
|
|
|
""" |
|
11
|
|
|
Transfering configured files between clients |
|
12
|
|
|
:return: |
|
13
|
|
|
""" |
|
14
|
|
|
if 'files' in system.config: |
|
15
|
|
|
for config in system.config['files']['config']: |
|
16
|
|
|
output.message( |
|
17
|
|
|
output.Subject.INFO, |
|
18
|
|
|
f'Starting rsync file transfer' |
|
|
|
|
|
|
19
|
|
|
) |
|
20
|
|
|
|
|
21
|
|
|
if mode.get_sync_mode() == mode.SyncMode.PROXY: |
|
22
|
|
|
# Proxy mode: Transferring from origin to local and from local to target |
|
23
|
|
|
utility.generate_temp_dir_name() |
|
24
|
|
|
helper.check_and_create_dump_dir(mode.Client.LOCAL, utility.temp_data_dir) |
|
25
|
|
|
synchronize( |
|
26
|
|
|
origin_path=config[mode.Client.ORIGIN], |
|
27
|
|
|
target_path=utility.temp_data_dir, |
|
28
|
|
|
exclude=config['exclude'], |
|
29
|
|
|
pseudo_client=mode.Client.ORIGIN |
|
30
|
|
|
) |
|
31
|
|
|
synchronize( |
|
32
|
|
|
origin_path=f'{utility.temp_data_dir}/*', |
|
33
|
|
|
target_path=config[mode.Client.TARGET], |
|
34
|
|
|
exclude=config['exclude'], |
|
35
|
|
|
pseudo_client=mode.Client.TARGET |
|
36
|
|
|
) |
|
37
|
|
|
utility.remove_temporary_dir() |
|
38
|
|
|
if mode.get_sync_mode() == mode.SyncMode.SYNC_REMOTE: |
|
39
|
|
|
synchronize( |
|
40
|
|
|
origin_path=config[mode.Client.ORIGIN], |
|
41
|
|
|
target_path=config[mode.Client.TARGET], |
|
42
|
|
|
exclude=config['exclude'], |
|
43
|
|
|
client=mode.Client.ORIGIN, |
|
44
|
|
|
force_remote=True |
|
45
|
|
|
) |
|
46
|
|
|
else: |
|
47
|
|
|
synchronize( |
|
48
|
|
|
origin_path=config[mode.Client.ORIGIN], |
|
49
|
|
|
target_path=config[mode.Client.TARGET], |
|
50
|
|
|
exclude=config['exclude'] |
|
51
|
|
|
) |
|
52
|
|
|
else: |
|
53
|
|
|
f'{output.Subject.WARNING} No file sync configuration provided' |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
def synchronize(origin_path, target_path, exclude, client=mode.Client.LOCAL, pseudo_client=None, force_remote=False): |
|
|
|
|
|
|
57
|
|
|
""" |
|
58
|
|
|
Using rsync command to synchronize files between systems |
|
59
|
|
|
:param origin_path: String |
|
60
|
|
|
:param target_path: String |
|
61
|
|
|
:param exclude: List |
|
62
|
|
|
:param client: String |
|
63
|
|
|
:param pseudo_client: String Client, which will be forced as remote client. Necessary for proxy transfer. |
|
|
|
|
|
|
64
|
|
|
:param force_remote: Boolean |
|
65
|
|
|
:return: |
|
66
|
|
|
""" |
|
67
|
|
|
_remote_client = None |
|
68
|
|
|
if force_remote: |
|
69
|
|
|
remote_client.load_ssh_client_origin() |
|
70
|
|
|
_origin_subject = f'{output.Subject.ORIGIN}{output.CliFormat.BLACK}[REMOTE]{output.CliFormat.ENDC} ' |
|
|
|
|
|
|
71
|
|
|
_target_subject = f'{output.Subject.TARGET}{output.CliFormat.BLACK}[REMOTE]{output.CliFormat.ENDC} ' |
|
|
|
|
|
|
72
|
|
|
elif mode.is_remote(mode.Client.ORIGIN) and pseudo_client != mode.Client.TARGET: |
|
73
|
|
|
_remote_client = mode.Client.ORIGIN |
|
74
|
|
|
_origin_subject = f'{output.Subject.ORIGIN}{output.CliFormat.BLACK}[REMOTE]{output.CliFormat.ENDC} ' |
|
|
|
|
|
|
75
|
|
|
_target_subject = f'{output.Subject.TARGET}{output.CliFormat.BLACK}[LOCAL]{output.CliFormat.ENDC} ' |
|
|
|
|
|
|
76
|
|
|
elif mode.is_remote(mode.Client.TARGET) and pseudo_client != mode.Client.ORIGIN: |
|
77
|
|
|
_remote_client = mode.Client.TARGET |
|
78
|
|
|
_origin_subject = f'{output.Subject.ORIGIN}{output.CliFormat.BLACK}[LOCAL]{output.CliFormat.ENDC} ' |
|
|
|
|
|
|
79
|
|
|
_target_subject = f'{output.Subject.TARGET}{output.CliFormat.BLACK}[REMOTE]{output.CliFormat.ENDC} ' |
|
|
|
|
|
|
80
|
|
|
elif not mode.is_remote(mode.Client.TARGET) and not mode.is_remote(mode.Client.ORIGIN): |
|
81
|
|
|
_origin_subject = f'{output.Subject.ORIGIN}{output.CliFormat.BLACK}[LOCAL]{output.CliFormat.ENDC} ' |
|
|
|
|
|
|
82
|
|
|
_target_subject = f'{output.Subject.TARGET}{output.CliFormat.BLACK}[LOCAL]{output.CliFormat.ENDC} ' |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
_origin_name = helper.get_ssh_host_name(mode.Client.ORIGIN, True) if _remote_client == mode.Client.ORIGIN else '' |
|
|
|
|
|
|
85
|
|
|
_target_name = helper.get_ssh_host_name(mode.Client.TARGET, True) if _remote_client == mode.Client.TARGET else '' |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
if not system.config['mute']: |
|
88
|
|
|
print( |
|
89
|
|
|
f'{_origin_subject}' |
|
90
|
|
|
f'{_origin_name}' |
|
91
|
|
|
f'{output.CliFormat.BLACK}{origin_path}{output.CliFormat.ENDC}' |
|
92
|
|
|
) |
|
93
|
|
|
|
|
94
|
|
|
print( |
|
95
|
|
|
f'{_target_subject}' |
|
96
|
|
|
f'{_target_name}' |
|
97
|
|
|
f'{output.CliFormat.BLACK}{target_path}{output.CliFormat.ENDC}' |
|
98
|
|
|
) |
|
99
|
|
|
|
|
100
|
|
|
_origin_user_host = utility.get_host(mode.Client.ORIGIN) if _remote_client == mode.Client.ORIGIN else '' |
|
|
|
|
|
|
101
|
|
|
_target_user_host = utility.get_host(mode.Client.TARGET) if _remote_client == mode.Client.TARGET else '' |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
_output = mode.run_command( |
|
104
|
|
|
f'{utility.get_password_environment(_remote_client)}rsync {utility.get_options()} ' |
|
105
|
|
|
f'{utility.get_authorization(_remote_client)} {utility.get_excludes(exclude)}' |
|
106
|
|
|
f'{_origin_user_host}{origin_path} {_target_user_host}{target_path}', |
|
107
|
|
|
client, |
|
108
|
|
|
True |
|
109
|
|
|
) |
|
110
|
|
|
utility.read_stats(_output) |
|
111
|
|
|
|
|
|
|
|
|
|
112
|
|
|
|