Skip to content

Backups & archive

Kinglet has two separate data-retention features that both use an S3-compatible bucket:

  • Snapshots are compressed full-database copies for disaster recovery. They can be scheduled, downloaded, and restored from the admin UI.
  • Log archive exports expiring events as compressed NDJSON parts before retention deletes them. It is not a backup and cannot restore a database.

Both are optional and admin-only. Snapshots and restore require a local SQLite database; remote libSQL/Turso deployments should use the provider’s point-in-time recovery for database backups. Log archive works with either storage backend.

Backups use one private S3-compatible bucket (Cloudflare R2, AWS S3, Backblaze B2, MinIO, or similar). Set these four required values in the server environment:

KINGLET_BACKUP_S3_BUCKET=kinglet-backups
KINGLET_BACKUP_S3_ENDPOINT=https://<account-id>.r2.cloudflarestorage.com
KINGLET_BACKUP_S3_ACCESS_KEY_ID=<access-key-id>
KINGLET_BACKUP_S3_SECRET_ACCESS_KEY=<secret-access-key>

Everything else is optional: region, prefix, path style, encryption, part size, and restore caps. Every variable and default is in the S3 backup reference.

KINGLET_BACKUP_MANIFEST_SECRET defaults to the S3 secret access key. Set a dedicated value if you rotate the S3 credential, because older manifests are signed with the key that was active when they were written. Leave the ALLOW_UNSIGNED_MANIFESTS and ALLOW_UNENCRYPTED_SNAPSHOTS flags unset; they exist only for legacy snapshots.

The bucket is the source of truth: the UI list is a cache that is reconciled from the bucket. Use a private bucket and a dedicated credential scoped to it.

R2 is the primary target. Create an R2 API token with Object Read & Write on the one bucket and use it with the endpoint format shown above. The defaults (KINGLET_BACKUP_S3_REGION=auto, path style false) are right for R2.

Until the four required values are present, the Backups tab shows a Connect a bucket setup card instead of status, scheduling, snapshots, and archive controls. If only some are set, or a value is invalid, Kinglet logs one warning at boot naming the variables (never their values) and leaves backups disabled. On remote libSQL/Turso, the Connect a bucket card includes a remote-storage notice until S3 is configured; once configured, the Log archive section is available while snapshot operations return 409.

Two independent layers are available:

  • Server-side encryption is controlled by KINGLET_BACKUP_S3_SSE:
    • AES256 (default) sends SSE-S3 headers.
    • aws:kms requires KINGLET_BACKUP_S3_KMS_KEY_ID.
    • none sends no encryption headers.
  • Client-side encryption is enabled by setting KINGLET_BACKUP_PASSPHRASE. Kinglet age-encrypts the compressed snapshot before upload; the object and download use the .db.zst.age suffix. Restore and archive import need the same passphrase.

The two layers can be combined. With a passphrase configured, restore refuses a snapshot that claims encryption=none unless KINGLET_BACKUP_ALLOW_UNENCRYPTED_SNAPSHOTS=1 is set for a legacy snapshot.

Manifests are signed with HMAC-SHA256 using KINGLET_BACKUP_MANIFEST_SECRET (falling back to the S3 secret access key). Restore rejects a missing or invalid signature unless KINGLET_BACKUP_ALLOW_UNSIGNED_MANIFESTS=1 is set, and rejects a stored object or decoded database larger than KINGLET_BACKUP_MAX_SNAPSHOT_MB (default 8192) before downloading it.

Open Settings → Backups. Scheduled snapshots are off by default; manual runs remain available when the schedule is off.

  • Choose a preset or enter a custom 5-field cron expression (minute hour day month weekday) or macro (@hourly, @daily, @weekly, @monthly). Seconds fields, ?, and expressions that restrict both day-of-month and day-of-week are rejected.
  • Pick a timezone, or click use my timezone. The initial value comes from KINGLET_TIMEZONE, then the server timezone.
  • Runs closer than 15 minutes apart are rejected.
  • The schedule preview shows a human description and the next three run times.
  • On boot, if the last successful run predates the previous scheduled slot, Kinglet runs one catch-up snapshot. It does not replay every missed slot.
  • Failed scheduled or catch-up snapshots automatically retry after 5 min, 15 min, and 60 min.

Presets:

Preset Cron
Hourly 0 * * * *
Every 6 hours 0 */6 * * *
Daily at 03:00 0 3 * * *
Weekly, Monday 03:00 0 3 * * 1
Monthly, 1st 03:00 0 3 1 * *

Retention keeps the last N successful snapshots, where N is 2–365 (default 7). The newest snapshot is always kept, even when you lower N. The panel estimates coverage from the gap between the next two runs, for example 7 snapshots × 6h ≈ 42h, and estimates storage from the current database size times N; real snapshots are smaller because they are compressed.

A run checks that the data directory has at least databaseSize + max(32 MiB, 0.5 × databaseSize) of free space, copies the database with VACUUM INTO, compresses it with zstd, optionally age-encrypts it, and streams a multipart upload using KINGLET_BACKUP_PART_SIZE_MB (default 8 MiB, minimum 5). It then verifies the uploaded object with HEAD, comparing its size and SHA-256 metadata before writing the manifest. The manifest is written last; a snapshot without one is not listed. Orphan snapshot objects without a manifest are cleaned up after 24 hours.

Bucket layout:

<prefix>/manifests/<snapshot-id>.json
<prefix>/snapshots/<snapshot-id>.db.zst
<prefix>/snapshots/<snapshot-id>.db.zst.age

The Snapshots table shows the trigger, size, contents, encryption, and state. Refresh snapshots from bucket reconciles the cache; the server also does so when a list request finds the cache older than 60 seconds.

  • Download requests a presigned URL that expires after 5 minutes. The download is recorded on the snapshot. Encrypted snapshots download as .age.
  • Delete removes the manifest, the object, and the cache row. Kinglet refuses to delete the last ready snapshot (409); the API accepts ?force=1 from a user with restore permission. Retention deletions are silent and do not send notifications.
  • Progress appears in the panel over the live system-event stream: snapshot, compress, encrypt, upload, verify, and retention phases, with byte progress during compress, encrypt, and upload. A failed run shows its error in the status card.

Restore is staged, not immediate. Select a snapshot and choose Restore (staged); the confirmation dialog calls it Stage restore.

  1. Kinglet verifies the manifest signature, checks the stored object against the manifest’s expected size, and confirms the data directory has at least objectBytes + databaseBytes + max(64 MiB, 0.25 × databaseBytes) of free space. The object and database sizes in the manifest must not exceed KINGLET_BACKUP_MAX_SNAPSHOT_MB.
  2. It downloads the object, checks the download size and SHA-256, then decrypts (if needed) and decompresses it.
  3. The staged database must pass PRAGMA integrity_check, and its schema must not be newer than the running build.
  4. The verified database is written under <data-dir>/restore/, a pending.json marker is written, and the server enters maintenance mode. Requests under /v1 other than GET/HEAD/OPTIONS return 503 with Retry-After: 30 until the restart.

The restore is applied at the next boot, before the database opens. Kinglet copies the current database to <data-dir>/pre-restore-<timestamp>.db, removes stale WAL/SHM files, and swaps in the staged file. If the swap fails, it keeps or restores the pre-restore copy. The three newest pre-restore copies are retained; older ones are pruned because they contain the full database.

After the swap, it clears all sessions and verification rows. Session clearing is fail-closed: if the delete fails, Kinglet refuses to serve the restored database and retries on the next boot. API keys are intentionally preserved. The pending marker records the requesting administrator; the CLI records no requester.

A checkout or image also ships a restore CLI for fresh hosts and recovery when the UI is unavailable. It stages the snapshot exactly like the admin route; the next boot applies it.

Terminal window
# from a Kinglet checkout, after pnpm --filter @kinglet/api build
node apps/api/dist/restore/cli.js --latest
node apps/api/dist/restore/cli.js --id <snapshot-id>
node apps/api/dist/restore/cli.js --key snapshots/<snapshot-id>.db.zst
# or via pnpm:
pnpm --filter @kinglet/api restore -- --latest

Use --key snapshots/<snapshot-id>.db.zst.age (or the full object key under your configured prefix) for an encrypted snapshot. The CLI reads the same KINGLET_BACKUP_S3_*, KINGLET_BACKUP_PASSPHRASE, and KINGLET_DB_PATH variables as the server; outside Docker, set KINGLET_DB_PATH to the same absolute path the server uses. In Docker:

Terminal window
docker compose run --rm --entrypoint node kinglet \
apps/api/dist/restore/cli.js --latest
docker compose restart kinglet

Use docker compose up -d kinglet instead of restart if the container was already stopped. Restarting is what applies the staged snapshot; running the CLI alone only stages it.

Replacing the live database with an older snapshot also restores the users and API keys that existed at snapshot time. Sessions are cleared on boot, but a key revoked after the snapshot becomes valid again — review the project’s keys after restoring an old copy.

Log archive is not a backup. It writes whole events as re-importable NDJSON, not a database. Archived parts are not searchable until you import them.

Enable it in Settings → Backups → Log archive: turn on the global archive switch, then opt in each project. Both are off by default. Archive requires the same S3 configuration as snapshots.

When the global switch is on and a project is enabled:

  • The hourly retention job skips that project, so archive owns deletion.
  • The archive job selects events older than KINGLET_RETENTION_DAYS, oldest first by internal id, up to 10,000 events per part, grouped by UTC date.
  • Each part is written as NDJSON, zstd-compressed, optionally age-encrypted, uploaded to a deterministic key, verified with HEAD (size and SHA-256), and recorded in archive_parts. The exported rows are deleted only after the upload verifies.
  • A run stops after a 5-minute budget; a large backlog drains across runs. The job has its own daily cron (default 0 3 * * * in KINGLET_TIMEZONE, or the server timezone) and can also be triggered with POST /v1/archive/run. The current Backups panel does not expose the archive schedule or a run-now button.

If any step fails, the rows are kept and the project stays at its current size. An admin can opt into deleting the failed batch with deleteOnFailure through PUT /v1/archive/settings; it is off by default and is not exposed in the Backups panel.

<prefix>/archive/events/project=<project-id>/date=<YYYY-MM-DD>/
part-<first-id>-<last-id>-<count>-<hash>.ndjson.zst[.age]

Project deletion and an all-time clear (DELETE /v1/events?project=<id> with no range and no q) delete that project’s archive objects as well as its live events. A range or query clear leaves archive parts alone because deleting whole date parts would remove events outside the selection. Other expired archive objects are not deleted by the app; use S3 lifecycle rules on the archive prefix to expire them.

Import is an admin API operation (backups.restore). It downloads each part through a short-lived presigned URL, decrypts and decompresses it, verifies the uncompressed NDJSON checksum, and inserts events in chunks:

  • Idempotent: re-imports skip duplicate event uids.
  • Safety limits: 10 s download timeout and 1 GiB decompressed cap per part. Events with a message > 8,000 chars, fields > 32,768 bytes, service > 128 chars, env > 64 chars, uid > 128 chars, or a timestamp > 5 minutes in the future are skipped.
  • Encryption: encrypted parts require KINGLET_BACKUP_PASSPHRASE. While a passphrase is set, unencrypted parts are refused (set KINGLET_BACKUP_ALLOW_UNENCRYPTED_SNAPSHOTS=1 only to import older parts written before you added the passphrase).
  • Safe with clears: a clear or project delete that overlaps an archive run waits for the in-flight part and drops it so removed events do not reappear.
Terminal window
curl -X POST https://logs.example.com/v1/archive/import \
-H "Content-Type: application/json" \
-H "X-Kinglet-Request: 1" \
-H "Cookie: <session-cookie>" \
-d '{"projectId":"<project-id>","from":"2026-11-01","to":"2026-11-20"}'

from and to are optional inclusive YYYY-MM-DD UTC bounds. The response reports imported and skipped counts. The command imports into an existing project; create the project first if you are recovering onto a fresh instance.

There is also a CLI for a checkout or container. It runs the same import against the configured database and reads the same KINGLET_BACKUP_S3_*, KINGLET_BACKUP_PASSPHRASE, and KINGLET_DB_PATH variables. Outside Docker, set KINGLET_DB_PATH to the absolute path the server uses: the two commands below run from different directories, so a relative default would open different databases.

Terminal window
pnpm --filter @kinglet/api build
node apps/api/dist/archive/cli.js --project <project-id> --from 2026-11-01 --to 2026-11-20
# or:
pnpm --filter @kinglet/api archive:import \
--project <project-id> --from 2026-11-01 --to 2026-11-20

Run it with the API stopped. It writes events and updates FTS and usage through the normal triggers, so a concurrent API process would be writing the same database. In Docker:

Terminal window
docker compose stop kinglet
docker compose run --rm --entrypoint node kinglet \
apps/api/dist/archive/cli.js --project <project-id> --from 2026-11-01 --to 2026-11-20
docker compose up -d kinglet

Litestream continuously replicates the SQLite database to object storage. It is an external tool: Kinglet does not configure, monitor, or restore Litestream replicas, and the Backups panel does not show their status. Use it when you need a much smaller recovery point objective than scheduled snapshots, or keep both for independent copies.

services:
kinglet:
image: ghcr.io/rushlightlab/kinglet:0.5.0
restart: unless-stopped
environment:
KINGLET_DB_PATH: /data/kinglet.db
KINGLET_BASE_URL: https://logs.example.com
volumes:
- kinglet-data:/data
litestream:
image: litestream/litestream
restart: unless-stopped
volumes:
- kinglet-data:/data
command: replicate /data/kinglet.db s3://<bucket>/kinglet
environment:
LITESTREAM_ACCESS_KEY_ID: ${LITESTREAM_ACCESS_KEY_ID}
LITESTREAM_SECRET_ACCESS_KEY: ${LITESTREAM_SECRET_ACCESS_KEY}
# Add the endpoint and region settings your provider needs.
volumes:
kinglet-data:

Restore with Litestream while Kinglet is stopped:

Terminal window
docker compose stop kinglet
docker compose run --rm litestream restore -o /data/kinglet.db s3://<bucket>/kinglet
docker compose up -d kinglet

Check Litestream’s own documentation for the current image tag and the endpoint, region, and credential settings for your provider.

  • S3 credentials and KINGLET_BACKUP_PASSPHRASE are read from the environment only. They are never returned by the API, written to the database, stored in manifests, or included in notifications.
  • Use a private bucket. Do not enable public bucket access; downloads use presigned URLs that expire after 5 minutes.
  • The S3 endpoint must use HTTPS; http:// is accepted only for loopback.
  • Give the credential the smallest scope that works: one bucket and prefix, with put, get, head, list, and delete permissions. Use separate credentials per instance.
  • Snapshots contain the full database, including users, sessions, API keys, and event contents. Archive parts contain raw events. Treat both as sensitive data; enable age encryption when the bucket is not fully trusted.
  • Backups, restore, and archive routes are admin-only. MCP keys and project API keys cannot reach them.
  • Restore clears sessions and verification rows fail-closed and keeps the three newest pre-restore copies. Restoring an older snapshot brings back the users and API keys that existed then; review project keys afterward.

Use Test bucket connection on the Backups status card. It writes, reads, and deletes a small probe object under <prefix>/_health/, so it checks put, get, and delete access. Listing snapshots and refreshing the cache also need list permission. Check the endpoint URL, bucket name, region, access key, secret, path-style setting, and TLS certificate.

The server has no complete KINGLET_BACKUP_S3_* configuration. Set all four required values and restart; the boot log names any missing variables. 409 also appears for snapshots on remote libSQL/Turso, which need the provider’s point-in-time recovery instead. Log archive still works there.

The process was not restarted. The admin restore stops Kinglet after staging so a supervisor can restart it. It exits with code 0, so on-failure does not restart it. Start the container, or use restart: unless-stopped. The staged database and pending.json remain under <data-dir>/restore/.

The snapshot is age-encrypted and KINGLET_BACKUP_PASSPHRASE is missing or different from the one used to create it. Kinglet cannot recover or bypass the passphrase. Set the original value and stage the restore again.

Archive keeps rows when an upload or verification fails, so the project can remain at KINGLET_MAX_EVENTS_PER_PROJECT and ingest keeps returning 429. Fix the S3 error, then run archive again with POST /v1/archive/run or wait for the next scheduled run, and check Settings → Backups → Log archive for the last error. Only events older than KINGLET_RETENTION_DAYS are eligible; events newer than the cutoff are not. If you need room immediately, raise the cap or shorten retention. Enabling deleteOnFailure can delete the failed batch, including rows that were never exported, and should be a deliberate last resort.