"""Private recovery delta for the single installed release, without exporting credentials.""" from pathlib import Path import hashlib import json import os import shutil import subprocess import tarfile root = Path('/var/www/compras-deploy/current').resolve() assert root == Path('/var/www/compras-deploy/releases/presentation-20261129') identity = json.loads((root/'compras-release.json').read_text())['release_id'] assert identity == 'compras-a2711a98b34fceb3' destination = Path('/var/backups/compras-presentation-20261129/consolidation/current') destination.mkdir(mode=0o700, exist_ok=False) shutil.copy2((root/'.env').resolve(), destination/'selected.env.private') (destination/'selected.env.private').chmod(0o600) with (destination/'database.dump').open('xb') as stream: subprocess.run(['runuser', '-u', 'postgres', '--', 'pg_dump', '-Fc', '-d', 'compras_demo_20261129'], stdout=stream, check=True) subprocess.run(['pg_restore', '--list', str(destination/'database.dump')], stdout=subprocess.DEVNULL, check=True) subprocess.run(['tar', '-czf', str(destination/'storage.tar.gz'), '-C', str((root/'storage').resolve()), '.'], check=True) subprocess.run(['tar', '-czf', str(destination/'code.tar.gz'), '--exclude=./storage', '--exclude=./.env', '--exclude=./bootstrap/cache/config.php', '-C', str(root), '.'], check=True) private = (root/'storage').resolve()/'app/private' verified = 0 with tarfile.open(destination/'storage.tar.gz') as archive: for source in private.rglob('*'): assert not source.is_symlink() if source.is_file(): name = './'+str(source.relative_to((root/'storage').resolve())) assert hashlib.sha256(archive.extractfile(name).read()).hexdigest() == hashlib.sha256(source.read_bytes()).hexdigest() verified += 1 for name in ['compras-operations.service', 'compras-operations.timer']: shutil.copy2(Path('/etc/systemd/system')/name, destination/name) shutil.copy2('/var/www/deploy/compras-tempo-deploy.sh', destination/'publisher.sh') proof = {'state': 'passed', 'release_id': identity, 'backup': str(destination), 'private_files_verified_in_backup': verified, 'database_archive_readable': True, 'full_database_restore': 'prior final-backup.json, no schema delta', 'hashes': {name: hashlib.sha256((destination/name).read_bytes()).hexdigest() for name in ['database.dump', 'storage.tar.gz', 'code.tar.gz']}} (destination/'result.json').write_text(json.dumps(proof, indent=2)+'\n') print(json.dumps(proof))