seafile-backup
Online backup of a Seafile CE stack (Seafile 12+) to restic.
Seafile keeps running for the whole backup:
- the database dump uses
--single-transaction, so it is a consistent InnoDB snapshot taken while seahub is serving; - the block store (
seafile-data/storage) is content-addressed and immutable once written, so restic can read it while Seafile writes new blocks.
A cold mode (COLD_BACKUP=true) stops seahub/seafile around the run for
pre-upgrade snapshots. It is off by default and needs /var/run/docker.sock.
What a run does
- Takes an exclusive lock (a second run exits immediately).
- Pings the Uptime Kuma push monitor with
?status=upso the monitor stays green while the run is in progress. - Dumps
ccnet_db,seafile_dbandseahub_dbinto one consistent--databasesdump at the stable path/config/db/current/seafile-databases.sql, reading the credentials from the Seafileconf/.env. A rotated copy is hardlinked into/config/db/archive/<timestamp>/. - Clears any stale restic lock left behind by a run that died (see
Stale locks), then runs
restic backupover the Seafile tree and that dump directory in a single snapshot, taggedseafile, hostcygnus. - Repeats the backup into
LOCAL_RESTIC_REPOSITORYwhen it is set. - Applies the retention policy, prunes weekly and runs a
--read-data-subsetcheck weekly — clearing stale locks again first, becauseforget,pruneandcheckwant an exclusive lock. - Deletes archived dumps older than
DB_DUMP_KEEP. - 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
record nothing, which is why the query string is the only form used here.
Environment
| Variable | Default | Meaning |
|---|---|---|
SEAFILE_ROOT |
/shared/seafile |
Seafile data root inside the container |
SEAFILE_CONF_ENV |
/shared/seafile/conf/.env |
source of the DB credentials |
DB_HOST / DB_USER / DB_PASSWORD |
from conf/.env |
override the DB target |
DB_SSL |
false |
pass TLS to mariadb-dump (the bundled server has none) |
DATABASES |
ccnet_db,seafile_db,seahub_db |
databases to dump |
DB_DUMP_DIR |
/config/db |
dump root: current/ is backed up, archive/ is rotated |
DB_DUMP_KEEP |
3 |
archived dumps to keep |
LOG_PATH |
/config/seafile-backup.log |
log file (rotated at 10 MB) |
RESTIC_REPOSITORY |
required | primary restic repository |
RESTIC_PASSWORD |
required | primary repository password |
LOCAL_RESTIC_REPOSITORY |
unset | optional second (local) repository |
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 |
CHECK_READ_DATA_SUBSET |
5% |
how much data restic check re-reads |
PRUNE_MAX_UNUSED |
5% |
restic prune --max-unused |
KUMA_PUSH_URL |
unset | Uptime Kuma push URL |
APPRISE_URL / APPRISE_URLS |
unset | Apprise endpoint and targets |
COLD_BACKUP |
false |
stop Seafile around the run |
SEAFILE_CONTAINER |
seafile |
container to stop in cold mode |
FAIL_ON_PARTIAL |
false |
treat restic exit 3 as a failure |
CRON |
0 3 * * * |
crond schedule (standard 5-field) |
LOCK_STALE_AFTER, LOCK_GRACE and RETRY_LOCK are read when the job starts,
so changing them needs a container recreate (a Komodo deploy), not just a
restart of the job.
AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY are read by restic itself for the
s3: backend.
Why the dump path is stable
restic only reuses a parent snapshot when the host and the path set match the
previous run exactly. Writing each dump to its own <DB_DUMP_DIR>/<stamp>/
directory changed the path set every night, so restic found no parent and
re-hashed the whole 337 GiB block store — about 17 minutes per repository, for
nothing. The dump now lives at /config/db/current/, which is the only dump
path in the snapshot; the rotated copies in archive/ are outside it.
The dump is written to seafile-databases.sql.part and renamed into place only
after the Dump completed trailer is verified, so a failed dump never replaces
the last good one.
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.
Reading the locks has to happen with --no-lock: restic cat and
restic snapshots take a lock of their own, so an exclusive lock left by a
dead run would block the very code that is trying to clear it. restic list locks does not lock, which is why the listing works either way.
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.
Verified against a real orphan: restic check killed with SIGKILL mid-run
leaves an exclusive lock, and the next cleanup removes it (orphaned, age 13s,
unlock --remove-all, successfully removed 1 locks). With the default
LOCK_GRACE the same lock is reported live and left alone until it is ten
minutes old.
Restore
restic restore latest --target /restore
# then, against an empty MariaDB 10.11:
mariadb -h <host> -u root -p < /restore/config/db/current/seafile-databases.sql
The dump is written with --add-drop-table, so it can also be replayed over an
existing database.
Scheduling
The container runs busybox crond in the foreground (the same scheduler
qbit-maid uses). entrypoint.sh writes /tmp/crontabs/root from CRON and
execs crond -f -c /tmp/crontabs; the rootfs is read-only, so the crontab
lives on the tmpfs. MAILTO= is set empty so job output stays on crond's own
stdout/stderr, i.e. the container log.
Two consequences of busybox crond:
- it only loads crontab files owned by uid 0, so the container must run as
root (
user: '0:0'— which the Seafile block permissions require anyway); - before each job it calls
initgroups(), so the container needsCAP_SETGIDon top ofCAP_DAC_OVERRIDE. Without it the job is forked and then dies withcrond: can't set groups: Operation not permitted.
There is no @reboot job: a container restart does not start a backup.
Build
docker build -t git.jonb.io/jblu/seafile-backup:latest .