feat: detect and remove stale restic locks
ci / docker (push) Successful in 17s

A container recreated mid-backup leaves a restic lock behind, and restic
will not call it stale for 30 minutes, so the next run fails or blocks.

Before each repository leg (and again before retention, which needs an
exclusive lock) list the locks, read each with 'restic cat lock' and
classify it: stale past LOCK_STALE_AFTER, orphaned when this container
took it and its pid is gone, live otherwise. Remove with
'unlock --remove-all' only when nothing live is left, otherwise fall back
to plain 'unlock' so a running backup is never disturbed. Removed ids are
re-checked with a second list and reported in the notification body.

Every restic call now carries --retry-lock (RETRY_LOCK, default 30m) as a
backstop, so a lock that survives cleanup makes the run wait instead of
failing.
This commit is contained in:
trex Bot committed 2026-11-24 23:26:24 -06:00
1 parent cbf0cbd963
commit 1d8f49980c
2 files changed
+204 -9

No files matched your search

+45 -4
View File
@@ -20,13 +20,17 @@ pre-upgrade snapshots. It is off by default and needs `/var/run/docker.sock`.
3. Dumps `ccnet_db`, `seafile_db` and `seahub_db` into one consistent
`--databases` dump under `/config/db/<timestamp>/`, reading the credentials
from the Seafile `conf/.env`.
4. Runs `restic backup` over the Seafile tree **and** that dump directory in a
single snapshot, tagged `seafile`, host `cygnus`.
4. Clears any stale restic lock left behind by a run that died (see
[Stale locks](#stale-locks)), then runs `restic backup` over the Seafile
tree **and** that dump directory in a single snapshot, tagged `seafile`,
host `cygnus`.
5. Repeats the backup into `LOCAL_RESTIC_REPOSITORY` when it is set.
6. Applies the retention policy, prunes weekly and runs a `--read-data-subset`
check weekly.
check weekly — clearing stale locks again first, because `forget`, `prune`
and `check` want an exclusive lock.
7. Deletes dump directories older than `DB_DUMP_KEEP`.
8. Pings Kuma with the result and sends an Apprise notification.
8. Pings Kuma with the result and sends an Apprise notification. The body
names any lock that was removed.
Any failure exits non-zero, pushes `?status=down` to Kuma and sends a failure
notification. Kuma has no `/start` or `/fail` path — those forms answer 200 but
@@ -50,6 +54,9 @@ record nothing, which is why the query string is the only form used here.
| `LOCAL_RESTIC_PASSWORD` | `RESTIC_PASSWORD` | password for the local repository |
| `RESTIC_CACHE_DIR` | `/config/restic-cache` | restic cache root |
| `RESTIC_HOST` / `RESTIC_TAG` | `cygnus` / `seafile` | snapshot labels |
| `LOCK_STALE_AFTER` | `1800` | seconds before a lock counts as stale (restic's own window) |
| `LOCK_GRACE` | `600` | minimum age before a lock with a dead owner is removed |
| `RETRY_LOCK` | `30m` | `--retry-lock` passed to every restic call; empty disables it |
| `KEEP_DAILY` / `KEEP_WEEKLY` / `KEEP_MONTHLY` / `KEEP_YEARLY` | `7` / `4` / `12` / `3` | retention |
| `PRUNE_WEEKDAY` | `0` (Monday) | weekday to run `restic prune` |
| `CHECK_WEEKDAY` | `6` (Sunday) | weekday to run `restic check` |
@@ -65,6 +72,40 @@ record nothing, which is why the query string is the only form used here.
`AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` are read by restic itself for the
`s3:` backend.
## Stale locks
restic takes a lock in the repository for the duration of a run. If the
container is recreated mid-backup — a Komodo deploy, a `docker restart`, an
OOM kill — the process dies without releasing it, and restic will not treat
that lock as stale for **30 minutes**. The next run then either fails with
`repository is already locked` or blocks.
Before each repository leg the script lists the locks, reads each one with
`restic cat lock` and classifies it:
| Verdict | Condition | Action |
|---|---|---|
| `stale` | older than `LOCK_STALE_AFTER` | removed |
| `orphaned` | taken by this container, older than `LOCK_GRACE`, pid gone | removed |
| `live` | anything else | left alone |
| `unknown` | unparseable timestamp | removed |
If nothing live is left, the removal is `restic unlock --remove-all`; if a live
lock is still there it falls back to plain `restic unlock`, which only drops the
ones past restic's own 30 minute window. Every lock is logged with its age,
host, pid and verdict, and the ids that were removed are re-checked with a
second `list locks` before being reported.
The `orphaned` check compares the lock's `hostname` with the container's own, so
the compose file pins `hostname: seafile-backup` — without it the hostname is
the container id and changes on every recreate, which is exactly the case this
is meant to catch. A lock whose pid has been reused by a live process is
treated as live, so the failure mode is a lock that waits for the 30 minute
window rather than one that is removed while still in use.
`RETRY_LOCK` is the backstop: every restic call carries `--retry-lock`, so a
lock that survives the cleanup makes the run wait instead of failing.
## Restore
```sh