-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathbackup.py
More file actions
542 lines (456 loc) · 19.3 KB
/
backup.py
File metadata and controls
542 lines (456 loc) · 19.3 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
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
#!/usr/bin/env python3
"""
EvoNexus — Workspace Backup & Restore
Export/import all gitignored user data (memory, config, logs, customizations).
Usage: python backup.py backup [--target local|s3] [--s3-bucket BUCKET]
python backup.py restore <file> [--mode merge|replace]
python backup.py list [--target local|s3] [--s3-bucket BUCKET]
"""
import argparse
import json
import os
import platform
import subprocess
import sys
import tempfile
import zipfile
from datetime import datetime
from pathlib import Path
WORKSPACE = Path(__file__).parent
BACKUPS_DIR = WORKSPACE / "backups"
# Directories to exclude wherever they appear in the path (reconstructible / heavy)
EXCLUDE_DIRS = {
"node_modules",
".venv",
"__pycache__",
"dist",
".git",
".next",
".cache",
".local",
"build",
".pytest_cache",
".ruff_cache",
".mypy_cache",
}
# Top-level directories to exclude entirely (relative to workspace root).
# These are reconstructible from source (npm install, build, git clone, etc.)
# and don't contain user data worth backing up.
EXCLUDE_TOP_LEVEL = {
"site", # static site (rebuilds from docs/)
"backups", # previous backups (don't nest backups inside backups)
".venv", # Python virtualenv
"_evo", # Evo method snapshot
"_evo-output", # Evo method output
}
EXCLUDE_EXTENSIONS = {
".pyc",
".db-shm",
".db-wal",
}
EXCLUDE_FILES = {
".DS_Store",
"Thumbs.db",
}
# ANSI colors (for non-rich fallback)
GREEN = "\033[92m"
CYAN = "\033[96m"
YELLOW = "\033[93m"
RED = "\033[91m"
BOLD = "\033[1m"
DIM = "\033[2m"
RESET = "\033[0m"
try:
from rich.console import Console
from rich.progress import Progress, SpinnerColumn, BarColumn, TextColumn
from rich.table import Table
console = Console()
HAS_RICH = True
except ImportError:
HAS_RICH = False
def banner(title: str):
if HAS_RICH:
console.print(f"\n[bold green] ╔══════════════════════════════════════╗[/]")
console.print(f"[bold green] ║ [bold white]{title:^32s}[/bold white] ║[/]")
console.print(f"[bold green] ╚══════════════════════════════════════╝[/]\n")
else:
print(f"\n{GREEN} ╔══════════════════════════════════════╗")
print(f" ║ {BOLD}{title:^32s}{RESET}{GREEN} ║")
print(f" ╚══════════════════════════════════════╝{RESET}\n")
def _get_version() -> str:
"""Read version from pyproject.toml."""
pyproject = WORKSPACE / "pyproject.toml"
if pyproject.exists():
for line in pyproject.read_text().splitlines():
if line.strip().startswith("version"):
return line.split("=")[1].strip().strip('"').strip("'")
return "unknown"
def _get_workspace_name() -> str:
"""Read workspace name from config/workspace.yaml."""
config_file = WORKSPACE / "config" / "workspace.yaml"
if config_file.exists():
try:
import yaml
config = yaml.safe_load(config_file.read_text())
return config.get("name", "EvoNexus Workspace")
except Exception:
pass
return "EvoNexus Workspace"
def _should_exclude(rel_path: str) -> bool:
"""Check if a file should be excluded from backup."""
parts = Path(rel_path).parts
# Exclude entire top-level directories (site/, backups/, etc.)
if parts and parts[0] in EXCLUDE_TOP_LEVEL:
return True
# Exclude anywhere in the path (node_modules, .venv, __pycache__, etc.)
for part in parts:
if part in EXCLUDE_DIRS:
return True
p = Path(rel_path)
if p.suffix in EXCLUDE_EXTENSIONS:
return True
if p.name in EXCLUDE_FILES:
return True
return False
def collect_files() -> list[str]:
"""Collect all gitignored files that exist on disk, minus exclusions."""
try:
result = subprocess.run(
["git", "ls-files", "--others", "--ignored", "--exclude-standard"],
capture_output=True, text=True, cwd=WORKSPACE, timeout=30
)
if result.returncode != 0:
print(f"{RED}Error running git ls-files: {result.stderr.strip()}{RESET}")
sys.exit(1)
except FileNotFoundError:
print(f"{RED}git not found. Backup requires git.{RESET}")
sys.exit(1)
except subprocess.TimeoutExpired:
print(f"{RED}git ls-files timed out. Repo may have too many ignored files.{RESET}")
sys.exit(1)
files = []
for line in result.stdout.strip().splitlines():
line = line.strip()
if not line:
continue
if _should_exclude(line):
continue
full_path = WORKSPACE / line
if full_path.is_file():
files.append(line)
return sorted(files)
def _format_size(size_bytes: int) -> str:
"""Format bytes to human-readable size."""
for unit in ["B", "KB", "MB", "GB"]:
if size_bytes < 1024:
return f"{size_bytes:.1f} {unit}"
size_bytes /= 1024
return f"{size_bytes:.1f} TB"
# ── Backup ───────────────────────────────────────
def backup_local(s3_upload: bool = False, s3_bucket: str = None) -> Path:
"""Create a ZIP backup of all gitignored user data."""
banner("Backup — Export")
files = collect_files()
if not files:
print(f"{YELLOW}No gitignored files found to backup.{RESET}")
sys.exit(0)
BACKUPS_DIR.mkdir(exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
zip_name = f"evonexus-backup-{timestamp}.zip"
zip_path = BACKUPS_DIR / zip_name
# Build manifest
file_entries = []
total_size = 0
for rel in files:
full = WORKSPACE / rel
size = full.stat().st_size
total_size += size
file_entries.append({"path": rel, "size": size})
manifest = {
"version": _get_version(),
"workspace_name": _get_workspace_name(),
"created_at": datetime.now().isoformat(),
"hostname": platform.node(),
"file_count": len(files),
"total_size": total_size,
"files": file_entries,
}
# Create ZIP
if HAS_RICH:
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("{task.completed}/{task.total}"),
console=console,
) as progress:
task = progress.add_task("Compressing...", total=len(files))
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
zf.writestr("manifest.json", json.dumps(manifest, indent=2))
for rel in files:
zf.write(WORKSPACE / rel, rel)
progress.advance(task)
else:
print(f" Compressing {len(files)} files...")
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
zf.writestr("manifest.json", json.dumps(manifest, indent=2))
for rel in files:
zf.write(WORKSPACE / rel, rel)
zip_size = zip_path.stat().st_size
if HAS_RICH:
table = Table(show_header=False, box=None, padding=(0, 2))
table.add_row("[bold]Files[/]", str(len(files)))
table.add_row("[bold]Original size[/]", _format_size(total_size))
table.add_row("[bold]ZIP size[/]", _format_size(zip_size))
table.add_row("[bold]Saved to[/]", str(zip_path.relative_to(WORKSPACE)))
console.print()
console.print(table)
console.print(f"\n [bold green]✓ Backup complete[/]")
else:
print(f"\n Files: {len(files)}")
print(f" Original size: {_format_size(total_size)}")
print(f" ZIP size: {_format_size(zip_size)}")
print(f" Saved to: {zip_path.relative_to(WORKSPACE)}")
print(f"\n {GREEN}✓ Backup complete{RESET}")
# S3 upload if requested
if s3_upload:
backup_s3_upload(zip_path, s3_bucket)
return zip_path
# ── Restore ──────────────────────────────────────
def restore_local(zip_path: Path, mode: str = "merge"):
"""Restore workspace from a ZIP backup."""
banner("Backup — Restore")
if not zip_path.exists():
print(f"{RED}File not found: {zip_path}{RESET}")
sys.exit(1)
with zipfile.ZipFile(zip_path, "r") as zf:
# Validate manifest
if "manifest.json" not in zf.namelist():
print(f"{RED}Invalid backup: missing manifest.json{RESET}")
sys.exit(1)
manifest = json.loads(zf.read("manifest.json"))
file_list = [e["path"] for e in manifest["files"]]
restored = 0
skipped = 0
if HAS_RICH:
console.print(f" [bold]Backup from:[/] {manifest['created_at']}")
console.print(f" [bold]Version:[/] {manifest['version']}")
console.print(f" [bold]Files:[/] {manifest['file_count']}")
console.print(f" [bold]Mode:[/] {mode}\n")
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("{task.completed}/{task.total}"),
console=console,
) as progress:
task = progress.add_task("Restoring...", total=len(file_list))
for rel in file_list:
dest = WORKSPACE / rel
if mode == "merge" and dest.exists():
skipped += 1
else:
dest.parent.mkdir(parents=True, exist_ok=True)
data = zf.read(rel)
dest.write_bytes(data)
restored += 1
progress.advance(task)
else:
print(f" Backup from: {manifest['created_at']}")
print(f" Version: {manifest['version']}")
print(f" Files: {manifest['file_count']}")
print(f" Mode: {mode}\n")
print(f" Restoring {len(file_list)} files...")
for rel in file_list:
dest = WORKSPACE / rel
if mode == "merge" and dest.exists():
skipped += 1
else:
dest.parent.mkdir(parents=True, exist_ok=True)
data = zf.read(rel)
dest.write_bytes(data)
restored += 1
if HAS_RICH:
table = Table(show_header=False, box=None, padding=(0, 2))
table.add_row("[bold]Restored[/]", str(restored))
table.add_row("[bold]Skipped[/]", str(skipped))
table.add_row("[bold]Total[/]", str(restored + skipped))
console.print()
console.print(table)
console.print(f"\n [bold green]✓ Restore complete ({mode} mode)[/]")
else:
print(f"\n Restored: {restored}")
print(f" Skipped: {skipped}")
print(f" Total: {restored + skipped}")
print(f"\n {GREEN}✓ Restore complete ({mode} mode){RESET}")
# ── S3 ───────────────────────────────────────────
def _require_boto3():
"""Import boto3 or exit with a clear message."""
try:
import boto3
return boto3
except ImportError:
print(f"{RED}boto3 not installed. Run: uv add boto3{RESET}")
sys.exit(1)
def _get_s3_config(s3_bucket: str = None) -> tuple[str, str]:
"""Get S3 bucket and prefix from args or env vars."""
bucket = s3_bucket or os.environ.get("BACKUP_S3_BUCKET")
if not bucket:
print(f"{RED}S3 bucket not configured. Set BACKUP_S3_BUCKET env var or use --s3-bucket.{RESET}")
sys.exit(1)
prefix = os.environ.get("BACKUP_S3_PREFIX", "evonexus-backups/")
if not prefix.endswith("/"):
prefix += "/"
return bucket, prefix
def backup_s3_upload(zip_path: Path, s3_bucket: str = None):
"""Upload a local backup ZIP to S3."""
boto3 = _require_boto3()
bucket, prefix = _get_s3_config(s3_bucket)
s3_key = prefix + zip_path.name
if HAS_RICH:
console.print(f"\n [bold]Uploading to S3...[/]")
console.print(f" Bucket: {bucket}")
console.print(f" Key: {s3_key}")
s3 = boto3.client("s3")
s3.upload_file(str(zip_path), bucket, s3_key)
if HAS_RICH:
console.print(f" [bold green]✓ Uploaded to s3://{bucket}/{s3_key}[/]")
else:
print(f" {GREEN}✓ Uploaded to s3://{bucket}/{s3_key}{RESET}")
def restore_s3(s3_key: str = None, s3_bucket: str = None, mode: str = "merge"):
"""Download a backup from S3 and restore it."""
boto3 = _require_boto3()
bucket, prefix = _get_s3_config(s3_bucket)
s3 = boto3.client("s3")
# If no key specified, list available and let user pick the latest
if not s3_key:
if HAS_RICH:
console.print(f" [bold]Listing backups in s3://{bucket}/{prefix}...[/]\n")
resp = s3.list_objects_v2(Bucket=bucket, Prefix=prefix)
contents = resp.get("Contents", [])
zips = [c for c in contents if c["Key"].endswith(".zip")]
if not zips:
print(f"{YELLOW}No backups found in s3://{bucket}/{prefix}{RESET}")
sys.exit(0)
# Use the most recent
zips.sort(key=lambda c: c["LastModified"], reverse=True)
s3_key = zips[0]["Key"]
if HAS_RICH:
console.print(f" Using latest: [bold]{s3_key}[/]\n")
else:
print(f" Using latest: {s3_key}")
# Download to temp
with tempfile.NamedTemporaryFile(suffix=".zip", delete=False) as tmp:
tmp_path = Path(tmp.name)
if HAS_RICH:
console.print(f" [bold]Downloading s3://{bucket}/{s3_key}...[/]")
s3.download_file(bucket, s3_key, str(tmp_path))
restore_local(tmp_path, mode)
tmp_path.unlink(missing_ok=True)
# ── List ─────────────────────────────────────────
def list_backups(target: str = "local", s3_bucket: str = None):
"""List available backups."""
banner("Backup — List")
if target == "local":
if not BACKUPS_DIR.exists():
print(f" {YELLOW}No backups directory found.{RESET}")
return
zips = sorted(BACKUPS_DIR.glob("evonexus-backup-*.zip"), reverse=True)
if not zips:
print(f" {YELLOW}No local backups found.{RESET}")
return
if HAS_RICH:
table = Table(title="Local Backups")
table.add_column("File", style="cyan")
table.add_column("Size", justify="right")
table.add_column("Date", style="dim")
for z in zips:
table.add_row(
z.name,
_format_size(z.stat().st_size),
datetime.fromtimestamp(z.stat().st_mtime).strftime("%Y-%m-%d %H:%M"),
)
console.print(table)
else:
for z in zips:
size = _format_size(z.stat().st_size)
date = datetime.fromtimestamp(z.stat().st_mtime).strftime("%Y-%m-%d %H:%M")
print(f" {z.name} {size:>10s} {date}")
elif target == "s3":
boto3 = _require_boto3()
bucket, prefix = _get_s3_config(s3_bucket)
s3 = boto3.client("s3")
resp = s3.list_objects_v2(Bucket=bucket, Prefix=prefix)
contents = resp.get("Contents", [])
zips = [c for c in contents if c["Key"].endswith(".zip")]
if not zips:
print(f" {YELLOW}No backups found in s3://{bucket}/{prefix}{RESET}")
return
zips.sort(key=lambda c: c["LastModified"], reverse=True)
if HAS_RICH:
table = Table(title=f"S3 Backups — s3://{bucket}/{prefix}")
table.add_column("Key", style="cyan")
table.add_column("Size", justify="right")
table.add_column("Date", style="dim")
for z in zips:
table.add_row(
z["Key"].removeprefix(prefix),
_format_size(z["Size"]),
z["LastModified"].strftime("%Y-%m-%d %H:%M"),
)
console.print(table)
else:
for z in zips:
name = z["Key"].removeprefix(prefix)
size = _format_size(z["Size"])
date = z["LastModified"].strftime("%Y-%m-%d %H:%M")
print(f" {name} {size:>10s} {date}")
# ── CLI ──────────────────────────────────────────
def main():
parser = argparse.ArgumentParser(
description="EvoNexus — Workspace Backup & Restore",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python backup.py backup # Local backup
python backup.py backup --target s3 # Local + S3 upload
python backup.py restore backups/evonexus-backup-20260409-120000.zip
python backup.py restore backups/latest.zip --mode replace
python backup.py restore --target s3 # Restore latest from S3
python backup.py list # List local backups
python backup.py list --target s3 # List S3 backups
""",
)
sub = parser.add_subparsers(dest="command", required=True)
# backup
bp = sub.add_parser("backup", help="Export workspace data to ZIP")
bp.add_argument("--target", choices=["local", "s3"], default="local", help="Backup target (default: local)")
bp.add_argument("--s3-bucket", help="S3 bucket (overrides BACKUP_S3_BUCKET env var)")
# restore
rp = sub.add_parser("restore", help="Import workspace data from ZIP")
rp.add_argument("file", nargs="?", help="Path to backup ZIP (or S3 key)")
rp.add_argument("--mode", choices=["merge", "replace"], default="merge", help="Restore mode (default: merge)")
rp.add_argument("--target", choices=["local", "s3"], default="local", help="Restore source (default: local)")
rp.add_argument("--s3-bucket", help="S3 bucket (overrides BACKUP_S3_BUCKET env var)")
# list
lp = sub.add_parser("list", help="List available backups")
lp.add_argument("--target", choices=["local", "s3"], default="local", help="List target (default: local)")
lp.add_argument("--s3-bucket", help="S3 bucket (overrides BACKUP_S3_BUCKET env var)")
args = parser.parse_args()
if args.command == "backup":
s3_upload = args.target == "s3"
backup_local(s3_upload=s3_upload, s3_bucket=args.s3_bucket)
elif args.command == "restore":
if args.target == "s3":
restore_s3(s3_key=args.file, s3_bucket=args.s3_bucket, mode=args.mode)
else:
if not args.file:
parser.error("restore requires a file path (or use --target s3)")
restore_local(Path(args.file), mode=args.mode)
elif args.command == "list":
list_backups(target=args.target, s3_bucket=args.s3_bucket)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print(f"\n{YELLOW}Cancelled.{RESET}")