|
| 1 | +"""Backups nobody has to remember to take. |
| 2 | +
|
| 3 | +Two kinds, both the same encrypted file the Backup and restore page makes, so |
| 4 | +either one goes back through that page: |
| 5 | +
|
| 6 | + nightly taken once a night by the scheduler |
| 7 | + before-delete taken just before a bulk delete of findings, which does not |
| 8 | + undo any other way |
| 9 | +
|
| 10 | +The five newest of each kind are kept. The two kinds are counted apart, so a |
| 11 | +run of deletes cannot push the nightly copies out, and five quiet nights cannot |
| 12 | +push out the copy taken before a delete. |
| 13 | +
|
| 14 | +The password comes from GCR_BACKUP_PASSWORD, which install.sh writes into .env. |
| 15 | +It is never kept in the database, because a backup whose password went down |
| 16 | +with the database could never be opened. With no password set, nothing is |
| 17 | +written and the reason is given; a backup under a made up password nobody |
| 18 | +knows would be worse than none, because it looks like protection. |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import logging |
| 24 | +import os |
| 25 | +import re |
| 26 | +import threading |
| 27 | +from datetime import datetime, timezone |
| 28 | +from pathlib import Path |
| 29 | + |
| 30 | +log = logging.getLogger("gcr.autobackup") |
| 31 | + |
| 32 | +KEEP = 5 |
| 33 | +KINDS = ("nightly", "before-delete") |
| 34 | + |
| 35 | +# The only names this module writes, and so the only names it will hand back |
| 36 | +# out. Anything else in the folder is somebody else's and is left alone. |
| 37 | +NAME = re.compile(r"^gcr-(nightly|before-delete)-(\d{8}-\d{6})\.zip$") |
| 38 | + |
| 39 | +_lock = threading.Lock() |
| 40 | + |
| 41 | + |
| 42 | +class NotConfigured(Exception): |
| 43 | + """No password, so no backup can be written.""" |
| 44 | + |
| 45 | + |
| 46 | +def folder() -> Path: |
| 47 | + """Where the files go. A host folder in production, so they outlive the |
| 48 | + container and the database volume both.""" |
| 49 | + raw = os.environ.get("GCR_BACKUP_DIR", "").strip() |
| 50 | + if raw: |
| 51 | + return Path(raw) |
| 52 | + from app.config import settings |
| 53 | + return settings.data_dir / "backups" |
| 54 | + |
| 55 | + |
| 56 | +def _password() -> str: |
| 57 | + from app.core import backup |
| 58 | + value = os.environ.get("GCR_BACKUP_PASSWORD", "") |
| 59 | + if not value: |
| 60 | + raise NotConfigured( |
| 61 | + "No backup password is set. Put GCR_BACKUP_PASSWORD in .env " |
| 62 | + "(install.sh writes one) and recreate the container.") |
| 63 | + if len(value) < backup.MIN_PASSWORD: |
| 64 | + raise NotConfigured( |
| 65 | + f"GCR_BACKUP_PASSWORD is shorter than {backup.MIN_PASSWORD} " |
| 66 | + "characters, so it is not used.") |
| 67 | + return value |
| 68 | + |
| 69 | + |
| 70 | +def configured() -> bool: |
| 71 | + try: |
| 72 | + _password() |
| 73 | + except NotConfigured: |
| 74 | + return False |
| 75 | + return True |
| 76 | + |
| 77 | + |
| 78 | +def listing() -> list[dict]: |
| 79 | + """The backups on disk, newest first.""" |
| 80 | + where = folder() |
| 81 | + if not where.is_dir(): |
| 82 | + return [] |
| 83 | + found = [] |
| 84 | + for entry in where.iterdir(): |
| 85 | + match = NAME.match(entry.name) |
| 86 | + if not match or not entry.is_file(): |
| 87 | + continue |
| 88 | + stat = entry.stat() |
| 89 | + found.append({ |
| 90 | + "name": entry.name, |
| 91 | + "kind": match.group(1), |
| 92 | + "taken_at": datetime.strptime(match.group(2), "%Y%m%d-%H%M%S") |
| 93 | + .replace(tzinfo=timezone.utc), |
| 94 | + "bytes": stat.st_size, |
| 95 | + }) |
| 96 | + found.sort(key=lambda item: item["taken_at"], reverse=True) |
| 97 | + return found |
| 98 | + |
| 99 | + |
| 100 | +def path_of(name: str) -> Path | None: |
| 101 | + """The file behind a name from the listing, or None. |
| 102 | +
|
| 103 | + The name has to be one this module writes and has to be in the folder |
| 104 | + right now. Nothing from a request is ever joined onto a path unchecked. |
| 105 | + """ |
| 106 | + if not isinstance(name, str) or not NAME.match(name): |
| 107 | + return None |
| 108 | + for item in listing(): |
| 109 | + if item["name"] == name: |
| 110 | + return folder() / item["name"] |
| 111 | + return None |
| 112 | + |
| 113 | + |
| 114 | +def _prune(kind: str) -> int: |
| 115 | + mine = [item for item in listing() if item["kind"] == kind] |
| 116 | + removed = 0 |
| 117 | + for item in mine[KEEP:]: |
| 118 | + try: |
| 119 | + (folder() / item["name"]).unlink() |
| 120 | + removed += 1 |
| 121 | + except OSError: |
| 122 | + log.warning("could not remove old backup %s", item["name"], exc_info=True) |
| 123 | + return removed |
| 124 | + |
| 125 | + |
| 126 | +def take(kind: str, actor: str = "system") -> dict: |
| 127 | + """Write one backup and trim that kind back to the newest five. |
| 128 | +
|
| 129 | + Raises NotConfigured when there is no password, and lets anything else |
| 130 | + through, because a caller about to delete findings must know it failed. |
| 131 | + """ |
| 132 | + if kind not in KINDS: |
| 133 | + raise ValueError(f"unknown backup kind {kind!r}") |
| 134 | + password = _password() |
| 135 | + |
| 136 | + from app.core import backup |
| 137 | + from app.core.audit import record |
| 138 | + from app.core.db import session_scope |
| 139 | + |
| 140 | + with _lock: |
| 141 | + where = folder() |
| 142 | + where.mkdir(parents=True, exist_ok=True) |
| 143 | + stamp = datetime.now(timezone.utc).strftime("%Y%m%d-%H%M%S") |
| 144 | + name = f"gcr-{kind}-{stamp}.zip" |
| 145 | + final = where / name |
| 146 | + partial = where / f".{name}.partial" |
| 147 | + |
| 148 | + with session_scope() as db: |
| 149 | + blob = backup.create(db, password, actor=actor) |
| 150 | + |
| 151 | + # Written beside the real name and moved into place, so a half written |
| 152 | + # file never carries a name the listing would offer for a restore. |
| 153 | + fd = os.open(partial, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) |
| 154 | + try: |
| 155 | + with os.fdopen(fd, "wb") as handle: |
| 156 | + handle.write(blob) |
| 157 | + handle.flush() |
| 158 | + os.fsync(handle.fileno()) |
| 159 | + os.replace(partial, final) |
| 160 | + except BaseException: |
| 161 | + try: |
| 162 | + partial.unlink() |
| 163 | + except OSError: |
| 164 | + pass |
| 165 | + raise |
| 166 | + |
| 167 | + removed = _prune(kind) |
| 168 | + with session_scope() as db: |
| 169 | + record(db, "backup.created", actor=actor or "system", target=name, |
| 170 | + detail=f"{kind}, {len(blob)} bytes" |
| 171 | + + (f", {removed} older removed" if removed else "")) |
| 172 | + |
| 173 | + log.info("%s backup written: %s (%d bytes)", kind, name, len(blob)) |
| 174 | + return {"name": name, "bytes": len(blob), "removed": removed} |
| 175 | + |
| 176 | + |
| 177 | +def nightly() -> None: |
| 178 | + """The scheduler's job. Never raises: a failed night is logged and the |
| 179 | + next night tries again.""" |
| 180 | + try: |
| 181 | + take("nightly", actor="system") |
| 182 | + except NotConfigured as exc: |
| 183 | + log.warning("nightly backup skipped: %s", exc) |
| 184 | + except Exception: # noqa: BLE001 - logged, and tomorrow is another go |
| 185 | + log.exception("nightly backup failed") |
0 commit comments