File size: 24,771 Bytes
3eecd6b | 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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 | """
Backup Module - Export project ke Google Drive tanpa zip.
Backup SMART: cuma file penting (models, data, config, code).
Fitur:
- Backup selective (essential / models only / data only / full)
- Restore dari backup
- Import dataset dari Drive ke /content/data/
- List & clean backup lama
"""
import os
import json
import shutil
from datetime import datetime
from pathlib import Path
from typing import Optional, Set, List, Dict, Tuple
from rich.table import Table
from rich.panel import Panel
from rich.prompt import Prompt, Confirm
from rich.progress import (
Progress, SpinnerColumn, TextColumn, BarColumn,
TimeElapsedColumn, TimeRemainingColumn,
)
from rich import box
from utils import console, Theme, error_logger, debug_logger
# ======================================================================
# Konfigurasi
# ======================================================================
DEFAULT_SOURCE = Path("/content")
DEFAULT_DEST_BASE = Path("/content/drive/MyDrive/colab_backup")
# Folder/file yang SELALU di-skip (biar backup tidak bengkak)
SKIP_NAMES: Set[str] = {
"drive",
"sample_data",
"__pycache__",
".ipynb_checkpoints",
".git",
".config",
".cache",
"checkpoints", # checkpoint training - bisa GB
"temp", # file temporer
"logs", # log files
"backups",
"exports",
}
SKIP_EXTENSIONS: Set[str] = {
".pyc", ".pyo", ".log", ".tmp",
}
# Mode backup -> item yang di-include
BACKUP_MODES = {
"essential": {
"label": "Essential",
"desc": "config.json + *.py + models/ + data/",
"files": ["config.json"],
"globs": ["*.py"],
"dirs": ["models", "data"],
},
"models_only": {
"label": "Models only",
"desc": "config.json + models/",
"files": ["config.json"],
"globs": [],
"dirs": ["models"],
},
"data_only": {
"label": "Data only",
"desc": "data/ (dataset + KB + quizzes)",
"files": [],
"globs": [],
"dirs": ["data"],
},
"full": {
"label": "Full project",
"desc": "Semua isi /content (kecuali folder besar)",
"files": [],
"globs": [],
"dirs": [],
},
}
# ======================================================================
# Helper
# ======================================================================
def _format_size(num_bytes: int) -> str:
for unit in ["B", "KB", "MB", "GB", "TB"]:
if num_bytes < 1024:
return f"{num_bytes:.2f} {unit}"
num_bytes /= 1024
return f"{num_bytes:.2f} PB"
def _should_skip(path: Path, skip_names: Set[str], skip_exts: Set[str]) -> bool:
if path.name in skip_names:
return True
if path.is_file() and path.suffix.lower() in skip_exts:
return True
return False
# ======================================================================
# BackupManager
# ======================================================================
class BackupManager:
def __init__(
self,
source: Path = DEFAULT_SOURCE,
dest_base: Path = DEFAULT_DEST_BASE,
):
self.source = Path(source)
self.dest_base = Path(dest_base)
# ------------------------------------------------------------------
def mount_drive(self) -> bool:
if Path("/content/drive/MyDrive").exists():
console.print(Theme.dim("β Google Drive sudah ter-mount"))
return True
try:
from google.colab import drive
console.print(Theme.info("Mounting Google Drive..."))
console.print(Theme.dim("(Ikuti link otorisasi)"))
drive.mount("/content/drive")
return Path("/content/drive/MyDrive").exists()
except ImportError:
console.print(Theme.warning("β Bukan Colab β lewati mount"))
self.dest_base = Path("/content/colab_backup")
self.dest_base.mkdir(parents=True, exist_ok=True)
return True
except Exception as e:
console.print(Theme.error(f"β Gagal mount Drive: {e}"))
return False
# ------------------------------------------------------------------
def _collect_files(self, mode: str) -> List[Path]:
"""Kumpulkan file berdasarkan mode backup."""
files: List[Path] = []
if mode == "full":
for item in self.source.rglob("*"):
if not item.is_file():
continue
if _should_skip(item, SKIP_NAMES, SKIP_EXTENSIONS):
continue
try:
item.resolve().relative_to(DEFAULT_DEST_BASE.resolve())
continue
except (ValueError, OSError):
pass
files.append(item)
return files
cfg = BACKUP_MODES.get(mode)
if cfg is None:
return files
# File langsung
for fname in cfg["files"]:
f = self.source / fname
if f.exists() and f.is_file():
files.append(f)
# Glob patterns
for pattern in cfg["globs"]:
for f in self.source.glob(pattern):
if f.is_file():
files.append(f)
# Folder
for dname in cfg["dirs"]:
d = self.source / dname
if not d.exists():
continue
if d.is_file():
files.append(d)
else:
for f in d.rglob("*"):
if not f.is_file():
continue
if _should_skip(f, SKIP_NAMES, SKIP_EXTENSIONS):
continue
files.append(f)
# Dedup
seen = set()
unique = []
for f in files:
key = str(f)
if key not in seen:
seen.add(key)
unique.append(f)
return unique
# ------------------------------------------------------------------
def export(self, mode: str = "essential", dry_run: bool = False) -> Optional[Path]:
"""Backup dengan mode tertentu."""
if mode not in BACKUP_MODES:
console.print(Theme.error(f"Mode tidak dikenal: {mode}"))
return None
if not self.mount_drive():
return None
# Kumpulkan file
console.print(Theme.info(f"Scanning files (mode: {mode})..."))
files = self._collect_files(mode)
if not files:
console.print(Theme.warning("Tidak ada file untuk di-backup"))
return None
n_files = len(files)
total_bytes = 0
for f in files:
try:
total_bytes += f.stat().st_size
except Exception:
pass
mode_info = BACKUP_MODES[mode]
console.print(Panel(
f"[cyan]Mode :[/cyan] {mode_info['label']}\n"
f"[cyan]Desc :[/cyan] {mode_info['desc']}\n"
f"[cyan]Files :[/cyan] {n_files:,} files\n"
f"[cyan]Total size :[/cyan] {_format_size(total_bytes)}\n"
f"[cyan]Destination:[/cyan] {self.dest_base}",
title="[bold yellow] BACKUP PREVIEW",
style="yellow",
))
if dry_run:
console.print(Theme.dim("(dry run - tidak ada file yang di-copy)"))
return None
if not Confirm.ask("[yellow]Lanjutkan backup?", default=True):
console.print(Theme.info("Dibatalkan"))
return None
# Buat folder backup
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
dest = self.dest_base / f"{mode}_{ts}"
dest.mkdir(parents=True, exist_ok=True)
console.print(Theme.info(f"Backup ke: {dest}"))
# Copy
counter = {"copied": 0, "errors": 0, "bytes": 0}
start = datetime.now()
try:
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
TimeElapsedColumn(),
TimeRemainingColumn(),
) as progress:
task = progress.add_task("[cyan]Copying...", total=len(files))
for f in files:
try:
rel = f.relative_to(self.source)
except ValueError:
progress.update(task, advance=1)
continue
target = dest / rel
target.parent.mkdir(parents=True, exist_ok=True)
try:
shutil.copy2(f, target)
counter["copied"] += 1
counter["bytes"] += f.stat().st_size
except (PermissionError, OSError) as e:
counter["errors"] += 1
debug_logger.debug(f"Skip {f}: {e}")
progress.update(task, advance=1)
except KeyboardInterrupt:
console.print(Theme.warning("\nβ Backup dihentikan"))
return dest
except Exception as e:
error_logger.error(f"Backup error: {e}")
console.print(Theme.error(f"β Backup gagal: {e}"))
return None
elapsed = (datetime.now() - start).total_seconds()
# Tulis manifest
manifest = {
"mode": mode,
"label": mode_info["label"],
"created": datetime.now().isoformat(),
"source": str(self.source),
"files": counter["copied"],
"bytes": counter["bytes"],
"errors": counter["errors"],
"elapsed_sec": elapsed,
}
try:
with open(dest / "MANIFEST.json", "w") as f:
json.dump(manifest, f, indent=2)
except Exception:
pass
table = Table(title="Backup Result", box=box.ROUNDED)
table.add_column("Item", style="cyan")
table.add_column("Value", style="green")
table.add_row("β File di-copy", f"{counter['copied']:,}")
table.add_row("β Error", f"{counter['errors']:,}")
table.add_row("π¦ Total size", _format_size(counter['bytes']))
table.add_row("β± Waktu", f"{elapsed:.1f} detik")
table.add_row("π Lokasi", str(dest))
console.print(table)
console.print(Theme.success(f" β Backup selesai: {dest.name}"))
return dest
# ------------------------------------------------------------------
def restore(self, backup_path: Optional[Path] = None) -> bool:
"""Restore dari backup ke /content."""
if backup_path is None:
backups = self._list_backup_paths()
if not backups:
console.print(Theme.warning("Tidak ada backup tersedia"))
return False
console.print(Theme.info("Pilih backup yang mau di-restore:"))
for i, b in enumerate(backups, 1):
size = sum(f.stat().st_size for f in b.rglob("*") if f.is_file())
mode = b.name.rsplit("_", 2)[0] if "_" in b.name else "?"
print(f" {i}. [{mode}] {b.name} ({_format_size(size)})")
pilih = Prompt.ask("[yellow]Nomor", default="1")
try:
backup_path = backups[int(pilih) - 1]
except (ValueError, IndexError):
console.print(Theme.error("Pilihan invalid"))
return False
backup_path = Path(backup_path)
if not backup_path.exists():
console.print(Theme.error(f"Backup tidak ada: {backup_path}"))
return False
console.print(Panel(
f"[cyan]Backup :[/cyan] {backup_path.name}\n"
f"[cyan]Target :[/cyan] {self.source}\n\n"
"[yellow]β File yang ada di target dengan nama sama akan DITIMPA[/yellow]",
title="[bold yellow] RESTORE",
style="yellow",
))
if not Confirm.ask("[yellow]Lanjutkan restore?", default=True):
console.print(Theme.info("Dibatalkan"))
return False
counter = {"copied": 0, "errors": 0, "bytes": 0}
start = datetime.now()
try:
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
TimeElapsedColumn(),
TimeRemainingColumn(),
) as progress:
all_files = [f for f in backup_path.rglob("*") if f.is_file()]
task = progress.add_task(
"[cyan]Restoring...",
total=max(1, len(all_files)),
)
for f in all_files:
if f.name == "MANIFEST.json":
progress.update(task, advance=1)
continue
try:
rel = f.relative_to(backup_path)
except ValueError:
progress.update(task, advance=1)
continue
target = self.source / rel
target.parent.mkdir(parents=True, exist_ok=True)
try:
shutil.copy2(f, target)
counter["copied"] += 1
counter["bytes"] += f.stat().st_size
except (PermissionError, OSError) as e:
counter["errors"] += 1
debug_logger.debug(f"Skip {f}: {e}")
progress.update(task, advance=1)
except Exception as e:
error_logger.error(f"Restore error: {e}")
console.print(Theme.error(f"β Restore gagal: {e}"))
return False
elapsed = (datetime.now() - start).total_seconds()
table = Table(title="Restore Result", box=box.ROUNDED)
table.add_column("Item", style="cyan")
table.add_column("Value", style="green")
table.add_row("β File di-restore", f"{counter['copied']:,}")
table.add_row("β Error", f"{counter['errors']:,}")
table.add_row("π¦ Size", _format_size(counter['bytes']))
table.add_row("β± Waktu", f"{elapsed:.1f} detik")
table.add_row("π Target", str(self.source))
console.print(table)
self._verify_restore()
return True
# ------------------------------------------------------------------
def _verify_restore(self) -> None:
"""Cek hasil restore β models, config, dataset."""
console.print("\n" + Theme.header(" Verifikasi Restore:"))
models_dir = self.source / "models"
if models_dir.exists() and any(models_dir.iterdir()):
console.print(Theme.success("β Folder /content/models/"))
for m in models_dir.iterdir():
if m.is_dir():
size = sum(f.stat().st_size for f in m.rglob("*") if f.is_file())
console.print(f" - {m.name} ({_format_size(size)})")
else:
console.print(Theme.warning("β /content/models/ kosong"))
cfg_path = self.source / "config.json"
if cfg_path.exists():
try:
with open(cfg_path) as f:
cfg = json.load(f)
models = list(cfg.get("models", {}).keys())
current = cfg.get("current_model")
console.print(Theme.success("β /content/config.json"))
console.print(f" Models: {models}")
console.print(f" Current: {current}")
except Exception as e:
console.print(Theme.warning(f"β config.json rusak: {e}"))
else:
console.print(Theme.warning("β /content/config.json tidak ada"))
data_dir = self.source / "data"
if data_dir.exists() and any(data_dir.iterdir()):
console.print(Theme.success("β Folder /content/data/"))
for f in data_dir.iterdir():
if f.is_file():
console.print(f" - {f.name} ({_format_size(f.stat().st_size)})")
elif f.is_dir():
n = sum(1 for _ in f.rglob("*") if _.is_file())
console.print(f" - {f.name}/ ({n} files)")
# ------------------------------------------------------------------
def import_dataset_from_drive(self) -> None:
"""Import dataset dari Drive ke /content/data/."""
console.print(Panel(
Theme.header(" IMPORT DATASET DARI DRIVE"),
title="IMPORT", style="bold yellow",
))
search_dirs = [
Path("/content/drive/MyDrive/colab_backup"),
Path("/content/drive/MyDrive"),
]
supported_ext = (
".txt", ".json", ".jsonl", ".csv", ".tsv",
".parquet", ".arrow", ".json.gz", ".jsonl.gz",
)
found_files: List[Path] = []
seen = set()
for d in search_dirs:
if not d.exists():
continue
for ext in supported_ext:
for f in d.rglob(f"*{ext}"):
if f.is_file() and f.stat().st_size > 0:
key = str(f.resolve())
if key not in seen:
seen.add(key)
found_files.append(f)
if found_files:
break
if not found_files:
console.print(Theme.warning("Tidak ada file dataset di Drive"))
return
console.print(Theme.info("File dataset di Drive:"))
for i, f in enumerate(found_files, 1):
size = f.stat().st_size
console.print(f" {i}. {f.name} ({_format_size(size)})")
console.print(f" [dim]{f.parent}[/dim]")
pilih = Prompt.ask("[yellow]Pilih nomor", default="1")
try:
src = found_files[int(pilih) - 1]
except (ValueError, IndexError):
console.print(Theme.error("Pilihan invalid"))
return
dest_dir = self.source / "data"
dest_dir.mkdir(parents=True, exist_ok=True)
dest = dest_dir / src.name
try:
shutil.copy2(src, dest)
console.print(Theme.success(f"β Dataset di-copy ke {dest}"))
console.print(Theme.dim(f" Size: {_format_size(src.stat().st_size)}"))
except Exception as e:
console.print(Theme.error(f"β Gagal copy: {e}"))
# ------------------------------------------------------------------
def _list_backup_paths(self) -> List[Path]:
if not self.dest_base.exists():
return []
return sorted(
[d for d in self.dest_base.iterdir()
if d.is_dir() and not d.name.startswith(".")],
key=lambda d: d.stat().st_mtime,
reverse=True,
)
def list_backups(self) -> List[Path]:
backups = self._list_backup_paths()
if not backups:
console.print(Theme.warning("Belum ada backup"))
return []
table = Table(title=f"Backups di {self.dest_base}", box=box.ROUNDED)
table.add_column("No", style="cyan")
table.add_column("Nama", style="green")
table.add_column("Mode", style="magenta")
table.add_column("Size", style="yellow")
table.add_column("Files", style="blue")
table.add_column("Waktu", style="dim")
for idx, b in enumerate(backups, 1):
try:
files = [f for f in b.rglob("*") if f.is_file()]
size = sum(f.stat().st_size for f in files)
mtime = datetime.fromtimestamp(b.stat().st_mtime).strftime("%Y-%m-%d %H:%M")
mode = b.name.rsplit("_", 2)[0] if "_" in b.name else "?"
table.add_row(
str(idx), b.name, mode,
_format_size(size), f"{len(files):,}", mtime,
)
except Exception:
table.add_row(str(idx), b.name, "?", "?", "?", "?")
console.print(table)
return backups
# ------------------------------------------------------------------
def clean_old_backups(self, keep: int = 3) -> None:
backups = self._list_backup_paths()
to_delete = backups[keep:]
if not to_delete:
console.print(Theme.success(f"β Tidak ada backup lama (total: {len(backups)})"))
return
console.print(Theme.warning(f"Akan menghapus {len(to_delete)} backup:"))
for old in to_delete:
console.print(f" - {old.name}")
if not Confirm.ask(Theme.error(f"Hapus {len(to_delete)} backup?"), default=False):
console.print(Theme.info("Dibatalkan"))
return
for old in to_delete:
try:
shutil.rmtree(old)
console.print(Theme.success(f" β {old.name} dihapus"))
except Exception as e:
console.print(Theme.error(f" β {old.name}: {e}"))
# ------------------------------------------------------------------
def show_sizes(self) -> None:
"""Tampilkan ukuran folder penting di /content."""
table = Table(title="Ukuran folder di /content", box=box.ROUNDED)
table.add_column("Folder/File", style="cyan")
table.add_column("Size", style="green")
table.add_column("Files", style="blue")
items = [
"models", "data", "checkpoints", "logs", "temp",
"cache", "backups", "config.json",
]
for name in items:
p = self.source / name
if not p.exists():
continue
if p.is_file():
table.add_row(name, _format_size(p.stat().st_size), "1")
else:
files = [f for f in p.rglob("*") if f.is_file()]
size = sum(f.stat().st_size for f in files)
table.add_row(name, _format_size(size), f"{len(files):,}")
console.print(table)
# ======================================================================
# Menu interaktif
# ======================================================================
def backup_menu() -> None:
console.print(Panel(
Theme.header(" BACKUP & RESTORE"),
title="BACKUP", style="bold yellow",
))
bm = BackupManager()
drive_ready = Path("/content/drive/MyDrive").exists()
status = "[green]CONNECTED" if drive_ready else "[yellow]NOT MOUNTED"
console.print(f" [cyan]Google Drive:[/cyan] {status}")
bm.show_sizes()
console.print()
console.print(Theme.info("Pilihan:"))
console.print(" [green]1. Backup ESSENTIAL (models + data + code) β direkomendasikan[/green]")
console.print(" [green]2. Backup MODELS saja (paling kecil)[/green]")
console.print(" [green]3. Backup DATA saja (dataset + KB + quizzes)[/green]")
console.print(" [green]4. Backup FULL project[/green]")
console.print(" [green]5. Restore dari backup[/green]")
console.print(" [green]6. Import dataset dari Drive[/green]")
console.print(" [green]7. List backups[/green]")
console.print(" [green]8. Hapus backup lama[/green]")
console.print(" [green]9. Kembali[/green]")
choice = Prompt.ask(
"[yellow]Pilih",
choices=['1', '2', '3', '4', '5', '6', '7', '8', '9'],
)
try:
if choice == '1':
bm.export(mode="essential")
elif choice == '2':
bm.export(mode="models_only")
elif choice == '3':
bm.export(mode="data_only")
elif choice == '4':
console.print(Theme.warning("β Full backup bisa sangat besar (GB+)"))
if Confirm.ask("[yellow]Lanjutkan?", default=False):
bm.export(mode="full")
elif choice == '5':
bm.restore()
elif choice == '6':
bm.import_dataset_from_drive()
elif choice == '7':
bm.list_backups()
elif choice == '8':
keep_str = Prompt.ask("[cyan]Simpan berapa backup terbaru?", default="3")
try:
keep = max(1, int(keep_str))
except ValueError:
keep = 3
bm.clean_old_backups(keep=keep)
elif choice == '9':
return
except Exception as e:
error_logger.error(f"Backup menu error: {e}")
console.print(Theme.error(f"Error: {e}"))
input("\n[yellow]Press Enter...[/yellow]") |