Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
.git
.github
.claude
.jj
ref
vendor
.vscode
build
**/build
Expand All @@ -17,7 +21,16 @@ install-util.sh
install.sh
LICENSE
README.md
docker-compose.yml.example
docker-compose.example.yml
docker-compose.yml
Makefile
.project
.classpath
bin/
test/
.settings/
.note.xnote
.env
.env.*
**/.env
**/.env.*
59 changes: 59 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
# CHANGELOG

Everything in this file is part of version 0.1.0, the first release. Entries are grouped by the date they were made.

## 11-24-26
- Archives are ordered, listed and pruned by the UTC timestamp in their file name (`yyyyMMdd-HHmmss-SSS-media-backup.zip`) instead of the file's last-modified time, so copying or restoring files no longer changes which archive is the newest or gets deleted by retention; `createdAt` in `GET /api/v1/backups` comes from the same timestamp (millisecond precision), and an archive whose name has no valid timestamp still uses its last-modified time
- If the database is found corrupt while the server is running, it is moved aside and replaced as before, and now setup also reopens without a restart: a new Setup PIN is logged, `/config` works again, paired Clients must pair again (they get `401` until then), open event streams are closed and unconfirmed pairings are dropped, and the backup location, retention and schedule go back to the defaults until set again
- The API docs list the `409` answers for `POST /backups/run` (a run is already in progress, or no services are configured), for `PUT /config` and the service endpoints while a backup is running, and for a service name that already exists
- The start page says the `/config` setup page is only available until setup is finished
- `.dockerignore` skips `bin/`, `.classpath` and `test/`

## 11-22-26
- Backup run scratch space is now `.mediagarrd-runs/` inside the backup folder instead of `runs/`, and startup cleanup only removes folders in it and `*-media-backup.zip.tmp` files; a `runs/` folder or other `.zip.tmp` files you keep in the backup folder are left alone
- A finished archive is flushed to disk before it is moved into place and the backup folder is flushed after the move, both before retention deletes any older archive, so a power loss cannot leave a truncated newest archive next to an already deleted good one
- The image has a health check against `/api/v1/health` on `SERVER_PORT` (default `8080`) and `.dockerignore` skips `.claude`, `.jj`, `ref`, `vendor` and the compose files
- Restore docs use `sudo` for `mgrecover` and `bsdtar`, because archives belong to root
- The backup interval must be between 15 minutes and 365 days: `PUT /api/v1/schedule` answers `400` for a missing, zero, negative, shorter or longer value and saves nothing; a stored or `MEDIAGARRD_BACKUP_INTERVAL` value outside that range is ignored with a warning (the default is `PT12H`); the setup page limits the hours field to 0.25 to 8760
- The pair-before-setup `409` now says `This server has not finished setup yet. Finish it on the server's /config page, then pair.`
- Setup page: the Setup PIN error is red and Finish Setup sits under Configured Services
- `POST /api/v1/setup/complete` answers `200` with `{"pairingPin": "NNNNNN"}` instead of an empty `204` (`pairingPin` is `null` when no PIN applies), and the setup page shows the Pairing PIN with a copy button after Finish Setup; it is still printed in the logs
- Pairing is confirmed by the Client's first request with its token: `POST /pair` creates a pending pairing in memory, and only the first authenticated request with that token stores the client and closes PIN pairing or spends the pairing code. A Client whose reply was lost can pair again with the same PIN or code
- A pending pairing expires after 5 minutes, at most 10 are kept, a restart drops them, and they are not listed or counted as paired clients; if several pair with one PIN or code, the first to confirm wins and the others get `401`
- A pairing code that confirms the first stored client also closes PIN pairing; `POST /setup/complete` answers `410` for a second concurrent finish instead of rotating the PIN, and the setup page disables Finish while it is in flight
- Pairing PIN is checked without resetting the wrong-attempt counter until pairing closes
- Older Clients keep working: they store the token without confirming, and their first request with it (within 5 minutes) confirms the pairing. A new Client confirms against any Server straight away
- No database change

## 11-21-26
- Added an optional failure webhook: when a backup run fails or only partly succeeds (scheduled or manual), the server posts a message to a URL you set, so a failed scheduled backup (which only retries after the next interval) is noticed. Set it on the setup page, with `PUT /api/v1/notifications/webhook`, or from the Client; `GET` reports only `configured` and `scheme://host`, and `POST /api/v1/notifications/webhook/test` sends a test (one every 10 seconds, `429` with `Retry-After` otherwise)
- The message is a Discord body (with `allowed_mentions` blocking pings), a Slack body, or a generic JSON body with `event`, `time`, `error`, `failedServices`, `archiveId`, `content` and `text`, chosen by host; it is sent in the background with timeouts, no redirects and no retry, and a failed delivery is only a warning that names the host
- The webhook URL is never returned, logged or echoed in an error, and is kept in plain text in the settings database, so it is also inside backup archives
- Fixed scheduled backups never running on a server restarted more often than its backup interval: the next backup is now due one interval after the last completed or partial backup, and an overdue one runs within about 30 seconds of start
- A server with no completed backup runs its first one soon after setup is finished; while setup is still open, and for installs that never finished setup and have no backup yet, it waits one interval from start
- Changing the backup interval counts from the last completed or partial backup instead of from the change
- Fixed the setup page being wider than a phone screen: grid columns, form rows and notes shrink to the screen and the webhook buttons wrap

## 11-19-26
- Every error answers `{"error": "<message>"}`, including `404`, `405` and the missing-backup and delete cases that used Spring's default body; a `5xx` says `Internal error`, except a failed `POST /backups/run`, which keeps the run's failure reason in the `500` body for the Client dashboard
- Request bodies over 64 KiB answer `413`, a service may have at most 32 paths of 4096 characters, and a PIN, code or backup location has a length limit
- Responses carry `X-Content-Type-Options: nosniff`, `Referrer-Policy: no-referrer`, a `default-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'` Content-Security-Policy and `Cache-Control: no-store` (not on the setup page's css and js)
- The setup cookie is `Secure` over HTTPS or `X-Forwarded-Proto: https`, and a setup session expires after 30 minutes without use
- Backup folders and the data folder the server creates are `0700`, archives and the database file `0600`; restore with `sudo`
- Retention deletes only archives the server has a record of; others are kept and listed
- Unpairing the last Client reopens pairing and logs a fresh `Pairing PIN`; opening or closing pairing no longer clears a lockout
- Before setup is finished the `401` message says so
- Fixed a helper container left behind when a stale one was listed after the matching one, and archive ids that dropped every `.zip` in a name
- The setup page keeps the workspace hidden when unlocking fails, shows the lockout wait, labels its path fields and table, shows a visible focus outline and scrolls its table on narrow screens
- Log values from requests have line breaks removed
- After setup, every `/api/v1` endpoint except `GET /health` (reduced), `GET /version`, `GET /setup`, `POST /setup/session` and `POST /pair` needs `Authorization: Bearer <token>`, and anything not on that list is refused by default, including `/events` and backup downloads
- A client gets its token with `POST /api/v1/pair` and the PIN printed in the logs as `Pairing PIN`; the token is returned once, only its SHA-256 hash is stored (new `paired_clients` table), and it is never logged
- When setup is finished the PIN is replaced with a fresh `Pairing PIN`, and it stops working as soon as one client pairs; an install with no paired client (including every install upgraded from an earlier version) prints a `Pairing PIN` on start
- `MEDIAGARRD_OPEN_PAIRING=true` opens pairing with a new PIN on start even when clients are paired, and logs a warning on every start while it is set
- A paired client can create a single-use pairing code valid for 10 minutes (`POST /api/v1/pairing-codes`), list paired clients (`GET /api/v1/clients`), and unpair any client including itself (`DELETE /api/v1/clients/{id}`), which also closes that client's event streams
- Five wrong PINs lock the setup PIN and the pairing PIN together for 10 minutes (`429` with `Retry-After`), and ten wrong pairing codes lock codes separately; the setup PIN had no time lock before
- `GET /api/v1/health` returns only `healthy` and `backupInterval` without a token, the full status with one, and `401` for a token that is not valid
- Setup page endpoints accept the setup session only until setup is finished, and the check no longer depends on the raw URL, so path variants such as `/api/v1/services;x` no longer bypass it
- A request body that is missing or not JSON is answered with `400` and `{"error": "..."}`
- Every response carries `X-Frame-Options: DENY` and `Content-Security-Policy: frame-ancestors 'none'`, so the setup page cannot be embedded in another page
- An unexpected failure (HTTP 500) answers with `{"error": "Internal error"}` and keeps the details in the log; a backup run where every service failed still reports its reason
- New `docs/https.md` for running the Server behind nginx or Caddy

## 11-12-26
- A run rejected before it starts (such as no services configured) publishes no run events
- A scheduled check with nothing to back up now waits one full interval instead of retrying every 30 seconds, and adding or editing a service makes the schedule due again right away
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ EXPOSE 8080

ENV JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=50"

HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \
CMD curl -fsS -o /dev/null http://127.0.0.1:${SERVER_PORT:-8080}/api/v1/health

ENTRYPOINT ["java", \
"-XX:+UseG1GC", \
"-Xms16m", \
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Full documentation, including configuration, archives and restoring, is on the [
- [settings.md](./docs/settings.md): every environment variable and its default.
- [API.md](./docs/API.md): every HTTP endpoint and what it returns.
- [database.md](./docs/database.md): recovering the server's database from a backup.
- [https.md](./docs/https.md): serving the API over HTTPS behind nginx or Caddy.

## Requirements
- A Linux host running Docker with Compose v2
Expand All @@ -26,13 +27,21 @@ docker logs mediagarrd-server 2>&1 | grep "Setup PIN"
```
Open `http://<server-ip>:38471/config`, enter the PIN, add the services to back up, and finish setup.

Then pair the [MediaGarrd Client](https://github.com/MediaGarrd/Client). When you press Finish Setup, the page shows a `Pairing PIN` once, with a copy button, and the logs show it too:
```bash
docker logs mediagarrd-server 2>&1 | grep "Pairing PIN"
```
Enter it in the Client when it asks. The Client receives a token, uses it once to confirm the pairing and sends it with every request, and the PIN stops working once that is done. To pair another Client, create a pairing code from a paired one, or restart the server with `MEDIAGARRD_OPEN_PAIRING=true` to print a new PIN (remove it afterwards). If you are upgrading from a version without pairing, the logs show a `Pairing PIN` on the first start.

## Updating
```bash
git pull
docker compose up --build -d
```

> **Security:** the server mounts `/var/run/docker.sock`, which gives it root-equivalent access to the host, and its API is unauthenticated after setup. Do not expose its port to untrusted networks.
> **Security:** the server mounts `/var/run/docker.sock`, which gives it root-equivalent access to the host. After setup its API needs a token from pairing, but the PIN and token travel in plain text over HTTP, so put it behind HTTPS ([docs/https.md](./docs/https.md)) before exposing it beyond a trusted network.
>
> A paired Client is treated as the owner of the host: it can choose which host paths are backed up and where archives are written. Only pair Clients you trust. Backup folders and archives created by the server are readable by their owner only (the server runs as root), so restore with `sudo`. See [docs/settings.md](./docs/settings.md#security-notes).

## License
[MIT](./LICENSE)
13 changes: 5 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
plugins {
java
id("org.springframework.boot") version "3.3.5"
id("io.spring.dependency-management") version "1.1.6"
id("org.springframework.boot") version "4.1.3"
id("io.spring.dependency-management") version "1.1.7"
}

version = "0.1.0"

extra["httpclient5.version"] = "5.4.4"
extra["httpcore5.version"] = "5.3.4"

repositories {
mavenCentral()
}

dependencies {
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.xerial:sqlite-jdbc:3.44.0.0")
testImplementation("org.springframework.boot:spring-boot-starter-webmvc-test")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-webmvc")
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.xerial:sqlite-jdbc:3.44.0.0")
implementation("org.xerial:sqlite-jdbc:3.54.1.0")
implementation("com.github.docker-java:docker-java-core:3.5.1")
implementation("com.github.docker-java:docker-java-transport-httpclient5:3.5.1")
implementation("org.apache.commons:commons-compress:1.27.1")
Expand Down
Loading
Loading