#!/usr/bin/env python3 """Preserve deployment configuration before migrations; this is not a database backup.""" from datetime import datetime, timezone import hashlib import json import os from pathlib import Path import shutil import tempfile root = Path.cwd() os.umask(0o077) parent = root / 'data/deployment-backups' parent.mkdir(parents=True, exist_ok=True) backup = Path(tempfile.mkdtemp(prefix='update-' + datetime.now(timezone.utc).strftime('%Y%m%dT%H%M%SZ-'), dir=parent)) files = list(root.glob('.env*')) + [root / name for name in ('docker-compose.yml', 'docker-compose.override.yml', 'compose.override.yaml')] records = [] for source in files: if not source.is_file() or source.name.endswith('.sample'): continue target = backup / source.name shutil.copyfile(source, target) target.chmod(0o600) records.append({'file': source.name, 'sha256': hashlib.sha256(target.read_bytes()).hexdigest()}) (backup / 'backup.json').write_text(json.dumps({'kind': 'deployment-config-only', 'files': records}, indent=2) + '\n') print('已备份升级前配置(不含数据库):' + str(backup))