Spaces:
Sleeping
Sleeping
File size: 1,986 Bytes
b3d02a4 | 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 | import hashlib
import os
from datetime import datetime, timezone
from typing import Dict
from .utils import load_json, save_json
BACKUPS_PATH = os.path.join(os.path.dirname(__file__), "..", "..", "infra", "backups.json")
def _timestamp() -> str:
return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
def _new_hash(device: str, timestamp: str) -> str:
return hashlib.sha256(f"{device}-{timestamp}".encode()).hexdigest()[:8]
def oob_get_last_backup(device: str) -> Dict:
backups = load_json(BACKUPS_PATH)
info = backups.get(device)
if not info:
return {
"device": device,
"last_backup_at": None,
"config_hash": None,
"snapshots": [],
"risk": "high", # no backup means risky
"message": "No backup found.",
}
age_msg = "Backup present"
if info.get("last_backup_at"):
age_msg = f"Last backup at {info['last_backup_at']}"
risk = "low"
# Optionally adjust risk based on recency; for now simple heuristic.
return {
"device": device,
"last_backup_at": info.get("last_backup_at"),
"config_hash": info.get("config_hash"),
"snapshots": info.get("snapshots", []),
"risk": risk,
"message": age_msg,
}
def oob_perform_backup(device: str) -> Dict:
backups = load_json(BACKUPS_PATH)
now = _timestamp()
cfg_hash = _new_hash(device, now)
entry = backups.get(device, {
"last_backup_at": None,
"config_hash": None,
"snapshots": [],
})
entry.update({
"last_backup_at": now,
"config_hash": cfg_hash,
})
entry.setdefault("snapshots", []).append(cfg_hash)
backups[device] = entry
save_json(BACKUPS_PATH, backups)
return {
"device": device,
"last_backup_at": now,
"config_hash": cfg_hash,
"snapshots": entry["snapshots"],
"message": "Backup completed (simulated).",
}
|