Backup and Restore: packages, Local/S3/SFTP/FTP destinations, UI and API

Encrypted packages (databases, PKI, client profiles) with safe restore and
a pre-restore safety copy, usable for server-to-server migration across
container and system-service installs. New Management page, ovp_backups
volume, migrations code in the profiler image, server name in package names.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ayurishchevandClaude Sonnet 5.5 committed 2026-10-07 09:38:09 +03:00
1 parent d29c261347
commit f8cdcd0fc6
55 files changed
+4401 -37

No files matched your search

+56
View File
@@ -94,3 +94,59 @@ Control the OpenVPN system service.
### `GET /server/process/stats`
Get telemetry for the OpenVPN process.
- **Response**: `{ "status": "running", "cpu_percent": 1.2, "memory_mb": 45.0 }`
---
## 4. Backup and Restore
Guide: [Backup and Restore](Backup_Restore.md). All routes need the JWT. Errors: `400` invalid request, `409` another job is running (or a restore is running), `502` the destination could not be reached. Messages never contain secrets.
While a restore runs, `POST /profiles`, `DELETE /profiles/{id}`, `PUT /profiles/{id}/note`, `PUT /config/*`, `POST /system/init`, `DELETE /system/pki`, `POST /server/configure` and `POST /server/process/{action}` return `409` (`"A backup restore is running"`).
| Method | Path | Purpose |
|---|---|---|
| GET, PUT | `/backup/settings` | encryption flag and passphrase, server name, default retention, local root, the implemented destination types with their fields |
| GET, POST | `/backup/destinations` | list, create (`201`) |
| PUT, DELETE | `/backup/destinations/{id}` | update, delete |
| POST | `/backup/destinations/{id}/test` | check connection, write and delete (SFTP: shows the host key when it is not trusted) |
| POST | `/backup/destinations/{id}/trust-host-key` | SFTP: pin the host key the administrator saw |
| GET | `/backup/destinations/{id}/packages` | check the source and list the packages, newest first |
| POST | `/backup/run` | start a backup (`202`) |
| POST | `/backup/restore` | start a restore (`202`) |
| GET | `/backup/status` | the current or last job |
| GET | `/backup/history` | last runs (`limit`, default 20) |
### `GET /backup/settings`, `PUT /backup/settings`
- **GET response**: `{"encryption_enabled": true, "default_retention": 14, "server_name": "vpn-main", "effective_server_name": "vpn-main", "server_name_source": "setting", "local_root": "/app/backups", "types": [{"id": "local", "label": "...", "remote": false, "pins_host_key": false, "fields": [...]}, ...]}`. The passphrase is never returned. `server_name` is the configured name (`""` when none); `effective_server_name` is the name used in package names and `server_name_source` says where it comes from: `setting`, `public_ip` (the address of the VPN settings) or `hostname`. A field of `fields` has `name`, `label`, `kind` (`text`, `number`, `bool`, `secret`, `secret_multiline`), `required`, `default`, optional `hint` and `warning` + `warn_if` (the warning applies while the value equals `warn_if`).
- **PUT body**: `{"passphrase": "..."}` (at least 8 characters) sets or changes it; `{"clear_passphrase": true}` removes it (`400` while an enabled remote destination exists); `{"default_retention": 14}`; `{"server_name": "vpn-main"}` (`^[A-Za-z0-9][A-Za-z0-9.-]{0,62}$`, else `400`; `""` clears it).
### Destinations
- **Body (POST, PUT)**: `{"name": "offsite", "type": "s3", "enabled": true, "retention_count": 14, "params": {...}, "secrets": {...}}`. `type` must be an implemented one (`local`, `s3`, `sftp`, `ftp`), otherwise `400`.
- `local`: `params: {"folder": "daily"}`, no secrets.
- `s3`: `params: {"endpoint": "s3.example.com", "secure": true, "region": "", "bucket": "name", "prefix": "ovpmon/"}`, `secrets: {"access_key": "...", "secret_key": "..."}`.
- `sftp`: `params: {"host": "files.example.com", "port": 22, "username": "backup", "directory": "ovpmon/daily"}`, `secrets: {"password": "..."}` **or** `{"private_key": "<key text>", "key_passphrase": "..."}` (not both). `params` also carries `host_key` and `host_key_type` once a key is trusted; they are set only by `trust-host-key` (ignored in the body, kept on `PUT` while `host` and `port` stay the same, cleared when one of them changes).
- `ftp`: `params: {"host": "ftp.example.com", "port": 21, "username": "backup", "directory": "ovpmon/daily", "tls": true, "verify_cert": true, "passive": true}`, `secrets: {"password": "..."}`.
- `directory` (SFTP, FTP): relative, letters, digits, `.`, `_`, `-`, no `..`, at most 4 levels.
- **Response**: the destination with `secrets` masked (`"********"` for each secret that is set), `last_status`, `last_message`, `last_at`. On `PUT`, a secret that is missing, empty or `"********"` keeps the stored value.
- **Test response**: `{"ok": true, "message": "..."}` (`ok: false` carries the reason). For an SFTP destination whose host key is not trusted (or changed): `{"ok": false, "message": "...", "needs_trust": true, "fingerprint": "SHA256:...", "key_type": "ssh-ed25519", "key_changed": false}`.
### `POST /backup/destinations/{id}/trust-host-key`
- **Body**: `{"fingerprint": "SHA256:..."}`: the one shown by Test. The server connects again (no credentials are sent) and pins the key only if it shows exactly that fingerprint; otherwise `400` ("run Test again"). Allowed for `sftp` destinations only (`400` otherwise); `409` while a job runs; `502` when the server cannot be reached.
- **Response**: the updated destination (`params.host_key` set).
- Until a key is trusted, SFTP backup, listing, restore and delete are refused (`502`, "The host key of the server is not trusted yet"); on a mismatch the connection is refused.
### `GET /backup/destinations/{id}/packages`
- **Response**: `{"packages": [{"name": "ovpmon-backup_vpn-main_20261007T101500Z.ovpbak.enc", "server_name": "vpn-main", "created_at": "2026-10-07T10:15:00Z", "size": 123456, "encrypted": true}]}`, newest first (time, then the `-2`, `-3` suffix). `created_at` is UTC and comes from the file name. `502` when the source cannot be read.
### `POST /backup/run`
- **Body (optional)**: `{"destination_ids": [1, 2]}`; without it all enabled destinations are used.
- **Response `400`**: nothing selected, a disabled destination, or a remote destination without a passphrase.
### `POST /backup/restore`
- **Body**: `{"destination_id": 1, "package": "<name from the listing>", "passphrase": "..."}`. The name is checked against the pattern and must be in the destination's listing; paths are never accepted. The passphrase is required for `.enc` packages.
- **Response `409`**: a job or an initialization of the PKI is running.
### `GET /backup/status`
- **Response**: `{"kind": "backup|restore|null", "state": "idle|running|done|partial|error", "stages": [{"name": "upload", "state": "pending|running|done|error"}], "results": [{"destination_id": 1, "name": "...", "state": "pending|running|ok|error|skipped", "message": "..."}], "warnings": [], "message": "...", "relogin": false, "started_at": "...", "finished_at": null}`.
- Backup stages: `package`, `encrypt` (only with a passphrase), `upload`. Restore stages: `download`, `decrypt`, `verify`, `prepare`, `safety_backup`, `stop_openvpn`, `replace`, `finalize`. `partial`: stored in some destinations only. `relogin` is true after a successful restore. The state is kept in memory (single Profiler process); the history is kept in `ovpmon_backup.db`.