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