"""Isolated backup/restore exercise. Python 3.12 standard library only. Creates a fresh temporary directory and synthetic data; does not access services. """ import hashlib import json from pathlib import Path import sqlite3 import tempfile import zipfile def main(): root = Path(tempfile.mkdtemp(prefix='duskcoil-restore-lab-')) source = root / 'source'; snapshot = root / 'snapshot'; restored = root / 'restored' for directory in (source, snapshot, restored): directory.mkdir() (source / 'note.txt').write_text('Synthetic restore fixture; not production data.\n') with sqlite3.connect(source / 'app.db') as db: db.execute('CREATE TABLE notes(id INTEGER PRIMARY KEY, text TEXT)') db.execute('INSERT INTO notes VALUES(1, ?)', ('restore-check',)) with sqlite3.connect(source / 'app.db') as db, sqlite3.connect(snapshot / 'app.db') as dest: db.backup(dest) (snapshot / 'note.txt').write_bytes((source / 'note.txt').read_bytes()) manifest = {p.name: hashlib.sha256(p.read_bytes()).hexdigest() for p in snapshot.iterdir()} with zipfile.ZipFile(root / 'backup.zip', 'w', zipfile.ZIP_DEFLATED) as archive: for name in manifest: archive.write(snapshot / name, name) with zipfile.ZipFile(root / 'backup.zip') as archive: for name in manifest: (restored / name).write_bytes(archive.read(name)) for name, digest in manifest.items(): assert hashlib.sha256((restored / name).read_bytes()).hexdigest() == digest with sqlite3.connect(restored / 'app.db') as db: assert db.execute('PRAGMA integrity_check').fetchone()[0] == 'ok' assert db.execute('SELECT text FROM notes WHERE id=1').fetchone()[0] == 'restore-check' result = {'file_hashes': 'pass', 'sqlite_integrity': 'pass', 'application_read': 'pass', 'scope': 'synthetic fixture only', 'directory': str(root)} (root / 'result.json').write_text(json.dumps(result, indent=2)) print(json.dumps(result, indent=2)) if __name__ == '__main__': main()