1
|
|
|
import os |
2
|
|
|
import shutil |
3
|
|
|
from typing import List |
4
|
|
|
|
5
|
|
|
from backuppc_clone.Config import Config |
6
|
|
|
from backuppc_clone.helper.AuxiliaryFileScanner import AuxiliaryFileScanner |
7
|
|
|
from backuppc_clone.misc import sizeof_fmt |
8
|
|
|
from backuppc_clone.style.BackupPcCloneStyle import BackupPcCloneStyle |
9
|
|
|
|
10
|
|
|
|
11
|
|
View Code Duplication |
class AuxiliaryFiles: |
|
|
|
|
12
|
|
|
""" |
13
|
|
|
Copies auxiliary files from original to clone. |
14
|
|
|
""" |
15
|
|
|
|
16
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
17
|
|
|
def __init__(self, io: BackupPcCloneStyle): |
18
|
|
|
""" |
19
|
|
|
Object constructor. |
20
|
|
|
|
21
|
|
|
@param BackupPcCloneStyle io: The output style. |
22
|
|
|
""" |
23
|
|
|
|
24
|
|
|
self.__io: BackupPcCloneStyle = io |
25
|
|
|
""" |
26
|
|
|
The output style. |
27
|
|
|
""" |
28
|
|
|
|
29
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
30
|
|
|
def __remove_obsolete_files(self, files_original: List, files_clone: List) -> None: |
31
|
|
|
""" |
32
|
|
|
Removes obsolete auxiliary files of the Clone. |
33
|
|
|
|
34
|
|
|
@param list files_original: The metadata of the auxiliary files of the Original. |
35
|
|
|
@param list files_clone: The metadata of the auxiliary files of the Clone. |
36
|
|
|
""" |
37
|
|
|
self.__io.section('Removing obsolete and changed files') |
38
|
|
|
|
39
|
|
|
obsolete = [] |
40
|
|
|
for file in files_clone: |
41
|
|
|
if file not in files_original: |
42
|
|
|
obsolete.append(file) |
43
|
|
|
|
44
|
|
|
total_size = 0 |
45
|
|
|
file_count = 0 |
46
|
|
|
for file in obsolete: |
47
|
|
|
path = os.path.join(Config.instance.top_dir_clone, 'pc', file['host'], file['name']) |
48
|
|
|
self.__io.log_very_verbose('Removing <fso>{}</fso>'.format(path)) |
49
|
|
|
os.unlink(path) |
50
|
|
|
file_count += 1 |
51
|
|
|
total_size += file['size'] |
52
|
|
|
|
53
|
|
|
self.__io.writeln(' Number of files removed: {}'.format(file_count)) |
54
|
|
|
self.__io.writeln(' Total bytes freed : {} ({}B) '.format(sizeof_fmt(total_size), total_size)) |
55
|
|
|
self.__io.writeln('') |
56
|
|
|
|
57
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
58
|
|
|
def __copy_new_files(self, files_original: List, files_clone: List) -> None: |
59
|
|
|
""" |
60
|
|
|
Copies new auxiliary files from the Original to Clone. |
61
|
|
|
|
62
|
|
|
@param list files_original: The metadata of the auxiliary files of the Original. |
63
|
|
|
@param list files_clone: The metadata of the auxiliary files of the Clone. |
64
|
|
|
""" |
65
|
|
|
self.__io.section('Coping new and changed files') |
66
|
|
|
|
67
|
|
|
new = [] |
68
|
|
|
for file in files_original: |
69
|
|
|
if file not in files_clone: |
70
|
|
|
new.append(file) |
71
|
|
|
|
72
|
|
|
total_size = 0 |
73
|
|
|
file_count = 0 |
74
|
|
|
for file in new: |
75
|
|
|
host_dir_clone = Config.instance.host_dir_clone(file['host']) |
76
|
|
|
host_dir_original = Config.instance.host_dir_original(file['host']) |
77
|
|
|
|
78
|
|
|
path_clone = os.path.join(host_dir_clone, file['name']) |
79
|
|
|
path_original = os.path.join(host_dir_original, file['name']) |
80
|
|
|
|
81
|
|
|
self.__io.log_very_verbose('Coping <fso>{}</fso> to <fso>{}</fso>'.format(path_original, path_clone)) |
82
|
|
|
|
83
|
|
|
if not os.path.exists(host_dir_clone): |
84
|
|
|
os.makedirs(host_dir_clone) |
85
|
|
|
|
86
|
|
|
shutil.copy2(path_original, path_clone) |
87
|
|
|
|
88
|
|
|
file_count += 1 |
89
|
|
|
total_size += file['size'] |
90
|
|
|
|
91
|
|
|
self.__io.writeln(' Number of files copied: {}'.format(file_count)) |
92
|
|
|
self.__io.writeln(' Total bytes copied : {} ({}B) '.format(sizeof_fmt(total_size), total_size)) |
93
|
|
|
self.__io.writeln('') |
94
|
|
|
|
95
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
96
|
|
|
def synchronize(self) -> None: |
97
|
|
|
""" |
98
|
|
|
Synchronizes the auxiliary file sof the Clone with the auxiliary files of the Original. |
99
|
|
|
""" |
100
|
|
|
scanner = AuxiliaryFileScanner(self.__io) |
101
|
|
|
files_original = scanner.scan(Config.instance.pc_dir_original) |
102
|
|
|
files_clone = scanner.scan(Config.instance.pc_dir_clone) |
103
|
|
|
|
104
|
|
|
self.__io.writeln('') |
105
|
|
|
|
106
|
|
|
self.__remove_obsolete_files(files_original, files_clone) |
107
|
|
|
self.__copy_new_files(files_original, files_clone) |
108
|
|
|
|
109
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
110
|
|
|
|