Files
mtusi2026lab2/scripts/track2_backup_restore.py
T
2026-10-23 18:28:52 +03:00

107 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""pg_dump → fresh Postgres volume → verification via psql (no local psycopg)."""
import json
import os
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / 'evidence' / 'track2'
DUMP = OUT / 'backup.dump'
MAIN = ['docker', 'compose', '-f', 'compose.yaml']
RESTORE = ['docker', 'compose', '-f', 'compose.track2-restore.yaml']
PSQL = os.environ.get('TRACK2_PSQL_SERVICE', 'postgres')
RESTORE_SVC = 'postgres_restore'
def run(cmd, **kwargs):
subprocess.run(cmd, cwd=ROOT, check=True, **kwargs)
def psql(service, compose, database, sql):
out = subprocess.check_output(
compose + ['exec', '-T', service, 'psql', '-U', 'messenger', '-d', database, '-tAc', sql],
cwd=ROOT,
text=True,
)
return out.strip()
def snapshot(service, compose, database):
messages = int(psql(service, compose, database, 'SELECT count(*) FROM messages'))
users = int(psql(service, compose, database, 'SELECT count(*) FROM users'))
raw = subprocess.check_output(
compose + [
'exec', '-T', service, 'psql', '-U', 'messenger', '-d', database, '-tAc',
"SELECT COALESCE(json_agg(json_build_object("
"'id', id::text, 'chat_id', chat_id::text, 'text', text) "
"ORDER BY created_at DESC, id DESC), '[]'::json)::text "
'FROM (SELECT id, chat_id, text, created_at FROM messages '
'ORDER BY created_at DESC, id DESC LIMIT 5) AS sample',
],
cwd=ROOT,
text=True,
).strip()
sample = json.loads(raw)
return {'messages': messages, 'users': users, 'sample': sample}
def main():
OUT.mkdir(parents=True, exist_ok=True)
started = time.perf_counter()
run(RESTORE + ['up', '-d', '--wait'])
with DUMP.open('wb') as handle:
run(MAIN + ['exec', '-T', PSQL, 'pg_dump', '-U', 'messenger', '-Fc', 'messenger'], stdout=handle)
before = snapshot(PSQL, MAIN, 'messenger')
run(
RESTORE + ['exec', '-T', RESTORE_SVC, 'psql', '-U', 'messenger', '-d', 'postgres', '-c',
'DROP DATABASE IF EXISTS messenger_restore'],
)
run(
RESTORE + ['exec', '-T', RESTORE_SVC, 'psql', '-U', 'messenger', '-d', 'postgres', '-c',
'CREATE DATABASE messenger_restore'],
)
run(RESTORE + ['cp', str(DUMP), f'{RESTORE_SVC}:/tmp/backup.dump'])
restore = subprocess.run(
RESTORE + [
'exec', '-T', RESTORE_SVC,
'pg_restore', '-U', 'messenger', '-d', 'messenger_restore',
'--no-owner', '--no-privileges', '/tmp/backup.dump',
],
cwd=ROOT,
)
restore.check_returncode()
after = snapshot(RESTORE_SVC, RESTORE, 'messenger_restore')
elapsed = time.perf_counter() - started
ok = (
before['messages'] == after['messages']
and before['users'] == after['users']
and before['sample'] == after['sample']
)
report = {
'source': before,
'restored': after,
'counts_match': before['messages'] == after['messages'],
'sample_match': before['sample'] == after['sample'],
'restore_seconds': round(elapsed, 2),
'dump_bytes': DUMP.stat().st_size,
'data_loss_boundary': (
'Logical pg_dump is consistent at dump start; concurrent writes during dump are not '
'included. WAL/PITR not used — RPO equals changes after dump began.'
),
}
(OUT / 'restore_report.json').write_text(
json.dumps(report, indent=2, ensure_ascii=False),
encoding='utf-8',
)
print(json.dumps(report, indent=2, ensure_ascii=False))
if not ok:
raise SystemExit(1)
print('PASS: restore verification')
if __name__ == '__main__':
main()