Skip to content

Add periodic S3 recovery backups - #10

Merged
msdmazvoid merged 6 commits into
mainfrom
feature/s3-backup
Nov 9, 2026
Merged

msdmazvoid merged 6 commits into
mainfrom
feature/s3-backup

Conversation

@msdmazvoid

@msdmazvoid msdmazvoid commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds periodic S3 persistence of the existing recovery set, with per-file client-side encryption, retention cleanup, a restore command, and a separate backup worker.

Process separation

  • kouja-agent no longer owns a backup goroutine, S3 client, backup ticker, or backup-only config validation.
  • kouja-backup is a one-shot worker run by kouja-backup.timer.
  • It snapshots SQLite through a read-only VACUUM INTO connection, so the serving agent is not opened or migrated by the backup process.
  • The revocation journal is snapshotted directly from its file.
  • systemd units and installer wiring are included.

S3 layout

Recovery files are uploaded individually rather than as a single opaque archive:

  • state.db — consistent SQLite snapshot
  • secret.key, host-id, revoked-serials.log
  • active config.yaml, emergency.log
  • TLS server certificate/key and client CA
  • certificate-authority signing key
  • issued certificate/key directory
  • restore.json manifest with per-file SHA-256 checksums

With per_backup_folder: true (default):

kouja/2026Nov091648/state.db
kouja/2026Nov091648/config.yaml
kouja/2026Nov091648/restore.json

With per_backup_folder: false, files use timestamped names directly under the prefix.

Behavior

  • Runs every 15 minutes by default via the packaged systemd timer; the timer is kept in sync with state.backup_interval_seconds.
  • Uses the standard AWS credential chain.
  • Supports S3-compatible endpoints and path-style addressing.
  • Supports SSE-S3 (AES256) and SSE-KMS (aws:kms).
  • When encryption_key_file is set, every object is encrypted client-side with AES-256-GCM and uploaded with a .enc suffix.
  • retention_days (default 7) deletes old backup objects under the configured prefix after each successful upload.
  • The manifest is uploaded last, so incomplete uploads are not treated as restorable backups.
  • kouja-restore accepts an S3 prefix, a local plain-file directory, or a legacy archive. It verifies checksums, decrypts, and restores files to the paths in the backup's config.

Verification

  • make test
  • make lint
  • make coverage-check (80.4%, floor 80%)
  • Fake S3 tests cover upload, list/download, retention deletion, per-folder and flat layouts, and client-side encryption.
  • Restore tests cover encrypted local directory, flat directory, S3 prefix, and legacy archive paths.

A live S3 upload was not run because no bucket/credentials were available in this environment.

Config

state:
  backup_interval_seconds: 900
  s3_backup:
    enabled: true
    bucket: kouja-backups
    prefix: kouja/
    region: eu-central-1
    endpoint: ""
    force_path_style: false
    server_side_encryption: AES256
    kms_key_id: ""
    per_backup_folder: true
    retention_days: 7
    encryption_key_file: /etc/kouja/backup.key

@msdmazvoid

Copy link
Copy Markdown
Contributor Author

Updated S3 layout: state.s3_backup.per_backup_folder (default true).\n\n- enabled: prefix/2026Nov091648/kouja-...tar.gz\n- disabled: prefix/kouja-...tar.gz\n\nBoth modes are covered by tests; CI is green.

voidstarit added 4 commits November 9, 2026 15:23
Upload recovery files individually instead of a single archive, encrypt each object with a configured backup key, delete objects older than retention_days, and add kouja-restore for S3 prefixes, local plain backups, and legacy archives.
Add error-path and helper coverage for backup, restore, PKI, agent options, and admin UI error mapping; raise the internal coverage floor to 80%.
Run periodic local/S3 backups from a systemd timer instead of inside kouja-agent. The worker snapshots SQLite through a read-only connection, snapshots the revocation journal as a file, and leaves the serving agent with no backup loop. Core config validation is separated so invalid backup-only settings do not block agent startup.
@msdmazvoid
msdmazvoid merged commit c6005c1 into main Nov 9, 2026
2 checks passed
@msdmazvoid
msdmazvoid deleted the feature/s3-backup branch November 9, 2026 23:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant