1
|
|
|
""" |
2
|
|
|
BackupPC Clone |
3
|
|
|
""" |
4
|
|
|
import os |
5
|
|
|
import time |
6
|
|
|
|
7
|
|
|
from backuppc_clone.Config import Config |
8
|
|
|
from backuppc_clone.DataLayer import DataLayer |
9
|
|
|
from backuppc_clone.ProgressBar import ProgressBar |
10
|
|
|
from backuppc_clone.helper.PoolScanner import PoolScanner |
11
|
|
|
from backuppc_clone.style.BackupPcCloneStyle import BackupPcCloneStyle |
12
|
|
|
|
13
|
|
|
|
14
|
|
View Code Duplication |
class PoolSync: |
|
|
|
|
15
|
|
|
""" |
16
|
|
|
Inventories the original pool, prunes the clone pool and maintains the database. |
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 __clean_clone_pool(self) -> None: |
34
|
|
|
""" |
35
|
|
|
Removes obsolete files from pool of clone. |
36
|
|
|
""" |
37
|
|
|
self.__io.writeln('') |
38
|
|
|
self.__io.section('Clone pool') |
39
|
|
|
self.__io.writeln('') |
40
|
|
|
|
41
|
|
|
file_count = DataLayer.instance.pool_prepare_obsolete_clone_files() |
42
|
|
|
progress = ProgressBar(self.__io.output, file_count) |
43
|
|
|
|
44
|
|
|
top_dir_clone = Config.instance.top_dir_clone |
45
|
|
|
count = 0 |
46
|
|
|
for rows in DataLayer.instance.pool_yield_obsolete_clone_files(): |
47
|
|
|
for row in rows: |
48
|
|
|
try: |
49
|
|
|
path = os.path.join(top_dir_clone, row['bpl_dir'], row['bpl_name']) |
50
|
|
|
self.__io.log_very_verbose('Removing <fso>{}</fso>'.format(path)) |
51
|
|
|
os.remove(path) |
52
|
|
|
count += 1 |
53
|
|
|
except FileNotFoundError: |
54
|
|
|
# Nothing to do. |
55
|
|
|
pass |
56
|
|
|
|
57
|
|
|
DataLayer.instance.pool_delete_row(row['bpl_id']) |
58
|
|
|
progress.advance() |
59
|
|
|
|
60
|
|
|
progress.finish() |
61
|
|
|
|
62
|
|
|
self.__io.writeln('') |
63
|
|
|
self.__io.writeln(' Files removed: {}'.format(count)) |
64
|
|
|
self.__io.writeln('') |
65
|
|
|
|
66
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
67
|
|
|
def __scan_original_pool(self, csv_filename: str) -> None: |
68
|
|
|
""" |
69
|
|
|
Scans the pool of the original and stores the data into a CSV file. |
70
|
|
|
|
71
|
|
|
:param str csv_filename: The name of the CSV file. |
72
|
|
|
""" |
73
|
|
|
self.__io.section('Original pool') |
74
|
|
|
|
75
|
|
|
scanner = PoolScanner(self.__io) |
76
|
|
|
scanner.scan_directory(Config.instance.top_dir_original, ['pool', 'cpool'], csv_filename) |
77
|
|
|
|
78
|
|
|
self.__io.writeln(' Files found: {}'.format(scanner.count)) |
79
|
|
|
self.__io.writeln('') |
80
|
|
|
|
81
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
82
|
|
|
def __import_csv(self, csv_filename: str) -> None: |
83
|
|
|
""" |
84
|
|
|
Imports to CSV file with entries of the original pool into the SQLite database. |
85
|
|
|
|
86
|
|
|
:param str csv_filename: The name of the CSV file. |
87
|
|
|
""" |
88
|
|
|
self.__io.log_verbose(' Importing <fso>{}</fso> into <dbo>IMP_POOL</dbo>'.format(csv_filename)) |
89
|
|
|
|
90
|
|
|
DataLayer.instance.import_csv('IMP_POOL', ['imp_inode', 'imp_dir', 'imp_name'], csv_filename) |
91
|
|
|
|
92
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
93
|
|
|
def __update_database(self) -> None: |
94
|
|
|
""" |
95
|
|
|
Updates the database. |
96
|
|
|
""" |
97
|
|
|
self.__io.log_verbose(' Updating <dbo>BKC_POOL</dbo>') |
98
|
|
|
self.__io.writeln('') |
99
|
|
|
|
100
|
|
|
DataLayer.instance.pool_delete_obsolete_original_rows() |
101
|
|
|
DataLayer.instance.pool_insert_new_original() |
102
|
|
|
|
103
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
104
|
|
|
def synchronize(self) -> None: |
105
|
|
|
""" |
106
|
|
|
Inventories the original pool, prunes the clone pool and maintains the database. |
107
|
|
|
""" |
108
|
|
|
Config.instance.last_pool_scan = int(time.time()) |
109
|
|
|
|
110
|
|
|
csv_filename = os.path.join(Config.instance.tmp_dir_clone, 'pool.csv') |
111
|
|
|
|
112
|
|
|
self.__scan_original_pool(csv_filename) |
113
|
|
|
self.__import_csv(csv_filename) |
114
|
|
|
self.__clean_clone_pool() |
115
|
|
|
self.__update_database() |
116
|
|
|
|
117
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
118
|
|
|
|