-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
285 lines (246 loc) · 9.58 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import signal
import sys
from datetime import datetime
from os import makedirs, path, remove, walk
from os.path import dirname, getsize, join, splitdrive
from shutil import copy2, disk_usage, move
from time import strftime, time
import readchar
from dateutil.relativedelta import relativedelta
from util import Config, byte_to_gb, percent
def backup_drive_calc():
logger.info("Calculating Disk Summary...")
backup_usage = disk_usage(splitdrive(config.backup_path)[0])
backup_drive = splitdrive(config.backup_path)[0]
backup_drive_total = byte_to_gb(disk_usage(backup_drive).total)
backup_drive_used = byte_to_gb(disk_usage(backup_drive).used)
backup_usage_percent = percent(backup_usage.used, backup_usage.total)
logger.info(
f"{backup_drive} Backup Drive Usage: {backup_drive_used:.2f}/{backup_drive_total:.2f} GB - {backup_usage_percent}%"
)
return backup_usage_percent, backup_usage
def san_drive_calc():
san_drive = splitdrive(config.san_drive)[0]
san_usage = disk_usage(san_drive)
san_drive_total = byte_to_gb(san_usage.total)
san_drive_free = byte_to_gb(san_usage.free)
san_free_percent = percent(san_usage.free, san_usage.total)
logger.info(
f"{san_drive} San Drive Free: {san_drive_free:.2f}/{san_drive_total:.2f} GB - {san_free_percent}%"
)
san_usage = disk_usage(splitdrive(config.san_drive)[0])
san_usage_percent = percent(san_usage.used, san_usage.total)
return san_usage_percent, san_usage
def create_dest_directory(dest_path):
"""
Create the destination directory if it doesn't exist.
Args:
dest_path (str): The path of the destination directory to be created.
Returns:
bool: True if the directory was created successfully or already exists, False otherwise.
"""
try:
makedirs(dest_path, exist_ok=True)
return True
except PermissionError as e:
logger.error(f"Unable to create destination directory, Premission Denied. {e}")
return False
except OSError as e:
logger.error(f"Unable to create destination directory: {e}")
return False
except Exception as e:
logger.error(e)
return False
def handle_duplicate_files(source_file, dest_file):
"""
Handle duplicate files found in the destination directory.
Args:
source_file (str): The path of the source file.
dest_file (str): The path of the destination file.
Returns:
bool: True if the duplicate file was handled successfully, False otherwise.
"""
if path.exists(dest_file):
logger.warning(
f"Warining: File duplication found! Source: {source_file} --- Destination: {dest_file}."
)
try:
remove(source_file)
logger.info(f"Dulicated file {source_file} has been deleted.")
return True
except PermissionError as e:
logger.error(
f"Unable to delete Source file, Permission denied. {source_file}\n{e}"
)
return False
except OSError as e:
logger.error(f"Unable to delete Source file: {source_file}\n{e}")
return False
except Exception as e:
logger.error(
f"Unknown error while deleting duplicated file. {source_file}\n{e}"
)
return False
def move_files(source, dest):
"""
Move files from the source directory to the destination directory.
Args:
source (str): The path of the source directory.
dest (str): The path of the destination directory.
Returns:
None
"""
start_time, total_size, files_moved = time(), 0, 0
for root_dir, sub_dirs, files in walk(source):
sub_dirs[:] = [d for d in sub_dirs if d.lower() not in config.exclude_path]
logger.info(config.log_separator)
logger.info(f"Source directory: {root_dir}")
if files == []:
logger.info(f"No candidate file was found.")
continue
for file in files:
source_file = join(root_dir, file)
dest_file = join(dest, splitdrive(source_file)[1][1:])
dest_path = dirname(dest_file)
if not create_dest_directory(dest_path):
continue
if handle_duplicate_files(source_file, dest_file):
continue
try:
move(source_file, dest_file, copy_function=copy2)
except PermissionError as e:
logger.error(
f"Unbale to move file, Premission Denied {e.filename}. {e}"
)
continue
except Exception as e:
logger.error(f"Unexpected Error in moving files, {e}")
continue
logger.info(f"Move: {file} -> {dest_path}")
total_size += getsize(dest_file)
files_moved = files_moved + 1
run_time = time() - start_time
total_size = byte_to_gb(total_size)
logger.info(
f"{files_moved} files, {total_size} GB moved, in {run_time/60:.2f} minutes"
)
def calc_preserved_days(filedate):
last_day = datetime(filedate.year, filedate.month, 1) + relativedelta(
months=1, days=-1
)
first_day = 1
fifteenth_day = 15
days_to_keep_files = [first_day, fifteenth_day, last_day.day]
return days_to_keep_files
def clean(folder_path):
"""
Clean up outdated files in the specified folder.
This function deletes files in the given folder that were not modified on the 1st,
15th, or last day of the month within the configured number of months to keep.
Args:
folder_path (str): The path of the folder to clean up.
Returns:
None
"""
start_time, total_size, files_deleted = time(), 0, 0
for root_dir, sub_dirs, files in walk(folder_path):
sub_dirs[:] = [d for d in sub_dirs if d.lower() not in config.exclude_path]
for file in files:
file_path = join(root_dir, file)
try:
file_modify_date = datetime.fromtimestamp(
path.getmtime(file_path)
).date()
except OSError as e:
logger.error(f"Unable to get modification date: {e}")
continue
days_to_keep_files = calc_preserved_days(file_modify_date)
current_date = datetime.today().date()
if file_modify_date.day not in days_to_keep_files and file_modify_date < (
current_date - relativedelta(months=config.months_to_keep)
):
try:
file_size = path.getsize(file_path)
remove(file_path)
except PermissionError as e:
logger.error(f"Permission denied: Unable to delete file. {e}")
continue
except FileNotFoundError as e:
print(f"File not found: The file '{file_path}' does not exist. {e}")
continue
except IsADirectoryError as e:
logger.error(
f"Is a directory: {file_path}. unable to delete dir. {e}"
)
continue
except OSError as e:
logger.error(f"Unable to delete file: {e}")
continue
except Exception as e:
logger.error(f"Unexpected error while deleting file. {e}")
continue
total_size += file_size
logger.info(f"File {file_path} has been deleted.")
files_deleted = files_deleted + 1
run_time = time() - start_time
total_size = byte_to_gb(total_size)
logger.info(
f"{files_deleted} files deleted, total size {total_size} GB,in {run_time/60:.2f} minutes"
)
def main():
logger.debug(config.log_separator)
logger.debug(f"BackupManager's starting at {strftime(config.date_format)}")
manage_disk_usage()
def manage_disk_usage():
backup_usage_percent, backup_usage = backup_drive_calc()
san_usage_percent, san_usage = san_drive_calc()
if (backup_usage_percent > config.backup_usage_percent) and (
backup_usage.used < san_usage.free
):
move_files(config.backup_path, config.san_drive)
input()
sys.exit(1)
elif san_usage_percent > config.san_usage_percent:
clean(config.san_drive)
manage_disk_usage()
input()
sys.exit(1)
elif (backup_usage_percent > config.backup_usage_percent) and (
backup_usage.used > san_usage.free
):
clean(config.san_drive)
move_files(config.backup_path, config.san_drive)
input()
sys.exit(1)
else:
logger.info("Backup in right condition...")
logger.debug(f"BackupManager's ending at {strftime(config.date_format)}")
logger.debug(config.log_separator)
input()
sys.exit(1)
def exit_handler(signum, frame):
"""
Handle the exit signal by asking the user for confirmation.
Args:
signum (int): The signal number.
frame (frame object): The current stack frame.
Returns:
None
"""
messsage = "Ctrl+C was pressed. Do you really want to exit? y/n "
print(messsage, end="", flush=True)
respond = readchar.readchar()
while readchar.readchar() != b"\n":
pass
if respond == "y":
print("")
exit(1)
else:
print("", end="\r", flush=True)
print(" " * len(messsage), end="", flush=True)
print(" ", end="\r", flush=True)
if __name__ == "__main__":
config = Config()
logger = config.logging()
signal.signal(signal.SIGINT, exit_handler)
main()