The problem
The backup documentation says: "Stalwart offers no online backup of [RocksDB]". The options are stopping the server or taking an atomic filesystem snapshot.
Many single-node installs (RocksDB is the default backend) have no snapshot-capable filesystem: plain XFS or ext4 without LVM. That leaves stopping the mail server every day, or running something like LVM thin or ZFS just for backups.
RocksDB already has the right tool: the Checkpoint API (`rocksdb::checkpoint::Checkpoint`, in the crate Stalwart already uses). It creates a consistent, point-in-time copy of an open database while it keeps running:
- it flushes the memtables;
- it hard-links the immutable SST and blob files;
- it copies only the small mutable files (MANIFEST, OPTIONS, WAL tail).
On the same filesystem this takes milliseconds and almost no extra space. Since the default blob store also lives in RocksDB, one checkpoint holds everything: messages, settings, accounts, keys and the queue.
How I’m doing it today (as a local patch)
I run Stalwart 1.0.1 built from source with a small local patch, and it works well:
- Store method: `RocksDbStore::create_checkpoint(dir)`, a thin wrapper around `Checkpoint::create_checkpoint`, run on the store’s blocking worker.
- Trigger: a background task, enabled only when `STALWART_CHECKPOINT_DIR` is set. It reads only the environment, so it can’t be changed through the API.
- a daily time (`STALWART_CHECKPOINT_TIME`, `HH:MM` UTC);
- `SIGUSR1` creates one immediately.
- Atomic publishing each checkpoint is built in `/.tmp`, fsynced, then swapped with `/current` using `renameat2(RENAME_EXCHANGE)`. The previous one is then removed. A backup tool never sees a partial checkpoint.
- Coordination with the backup tool: the backup holds a shared `flock` on `/.lock` while reading `current`. The swap takes the lock exclusively and waits, so `current` is never replaced mid-backup.
- Permissions: the service runs with umask 027, so the checkpoint is group-readable. The backup (restic) runs as its own unprivileged user in Stalwart’s group: no root, no access to the live data directory, and Stalwart doesn’t need to stop.
- Monitoring: a log line with the result and duration (`checkpoint created in 30 ms`).
Restores are verified: a restored checkpoint opens with `stalwart --export` and all the data is there.
What I’d suggest for a native version
The same idea, properly integrated:
1. Settings on the RocksDB store, for example:
- `checkpoint.path`;
- a schedule, using the existing scheduling format;
- optionally `checkpoint.keep`, the number of checkpoints to keep.
2. On-demand trigger: a store maintenance task (like `reindexAccounts`), available from the web admin, the API and `stalwart-cli`.
3. The same publishing guarantees: build in a temporary directory, swap atomically, keep a fixed `current` path, and provide a lock file (or another documented rule) so external tools know when they can read safely.
4. Telemetry events for success and failure (with the duration), so that failures can raise alerts.
5. Documentation: “back up `/current` with any tool (restic, borg, rsync…); there is no need to stop the server”.
Caveats worth documenting
- The checkpoint must be on the same filesystem as the database. Otherwise RocksDB copies every file instead of hard-linking it.
- Disk usage grows as compaction replaces SST files that a checkpoint still links to. Keeping only one or two recent checkpoints keeps this small.
- It only covers RocksDB. The SQL and FoundationDB backends already have their own online backup tools.
This would make “back up a running Stalwart” a supported, documented feature for the default setup, independent of the filesystem.