File size: 7,742 Bytes
75123b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad3d3f0
75123b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad3d3f0
 
 
 
 
 
 
75123b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad3d3f0
 
 
 
 
 
75123b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""Consistent SQLite snapshot + immutable files; PostgreSQL dump support.

Restore always targets a new directory, never the running database.
"""

import argparse
import hashlib
import json
import os
import re
import shutil
import sqlite3
import subprocess
import sys
import tempfile
import zipfile
from datetime import datetime, timezone
from pathlib import Path
from urllib.parse import urlsplit, unquote

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "backend"))


def backup(output):
    from app.config import DATABASE_URL
    from app.database import SessionLocal
    from app.models import File, TrashFile
    from app.storage import read
    from sqlalchemy import select

    output = Path(output).resolve()
    output.parent.mkdir(parents=True, exist_ok=True)
    if output.exists():
        raise ValueError("Escolha um arquivo de backup que ainda não existe.")
    with tempfile.TemporaryDirectory() as temp:
        work = Path(temp)
        manifest = {
            "format": "carla-backup-v1",
            "created_at": datetime.now(timezone.utc).isoformat(),
            "files": [],
        }
        if DATABASE_URL.startswith("sqlite:///"):
            source = sqlite3.connect(DATABASE_URL.removeprefix("sqlite:///"))
            snapshot = sqlite3.connect(work / "database.sqlite3")
            source.backup(snapshot)
            source.close()
            manifest["database"] = "database.sqlite3"
            manifest["provider"] = "sqlite"
            rows = snapshot.execute("SELECT key,checksum FROM files").fetchall()
            if snapshot.execute(
                "SELECT 1 FROM sqlite_master WHERE type='table' AND name='trash_files'"
            ).fetchone():
                rows += snapshot.execute(
                    "SELECT key,checksum FROM trash_files"
                ).fetchall()
            rows = list(dict(rows).items())
            snapshot.close()
        else:
            if not shutil.which("pg_dump"):
                raise RuntimeError(
                    "PostgreSQL requer pg_dump instalado para exportar o banco."
                )
            # Prevent cleanup of unlinked objects during the dump. Stop worker for PG backups.
            parsed = urlsplit(
                DATABASE_URL.replace("postgresql+psycopg://", "postgresql://")
            )
            env = os.environ.copy()
            env["PGPASSWORD"] = unquote(parsed.password or "")
            args = [
                "pg_dump",
                "--no-owner",
                "--no-privileges",
                "-h",
                parsed.hostname or "localhost",
                "-p",
                str(parsed.port or 5432),
                "-U",
                unquote(parsed.username or ""),
                "-d",
                parsed.path.lstrip("/"),
                "-f",
                str(work / "database.sql"),
            ]
            if "sslmode=" in parsed.query:
                env["PGSSLMODE"] = dict(
                    part.split("=", 1) for part in parsed.query.split("&")
                ).get("sslmode", "require")
            subprocess.run(args, env=env, check=True)
            manifest["database"] = "database.sql"
            manifest["provider"] = "postgresql"
            with SessionLocal() as db:
                rows = list(
                    dict(
                        [(f.key, f.checksum) for f in db.scalars(select(File))]
                        + [(f.key, f.checksum) for f in db.scalars(select(TrashFile))]
                    ).items()
                )
        with zipfile.ZipFile(output, "x", compression=zipfile.ZIP_DEFLATED) as archive:
            archive.write(work / manifest["database"], manifest["database"])
            for key, checksum in rows:
                data = read(key)
                if hashlib.sha256(data).hexdigest() != checksum:
                    raise RuntimeError(f"Integridade inválida: {key}")
                archive.writestr("files/" + key, data)
                manifest["files"].append({"key": key, "checksum": checksum})
            manifest["database_checksum"] = hashlib.sha256(
                (work / manifest["database"]).read_bytes()
            ).hexdigest()
            archive.writestr(
                "manifest.json", json.dumps(manifest, ensure_ascii=False, indent=2)
            )
    return manifest


def verify(archive_path):
    with zipfile.ZipFile(archive_path) as archive:
        manifest = json.loads(archive.read("manifest.json"))
        if manifest.get("format") != "carla-backup-v1":
            raise ValueError("Formato de backup desconhecido.")
        if manifest["database"] not in {"database.sqlite3", "database.sql"}:
            raise ValueError("Banco inválido.")
        if (
            hashlib.sha256(archive.read(manifest["database"])).hexdigest()
            != manifest["database_checksum"]
        ):
            raise ValueError("Checksum do banco inválido.")
        for file in manifest["files"]:
            if not re.fullmatch(r"[a-f0-9-]{36}\.[a-z0-9]+", file["key"]):
                raise ValueError("Chave de arquivo inválida.")
            if (
                hashlib.sha256(archive.read("files/" + file["key"])).hexdigest()
                != file["checksum"]
            ):
                raise ValueError("Checksum de arquivo inválido.")
    return manifest


def restore(archive_path, destination):
    manifest = verify(archive_path)
    destination = Path(destination).resolve()
    if destination.exists():
        raise ValueError(
            "A restauração requer uma pasta nova, para preservar os dados existentes."
        )
    destination.mkdir(parents=True)
    with zipfile.ZipFile(archive_path) as archive:
        (destination / manifest["database"]).write_bytes(
            archive.read(manifest["database"])
        )
        (destination / "files").mkdir()
        for file in manifest["files"]:
            (destination / "files" / file["key"]).write_bytes(
                archive.read("files/" + file["key"])
            )
        (destination / "manifest.json").write_text(
            json.dumps(manifest, ensure_ascii=False, indent=2)
        )
    if manifest["provider"] == "sqlite":
        with sqlite3.connect(destination / "database.sqlite3") as db:
            assert db.execute("PRAGMA integrity_check").fetchone()[0] == "ok"
            assert db.execute("PRAGMA foreign_key_check").fetchall() == []
            references = db.execute("SELECT COUNT(*) FROM revision_files").fetchone()[0]
        print(f"Restauração verificada: {references} vínculos de arquivos preservados.")
    else:
        print(
            "Dump PostgreSQL restaurado na pasta; importe database.sql em um banco NOVO usando psql -v ON_ERROR_STOP=1."
        )
    return manifest


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Backup, verificação e recuperação da CARLA"
    )
    parser.add_argument("action", choices=["create", "verify", "restore"])
    parser.add_argument("path", help="Caminho do arquivo ZIP")
    parser.add_argument("--destination", help="Pasta nova para restauração")
    args = parser.parse_args()
    try:
        if args.action == "create":
            result = backup(args.path)
        elif args.action == "verify":
            result = verify(args.path)
        else:
            if not args.destination:
                parser.error("restore requer --destination")
            result = restore(args.path, args.destination)
        print(
            f'Concluído: {len(result["files"])} arquivos íntegros; formato {result["provider"]}.'
        )
    except Exception as exc:
        print(f"Falha: {exc}", file=sys.stderr)
        sys.exit(1)