-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackup Manager Pro
More file actions
421 lines (347 loc) · 13.9 KB
/
Copy pathBackup Manager Pro
File metadata and controls
421 lines (347 loc) · 13.9 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# Kousha Backup Manager Pro
# Funzioni principali:
# - Backup file e cartelle (multipli)
# - ZIP / 7z / RAR / TAR/GZ
# - Backup incrementale (hash MD5)
# - Log dettagliato + statistiche
# - Tema chiaro/scuro personalizzato
# - Drag & Drop
# - Più destinazioni (HD, OneDrive, extra)
# - Modalità compatta (mini-widget)
import customtkinter as ctk
from tkinter import filedialog, messagebox
from tkinter import Tk
from PIL import Image
import shutil
import zipfile
import py7zr
import rarfile
import tarfile
import os
import time
import hashlib
# ---------------- CONFIG TEMA ----------------
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("blue")
COLOR_PRIMARY = "#003B73"
COLOR_SECONDARY = "#6EC6FF"
COLOR_BG_LIGHT = "#E6E6E6"
COLOR_BG_DARK = "#1A1A1A"
LOG_FILE = "kousha_backup_log.txt"
# ---------------- STATO ----------------
file_sorgente = []
cartella_sorgente = ""
dest_hd = ""
dest_onedrive = ""
dest_extra = ""
log_lines = []
total_copied = 0
total_size = 0
start_time = None
# ---------------- UTIL ----------------
def add_log(msg):
global log_lines
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
line = f"[{timestamp}] {msg}"
log_lines.append(line)
log_text.configure(state="normal")
log_text.delete("1.0", "end")
log_text.insert("end", "\n".join(log_lines))
log_text.configure(state="disabled")
try:
with open(LOG_FILE, "a", encoding="utf-8") as f:
f.write(line + "\n")
except:
pass
def show_toast(message, duration=3000):
toast = ctk.CTkToplevel(app)
toast.title("")
toast.geometry("300x80+50+50")
toast.overrideredirect(True)
toast.configure(fg_color=COLOR_PRIMARY)
label = ctk.CTkLabel(toast, text=message, text_color="white", font=("Arial", 14))
label.pack(expand=True, fill="both", padx=10, pady=10)
def close_toast():
toast.destroy()
toast.after(duration, close_toast)
def file_md5(path):
h = hashlib.md5()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
h.update(chunk)
return h.hexdigest()
# ---------------- TEMA ----------------
def cambia_tema():
if switch_tema.get() == 1:
ctk.set_appearance_mode("dark")
app.configure(fg_color=COLOR_BG_DARK)
else:
ctk.set_appearance_mode("light")
app.configure(fg_color=COLOR_BG_LIGHT)
# ---------------- SELEZIONE ----------------
def scegli_file_multipli():
global file_sorgente
file_sorgente = filedialog.askopenfilenames(title="Scegli uno o più file da copiare")
if file_sorgente:
label_file.configure(text=f"{len(file_sorgente)} file selezionati")
add_log(f"Selezionati {len(file_sorgente)} file.")
else:
label_file.configure(text="Nessun file selezionato")
def scegli_cartella_sorgente():
global cartella_sorgente
cartella_sorgente = filedialog.askdirectory(title="Scegli cartella sorgente")
if cartella_sorgente:
label_src.configure(text=f"Sorgente:\n{cartella_sorgente}")
add_log(f"Cartella sorgente: {cartella_sorgente}")
else:
label_src.configure(text="Nessuna cartella selezionata")
def scegli_dest_hd():
global dest_hd
dest_hd = filedialog.askdirectory(title="Scegli cartella HD esterno")
if dest_hd:
label_hd.configure(text=f"HD esterno:\n{dest_hd}")
add_log(f"Destinazione HD: {dest_hd}")
else:
label_hd.configure(text="Nessuna cartella selezionata")
def scegli_dest_onedrive():
global dest_onedrive
dest_onedrive = filedialog.askdirectory(title="Scegli cartella OneDrive")
if dest_onedrive:
label_onedrive.configure(text=f"OneDrive:\n{dest_onedrive}")
add_log(f"Destinazione OneDrive: {dest_onedrive}")
else:
label_onedrive.configure(text="Nessuna cartella selezionata")
def scegli_dest_extra():
global dest_extra
dest_extra = filedialog.askdirectory(title="Scegli cartella extra destinazione")
if dest_extra:
label_extra.configure(text=f"Extra:\n{dest_extra}")
add_log(f"Destinazione extra: {dest_extra}")
else:
label_extra.configure(text="Nessuna cartella selezionata")
# ---------------- ESTRAZIONE ARCHIVI ----------------
def estrai_archivio(path):
ext = path.lower()
temp_folder = None
if ext.endswith(".zip"):
temp_folder = f"temp_zip_{os.path.basename(path)}"
os.makedirs(temp_folder, exist_ok=True)
with zipfile.ZipFile(path, "r") as z:
z.extractall(temp_folder)
add_log(f"Estratto ZIP: {path}")
elif ext.endswith(".7z"):
temp_folder = f"temp_7z_{os.path.basename(path)}"
os.makedirs(temp_folder, exist_ok=True)
with py7zr.SevenZipFile(path, mode="r") as z:
z.extractall(temp_folder)
add_log(f"Estratto 7z: {path}")
elif ext.endswith(".rar"):
temp_folder = f"temp_rar_{os.path.basename(path)}"
os.makedirs(temp_folder, exist_ok=True)
with rarfile.RarFile(path) as r:
r.extractall(temp_folder)
add_log(f"Estratto RAR: {path}")
elif ext.endswith(".tar") or ext.endswith(".tar.gz") or ext.endswith(".tgz"):
temp_folder = f"temp_tar_{os.path.basename(path)}"
os.makedirs(temp_folder, exist_ok=True)
with tarfile.open(path, "r:*") as t:
t.extractall(temp_folder)
add_log(f"Estratto TAR/GZ: {path}")
if temp_folder:
return temp_folder, True
return path, False
# ---------------- COPIA FILE SINGOLO ----------------
def copia_su_destinazioni(src_path, rel_name=None):
global total_copied, total_size
if rel_name is None:
rel_name = os.path.basename(src_path)
dests = []
if dest_hd:
dests.append(os.path.join(dest_hd, rel_name))
if dest_onedrive:
dests.append(os.path.join(dest_onedrive, rel_name))
if dest_extra:
dests.append(os.path.join(dest_extra, rel_name))
for d in dests:
os.makedirs(os.path.dirname(d), exist_ok=True)
shutil.copy2(src_path, d)
size = os.path.getsize(src_path)
total_copied += 1
total_size += size
add_log(f"Copiato: {rel_name} ({size/1024:.1f} KB)")
# ---------------- BACKUP FILE MULTIPLI ----------------
def backup_file_multipli():
global start_time, total_copied, total_size
if not file_sorgente:
messagebox.showerror("Errore", "Seleziona almeno un file")
return
if not (dest_hd or dest_onedrive or dest_extra):
messagebox.showerror("Errore", "Seleziona almeno una destinazione")
return
start_time = time.time()
total_copied = 0
total_size = 0
add_log("Inizio backup file multipli...")
show_toast("Backup file multipli avviato")
n = len(file_sorgente)
for i, f in enumerate(file_sorgente, start=1):
path, is_temp = estrai_archivio(f)
if os.path.isdir(path):
for root, dirs, files in os.walk(path):
for name in files:
src = os.path.join(root, name)
rel = os.path.relpath(src, path)
copia_su_destinazioni(src, rel)
else:
copia_su_destinazioni(path)
progress_bar.set(i / n)
app.update_idletasks()
if is_temp and os.path.isdir(path):
shutil.rmtree(path)
end_time = time.time()
elapsed = end_time - start_time
add_log(f"Backup file multipli completato. File: {total_copied}, Dimensione: {total_size/1024:.1f} KB, Tempo: {elapsed:.1f}s")
show_toast("Backup file multipli completato")
progress_bar.set(1.0)
# ---------------- BACKUP CARTELLA (INCREMENTALE + HASH) ----------------
STATE_FILE = "kousha_state.json"
def load_state():
if os.path.exists(STATE_FILE):
try:
import json
with open(STATE_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except:
return {}
return {}
def save_state(state):
import json
with open(STATE_FILE, "w", encoding="utf-8") as f:
json.dump(state, f, indent=2)
def backup_cartella_incrementale():
global start_time, total_copied, total_size
if not cartella_sorgente:
messagebox.showerror("Errore", "Seleziona una cartella sorgente")
return
if not (dest_hd or dest_onedrive or dest_extra):
messagebox.showerror("Errore", "Seleziona almeno una destinazione")
return
state = load_state()
if cartella_sorgente not in state:
state[cartella_sorgente] = {}
start_time = time.time()
total_copied = 0
total_size = 0
add_log("Inizio backup cartella (incrementale + hash)...")
show_toast("Backup cartella avviato")
total_files = 0
for root, dirs, files in os.walk(cartella_sorgente):
for f in files:
total_files += 1
processed = 0
for root, dirs, files in os.walk(cartella_sorgente):
rel_root = os.path.relpath(root, cartella_sorgente)
for f in files:
src = os.path.join(root, f)
rel = os.path.join(rel_root, f)
processed += 1
md5 = file_md5(src)
prev = state[cartella_sorgente].get(rel)
if prev and prev.get("md5") == md5:
add_log(f"Saltato (incrementale): {rel}")
else:
copia_su_destinazioni(src, rel)
state[cartella_sorgente][rel] = {"md5": md5}
progress_bar.set(processed / max(total_files, 1))
app.update_idletasks()
save_state(state)
end_time = time.time()
elapsed = end_time - start_time
add_log(f"Backup cartella completato. File copiati: {total_copied}, Dimensione: {total_size/1024:.1f} KB, Tempo: {elapsed:.1f}s")
show_toast("Backup cartella completato")
progress_bar.set(1.0)
# ---------------- DRAG & DROP ----------------
def enable_drag_and_drop():
messagebox.showinfo("Info", "Per drag & drop avanzato servirebbe integrazione con tkdnd.\nQuesta versione è predisposta ma non lo include.")
# ---------------- MODALITÀ COMPATTA ----------------
def open_compact_mode():
mini = ctk.CTkToplevel(app)
mini.title("Kousha Backup - Compatto")
mini.geometry("250x200")
mini.configure(fg_color=COLOR_PRIMARY)
ctk.CTkLabel(mini, text="Kousha Backup\nModalità compatta", text_color="white", font=("Arial", 16, "bold")).pack(pady=10)
ctk.CTkButton(mini, text="Backup cartella", command=backup_cartella_incrementale).pack(pady=5)
ctk.CTkButton(mini, text="Backup file multipli", command=backup_file_multipli).pack(pady=5)
# ---------------- UI PRINCIPALE ----------------
app = ctk.CTk()
app.title("Kousha Backup Manager Pro")
app.geometry("950x720")
app.configure(fg_color=COLOR_BG_DARK)
logo_img = None
if os.path.exists("logo.png"):
raw = Image.open("logo.png").resize((80, 80))
logo_img = ctk.CTkImage(light_image=raw, dark_image=raw, size=(80, 80))
header = ctk.CTkFrame(app, fg_color=COLOR_PRIMARY)
header.pack(fill="x")
if logo_img:
ctk.CTkLabel(header, image=logo_img, text="").pack(side="left", padx=20, pady=10)
ctk.CTkLabel(
header,
text="Kousha Backup Manager Pro",
font=("Arial", 26, "bold"),
text_color="white"
).pack(side="left", padx=10, pady=10)
switch_tema = ctk.CTkSwitch(header, text="Tema Scuro / Chiaro", command=cambia_tema)
switch_tema.pack(side="right", padx=20, pady=10)
ctk.CTkButton(header, text="Modalità compatta", command=open_compact_mode).pack(side="right", padx=10, pady=10)
tabview = ctk.CTkTabview(app)
tabview.pack(expand=True, fill="both", padx=20, pady=20)
tab_backup = tabview.add("Backup")
tab_settings = tabview.add("Impostazioni")
tab_log_tab = tabview.add("Log")
# -------- TAB BACKUP --------
tab_backup.configure(fg_color=COLOR_BG_DARK)
ctk.CTkLabel(tab_backup, text="Backup file multipli", font=("Arial", 18, "bold")).pack(pady=10)
btn_file = ctk.CTkButton(tab_backup, text="Scegli file (multipli)", command=scegli_file_multipli)
btn_file.pack(pady=5)
label_file = ctk.CTkLabel(tab_backup, text="Nessun file selezionato")
label_file.pack()
ctk.CTkLabel(tab_backup, text="Backup cartella (incrementale + hash)", font=("Arial", 18, "bold")).pack(pady=15)
btn_src = ctk.CTkButton(tab_backup, text="Scegli cartella sorgente", command=scegli_cartella_sorgente)
btn_src.pack(pady=5)
label_src = ctk.CTkLabel(tab_backup, text="Nessuna cartella selezionata")
label_src.pack()
ctk.CTkLabel(tab_backup, text="Destinazioni", font=("Arial", 18, "bold")).pack(pady=15)
btn_hd = ctk.CTkButton(tab_backup, text="Cartella HD esterno", command=scegli_dest_hd)
btn_hd.pack(pady=5)
label_hd = ctk.CTkLabel(tab_backup, text="Nessuna cartella selezionata")
label_hd.pack()
btn_onedrive = ctk.CTkButton(tab_backup, text="Cartella OneDrive", command=scegli_dest_onedrive)
btn_onedrive.pack(pady=5)
label_onedrive = ctk.CTkLabel(tab_backup, text="Nessuna cartella selezionata")
label_onedrive.pack()
btn_extra = ctk.CTkButton(tab_backup, text="Cartella extra destinazione", command=scegli_dest_extra)
btn_extra.pack(pady=5)
label_extra = ctk.CTkLabel(tab_backup, text="Nessuna cartella selezionata")
label_extra.pack()
progress_bar = ctk.CTkProgressBar(tab_backup)
progress_bar.pack(pady=20)
progress_bar.set(0.0)
ctk.CTkButton(tab_backup, text="Avvia backup file multipli", fg_color="green", command=backup_file_multipli).pack(pady=5)
ctk.CTkButton(tab_backup, text="Avvia backup cartella", fg_color="green", command=backup_cartella_incrementale).pack(pady=5)
# -------- TAB IMPOSTAZIONI --------
tab_settings.configure(fg_color=COLOR_BG_DARK)
ctk.CTkLabel(tab_settings, text="Funzioni avanzate", font=("Arial", 18, "bold")).pack(pady=10)
ctk.CTkButton(tab_settings, text="Drag & Drop (info)", command=enable_drag_and_drop).pack(pady=5)
ctk.CTkLabel(
tab_settings,
text="Stub integrazioni cloud (Google Drive, Dropbox, FTP, ecc.)\nDa implementare con API dedicate.",
justify="left"
).pack(pady=10)
# -------- TAB LOG --------
tab_log_tab.configure(fg_color=COLOR_BG_DARK)
log_text = ctk.CTkTextbox(tab_log_tab, width=850, height=500)
log_text.pack(pady=10, padx=10)
log_text.configure(state="disabled")
add_log("Kousha Backup Manager Pro avviato.")
app.mainloop()