forked from mtusi2026/mtusi2026lab2
lab2
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
#!/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 id::text || '|' || chat_id::text || '|' || text FROM messages "
|
||||
'ORDER BY created_at DESC LIMIT 5',
|
||||
],
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
).strip()
|
||||
sample = []
|
||||
if raw:
|
||||
for line in raw.splitlines():
|
||||
parts = line.split('|', 2)
|
||||
sample.append({'id': parts[0], 'chat_id': parts[1], 'text': parts[2]})
|
||||
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,
|
||||
)
|
||||
if restore.returncode not in (0, 1):
|
||||
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()
|
||||
Reference in New Issue
Block a user