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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
| | import json
| | import os
| | from datetime import datetime, timezone
| | from typing import Any, AsyncGenerator
| |
| | from fastapi import APIRouter, Depends, HTTPException, Query
| | from fastapi.responses import StreamingResponse
| | from starlette.responses import FileResponse
| |
| | from app.auth import verify_token
| | from app.ops_runner import (
| | run_ops, run_ops_json, run_ops_host, run_ops_host_json, run_command_host,
| | stream_ops_host, stream_command_host, new_op_id, is_cancelled, clear_cancelled,
| | _BACKUP_TIMEOUT, OFFSITE_PYTHON,
| | )
| |
| | router = APIRouter()
| |
| |
| | def _sse(payload: dict) -> str:
| | return f"data: {json.dumps(payload)}\n\n"
| |
| |
| | def _now() -> str:
| | return datetime.now(timezone.utc).isoformat()
| |
| |
| | @router.get("/", summary="List local backups")
| | async def list_backups(
| | _: str = Depends(verify_token),
| | ) -> list[dict[str, Any]]:
| | """Returns a list of local backup records from `ops backups --json`."""
| | result = await run_ops_json(["backups"])
| | if not result["success"]:
| | raise HTTPException(status_code=500, detail=f"Failed to list backups: {result['error']}")
| |
| | data = result["data"]
| | if isinstance(data, list):
| | return data
| | if isinstance(data, dict):
| | for key in ("backups", "data", "items"):
| | if key in data and isinstance(data[key], list):
| | return data[key]
| | return [data]
| | return []
| |
| |
| | @router.get("/offsite", summary="List offsite backups")
| | async def list_offsite_backups(
| | _: str = Depends(verify_token),
| | ) -> list[dict[str, Any]]:
| | """Returns a list of offsite backup records."""
| | # Get project list from registry
| | import yaml
| | registry_path = "/opt/infrastructure/servers/hetzner-vps/registry.yaml"
| | try:
| | with open(registry_path) as f:
| | registry = yaml.safe_load(f)
| | projects = [
| | name for name, cfg in registry.get("projects", {}).items()
| | if cfg.get("backup_dir") and not cfg.get("infrastructure") and not cfg.get("static")
| | ]
| | except Exception:
| | projects = ["mdf", "seriousletter"] # Fallback
| |
| | all_backups = []
| | for project in projects:
| | result = await run_ops_host_json(["offsite", "list", project])
| | if result["success"] and isinstance(result["data"], list):
| | for b in result["data"]:
| | b["project"] = project
| | all_backups.extend(result["data"])
| | return all_backups
| |
| |
| | @router.post("/{project}/{env}", summary="Create a local backup")
| | async def create_backup(
| | project: str,
| | env: str,
| | _: str = Depends(verify_token),
| | ) -> dict[str, Any]:
| | """
| | Runs `ops backup {project} {env}` on the host.
| |
| | Runs via nsenter because ops backup delegates to project CLIs
| | that use host Python venvs.
| | """
| | result = await run_ops_host(["backup", project, env], timeout=_BACKUP_TIMEOUT)
| | if not result["success"]:
| | raise HTTPException(
| | status_code=500,
| | detail=f"Backup failed: {result['error'] or result['output']}",
| | )
| | return {
| | "success": True,
| | "output": result["output"],
| | "project": project,
| | "env": env,
| | }
| |
| |
| | async def _backup_stream(project: str, env: str) -> AsyncGenerator[str, None]:
| | """Stream backup creation progress via SSE."""
| | op_id = new_op_id()
| | yield _sse({"op_id": op_id})
| | yield _sse({"line": f"Creating backup for {project}/{env}...", "timestamp": _now()})
| |
| | try:
| | success = True
| | async for line in stream_ops_host(
| | ["backup", project, env], timeout=_BACKUP_TIMEOUT, op_id=op_id
| | ):
| | yield _sse({"line": line, "timestamp": _now()})
| | if line.startswith("[error]") or line.startswith("ERROR"):
| | success = False
| |
| | if is_cancelled(op_id):
| | yield _sse({"done": True, "success": False, "cancelled": True})
| | else:
| | yield _sse({"done": True, "success": success, "project": project, "env": env})
| | finally:
| | clear_cancelled(op_id)
| |
| |
| | @router.get("/stream/{project}/{env}", summary="Create backup with streaming output")
| | async def create_backup_stream(
| | project: str,
| | env: str,
| | _: str = Depends(verify_token),
| | ) -> StreamingResponse:
| | """Create a backup with real-time SSE progress output."""
| | return StreamingResponse(
| | _backup_stream(project, env),
| | media_type="text/event-stream",
| | headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
| | )
| |
| |
| | @router.post("/offsite/upload/{project}/{env}", summary="Upload backup to offsite")
| | async def upload_offsite(
| | project: str,
| | env: str,
| | _: str = Depends(verify_token),
| | ) -> dict[str, Any]:
| | """Runs `ops offsite upload {project} {env}` on the host."""
| | result = await run_ops_host(
| | ["offsite", "upload", project, env], timeout=_BACKUP_TIMEOUT
| | )
| | if not result["success"]:
| | raise HTTPException(
| | status_code=500,
| | detail=f"Offsite upload failed: {result['error'] or result['output']}",
| | )
| | return {"success": True, "output": result["output"], "project": project, "env": env}
| |
| |
| | async def _upload_stream(project: str, env: str, name: str | None = None) -> AsyncGenerator[str, None]:
| | """Stream offsite upload progress via SSE."""
| | op_id = new_op_id()
| | yield _sse({"op_id": op_id})
| | label = f"{project}/{env}/{name}" if name else f"{project}/{env} (latest)"
| | yield _sse({"line": f"Uploading {label} to offsite storage...", "timestamp": _now()})
| |
| | cmd = ["offsite", "upload", project, env]
| | if name:
| | cmd.append(name)
| |
| | try:
| | success = True
| | async for line in stream_ops_host(
| | cmd, timeout=_BACKUP_TIMEOUT, op_id=op_id
| | ):
| | yield _sse({"line": line, "timestamp": _now()})
| | if line.startswith("[error]") or line.startswith("ERROR"):
| | success = False
| |
| | if is_cancelled(op_id):
| | yield _sse({"done": True, "success": False, "cancelled": True})
| | else:
| | yield _sse({"done": True, "success": success, "project": project, "env": env})
| | finally:
| | clear_cancelled(op_id)
| |
| |
| | @router.get("/offsite/stream/{project}/{env}", summary="Upload to offsite with streaming output")
| | async def upload_offsite_stream(
| | project: str,
| | env: str,
| | name: str | None = Query(None),
| | _: str = Depends(verify_token),
| | ) -> StreamingResponse:
| | """Upload backup to offsite with real-time SSE progress output."""
| | return StreamingResponse(
| | _upload_stream(project, env, name),
| | media_type="text/event-stream",
| | headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
| | )
| |
| |
| | async def _download_stream(project: str, env: str, name: str) -> AsyncGenerator[str, None]:
| | """Stream offsite-to-local download progress via SSE."""
| | op_id = new_op_id()
| | yield _sse({"op_id": op_id})
| | yield _sse({"line": f"Downloading {name} from offsite to local storage...", "timestamp": _now()})
| |
| | # Download to the local backup directory so it appears in the backup list
| | local_path = f"/opt/data/backups/{project}/{env}/{name}"
| | cmd = [
| | OFFSITE_PYTHON, "-c",
| | f"import sys; sys.stdout.reconfigure(line_buffering=True); "
| | f"sys.path.insert(0, '/opt/data/scripts'); "
| | f"from offsite import download; from pathlib import Path; "
| | f"import os; os.makedirs('/opt/data/backups/{project}/{env}', exist_ok=True); "
| | f"ok = download('{name}', Path('{local_path}'), '{project}', '{env}'); "
| | f"sys.exit(0 if ok else 1)"
| | ]
| |
| | try:
| | success = True
| | async for line in stream_command_host(cmd, timeout=_BACKUP_TIMEOUT, op_id=op_id):
| | yield _sse({"line": line, "timestamp": _now()})
| | if line.startswith("[error]") or line.startswith("ERROR") or "failed" in line.lower():
| | success = False
| |
| | if is_cancelled(op_id):
| | yield _sse({"done": True, "success": False, "cancelled": True})
| | else:
| | yield _sse({"done": True, "success": success, "project": project, "env": env, "name": name})
| | finally:
| | clear_cancelled(op_id)
| |
| |
| | @router.get("/offsite/download/stream/{project}/{env}", summary="Download offsite backup to local storage with streaming output")
| | async def download_offsite_stream(
| | project: str,
| | env: str,
| | name: str = Query(...),
| | _: str = Depends(verify_token),
| | ) -> StreamingResponse:
| | """Download an offsite backup to local storage with real-time SSE progress output."""
| | if "/" in name or "\\" in name or ".." in name:
| | raise HTTPException(status_code=400, detail="Invalid backup name")
| | return StreamingResponse(
| | _download_stream(project, env, name),
| | media_type="text/event-stream",
| | headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
| | )
| |
| |
| | @router.post("/offsite/retention", summary="Apply offsite retention policy")
| | async def apply_retention(
| | _: str = Depends(verify_token),
| | ) -> dict[str, Any]:
| | """Runs `ops offsite retention` on the host."""
| | result = await run_ops_host(["offsite", "retention"], timeout=_BACKUP_TIMEOUT)
| | if not result["success"]:
| | raise HTTPException(
| | status_code=500,
| | detail=f"Retention policy failed: {result['error'] or result['output']}",
| | )
| | return {"success": True, "output": result["output"]}
| |
| |
| | @router.delete("/{project}/{env}/{name}", summary="Delete a backup")
| | async def delete_backup(
| | project: str,
| | env: str,
| | name: str,
| | target: str = Query("local", regex="^(local|offsite|both)$"),
| | _: str = Depends(verify_token),
| | ) -> dict[str, Any]:
| | """
| | Delete a backup from local storage, offsite, or both.
| |
| | Query param `target`: local | offsite | both (default: local).
| | """
| | if "/" in name or "\\" in name or ".." in name:
| | raise HTTPException(status_code=400, detail="Invalid backup name")
| |
| | results = {"local": None, "offsite": None}
| |
| | # Delete local
| | if target in ("local", "both"):
| | backup_path = f"/opt/data/backups/{project}/{env}/{name}"
| | check = await run_command_host(["test", "-f", backup_path])
| | if check["success"]:
| | result = await run_command_host(["rm", backup_path])
| | results["local"] = "ok" if result["success"] else "failed"
| | else:
| | results["local"] = "not_found"
| |
| | # Delete offsite
| | if target in ("offsite", "both"):
| | result = await run_command_host([
| | "/opt/data/\u03c0/bin/python3", "-c",
| | f"import sys; sys.path.insert(0, '/opt/data/scripts'); "
| | f"from offsite import delete; "
| | f"ok = delete('{name}', '{project}', '{env}', quiet=True); "
| | f"sys.exit(0 if ok else 1)"
| | ])
| | results["offsite"] = "ok" if result["success"] else "failed"
| |
| | # Check if anything succeeded
| | any_ok = "ok" in results.values()
| | if not any_ok:
| | raise HTTPException(status_code=500, detail=f"Delete failed: {results}")
| |
| | return {"success": True, "project": project, "env": env, "name": name, "results": results}
| |
| |
| | @router.get("/download/{project}/{env}/{name}", summary="Download a backup to browser")
| | async def download_backup(
| | project: str,
| | env: str,
| | name: str,
| | _: str = Depends(verify_token),
| | ) -> FileResponse:
| | """Serve a local backup file as a browser download."""
| | for param in (project, env, name):
| | if "/" in param or "\\" in param or ".." in param:
| | raise HTTPException(status_code=400, detail="Invalid path parameter")
| |
| | backup_path = f"/opt/data/backups/{project}/{env}/{name}"
| | if not os.path.isfile(backup_path):
| | raise HTTPException(status_code=404, detail="Backup file not found")
| |
| | media_type = "application/gzip" if name.endswith(".gz") else "application/octet-stream"
| | return FileResponse(
| | path=backup_path,
| | media_type=media_type,
| | filename=name,
| | )
|
|