-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_engine.py
More file actions
141 lines (115 loc) · 4.84 KB
/
Copy pathbackup_engine.py
File metadata and controls
141 lines (115 loc) · 4.84 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
"""
backup_engine.py
────────────────
Creates versioned ZIP archives of a directory.
Prunes old archives once MAX_VERSIONS_PER_DIR is exceeded.
Supports listing and extracting (recovery) of any stored version.
"""
import os
import re
import zipfile
from datetime import datetime
from pathlib import Path
from typing import List, Optional
from config import BACKUP_DIR, MAX_VERSIONS_PER_DIR
from logger_setup import get_logger
log = get_logger("backup")
def _archive_name(source_dir: str) -> str:
"""Build a timestamped archive filename for a given source directory."""
safe = re.sub(r"[^A-Za-z0-9_.-]", "_", os.path.basename(source_dir.rstrip("/\\")))
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
return f"{safe}__{ts}.zip"
def _existing_archives(source_dir: str) -> List[Path]:
"""Return all archives for *source_dir*, sorted oldest-first."""
safe = re.sub(r"[^A-Za-z0-9_.-]", "_", os.path.basename(source_dir.rstrip("/\\")))
pattern = re.compile(rf"^{re.escape(safe)}__\d{{8}}_\d{{6}}\.zip$")
dest = Path(BACKUP_DIR)
archives = sorted(
[p for p in dest.iterdir() if p.is_file() and pattern.match(p.name)],
key=lambda p: p.stat().st_mtime,
)
return archives
def create_backup(source_dir: str, reason: str = "manual") -> Optional[str]:
"""
Compress *source_dir* into a versioned ZIP inside BACKUP_DIR.
Returns the path of the created archive, or None on failure.
"""
source_dir = os.path.expanduser(source_dir)
if not os.path.isdir(source_dir):
log.warning("Skipping backup — directory does not exist: %s", source_dir)
return None
os.makedirs(BACKUP_DIR, exist_ok=True)
archive_path = os.path.join(BACKUP_DIR, _archive_name(source_dir))
log.info("Backing up [%s] → %s (reason: %s)", source_dir, archive_path, reason)
try:
with zipfile.ZipFile(archive_path, "w", zipfile.ZIP_DEFLATED, allowZip64=True) as zf:
for root, dirs, files in os.walk(source_dir):
# Skip hidden dirs and our own backup/log folders
dirs[:] = [d for d in dirs if not d.startswith(".")]
for fname in files:
if fname.startswith("."):
continue
full = os.path.join(root, fname)
arcname = os.path.relpath(full, os.path.dirname(source_dir))
try:
zf.write(full, arcname)
except (PermissionError, OSError) as e:
log.debug("Skipped file %s: %s", full, e)
size_mb = os.path.getsize(archive_path) / 1e6
log.info("Backup complete: %s (%.2f MB)", archive_path, size_mb)
_prune_old_archives(source_dir)
return archive_path
except Exception as exc:
log.error("Backup failed for %s: %s", source_dir, exc)
if os.path.exists(archive_path):
os.remove(archive_path)
return None
def _prune_old_archives(source_dir: str) -> None:
"""Delete oldest archives if we exceed MAX_VERSIONS_PER_DIR."""
archives = _existing_archives(source_dir)
excess = len(archives) - MAX_VERSIONS_PER_DIR
for old in archives[:excess]:
log.info("Pruning old archive: %s", old.name)
old.unlink()
def list_backups(source_dir: Optional[str] = None) -> List[dict]:
"""
Return a list of backup metadata dicts.
If *source_dir* is given, only show backups for that directory.
"""
dest = Path(BACKUP_DIR)
if not dest.exists():
return []
results = []
for p in sorted(dest.iterdir(), key=lambda f: f.stat().st_mtime, reverse=True):
if not p.suffix == ".zip":
continue
if source_dir:
safe = re.sub(r"[^A-Za-z0-9_.-]", "_", os.path.basename(source_dir.rstrip("/\\")))
if not p.name.startswith(safe + "__"):
continue
results.append({
"file": p.name,
"path": str(p),
"size_mb": round(p.stat().st_size / 1e6, 2),
"created": datetime.fromtimestamp(p.stat().st_mtime).strftime("%Y-%m-%d %H:%M:%S"),
})
return results
def restore_backup(archive_path: str, restore_to: str) -> bool:
"""
Extract *archive_path* into *restore_to*.
Returns True on success, False on failure.
"""
if not os.path.isfile(archive_path):
log.error("Archive not found: %s", archive_path)
return False
restore_to = os.path.expanduser(restore_to)
os.makedirs(restore_to, exist_ok=True)
log.info("Restoring %s → %s", archive_path, restore_to)
try:
with zipfile.ZipFile(archive_path, "r") as zf:
zf.extractall(restore_to)
log.info("Restore complete → %s", restore_to)
return True
except Exception as exc:
log.error("Restore failed: %s", exc)
return False