Backup and restore technical design
Technical/historical reference. This design or procedure was recorded in the source repository and may describe earlier implementation states. For current deployment and optional-backup behavior use the installation guide and update guide.
Backup and disaster recovery design
Section titled “Backup and disaster recovery design”New in-container application-profile backup (dev): The host-side full image/Compose snapshot and restore described below are retained for legacy archive compatibility. The new Backup Rules create encrypted PostgreSQL/signature/identity-provider archives inside the application, no Docker socket or host operator required. These archives can be recovered safely to a NEW staging directory via
backup_cli.py restore-staging. A full automated running-instance restore of the new profile has not yet been implemented or cross-host validated. Read BACKUP_RULE_MANAGEMENT.md before migration.
Status: Versioned archive primitives, age encryption, Docker Compose full-backup creation (including saved container images), encrypted archive verification, fresh-deployment restore, verified retention, guarded scheduled remote-filesystem backups and selective administrator component exports are implemented. Cross-host restore testing, destructive restore and validation against a real NFS/SMB target remain outstanding. A dedicated two-host operator drill is documented in CROSS_HOST_RESTORE_DRILL.md; it is a procedure, not evidence that the drill has passed. Scope: Standalone OpenSigWeave deployment. This design does not configure or modify Mailcow.
1. Goals and guarantees
Section titled “1. Goals and guarantees”OpenSigWeave needs two backup entry points with different scopes:
- Administrator UI: selectable, component-level exports for operational use.
- Console: one complete, portable disaster-recovery backup. Partial console backups are intentionally not supported.
A complete backup must be restorable on a different Linux host with Docker Compose, without depending on the original Docker volumes or host paths. Restore must preserve signature content and history, domain configuration, identity-provider metadata and credentials, application configuration, and all database state required by the application.
The implementation must never describe a backup as successful until the archive has been finalized, encrypted, and verified.
2. Data inventory
Section titled “2. Data inventory”| Component | Source of truth | Full backup | Selective UI export |
|---|---|---|---|
| PostgreSQL records/migrations, including rules, schedules and branding | PostgreSQL | Yes | Only supported logical components listed below |
| Current signatures and immutable versions | opensigweave-signatures volume |
Yes | Yes, when Signatures is selected |
| Identity-provider secret files | opensigweave-idp-data volume |
Yes | No; only non-secret provider metadata is exportable |
| Application environment and independent secrets | Host .env |
Yes, encrypted | No |
| Compose definition and deployment metadata | Host compose.yaml and manifest |
Yes | No |
| Application image identity/version | Saved image + backup manifest | Yes | No |
Both PostgreSQL and the signature volume are mandatory for a usable full restore. The IDP volume and host environment file are mandatory to restore managed authentication and the configured application secrets.
Do not include Docker socket contents, host SSH keys, unrelated volumes, logs containing credentials, or arbitrary host directories.
3. Archive format
Section titled “3. Archive format”Use a versioned archive format. A full backup contains:
opensigweave-backup-<UTC timestamp>/├── manifest.json├── database/│ └── opensigweave.dump├── data/│ ├── signatures/ # complete signature volume contents│ └── idp-config/ # complete identity-provider volume contents└── deployment/ ├── compose.yaml └── env.encThe final distributable artifact is encrypted with age. The encryption recipient is configured separately from the application secrets; the corresponding private identity must be stored offline or in a separate secrets vault. Never put the age private identity in the backup or the same server’s .env.
The manifest records at least:
- Backup format version and OpenSigWeave version/image digest.
- UTC creation time, database engine/version and the exact Alembic revision.
- Included component names and byte sizes.
- SHA-256 checksums for every payload file.
- Migration revision at backup time.
- Whether the environment file is present and encrypted.
Checksums are verified after decryption and before any restore action. A checksum is an integrity check, not a substitute for authenticated encryption.
4. Consistency and creation flow
Section titled “4. Consistency and creation flow”The full console operation is all-or-nothing:
- Validate Docker Compose project, required services, backup destination, encryption recipient, and free disk space.
- Create a private temporary staging directory with restrictive permissions.
- Stop the application service to prevent signature/IDP writes while the snapshot is taken. Keep PostgreSQL running.
- Export PostgreSQL using a PostgreSQL-native custom-format dump.
- Archive the signature and IDP volumes from the stopped application container’s mounted volumes.
- Copy the Compose definition and encrypt the host .env as a separate payload.
- Write the manifest and SHA-256 checksums.
- Build the archive, encrypt it with age, then decrypt to a temporary verification stream and validate the archive/checksums.
- Atomically publish the encrypted artifact only after verification.
- Restart the application service and remove temporary plaintext material, including on failure.
If any step fails, the command returns a non-zero exit code, does not publish a partial backup, removes temporary plaintext files where possible, and attempts to restart the application service. It must clearly report if restart recovery also fails.
A backup must not silently proceed without the IDP volume or encryption. A deliberate UI export may omit components, but its manifest must state exactly what is absent and it must not be labelled a disaster-recovery backup.
5. Restore flow and safety
Section titled “5. Restore flow and safety”Restore is a destructive administrative operation and must be a separate, explicit command:
python backup_cli.py create --project-dir /opt/opensigweave --output /backup/opensigweave-<timestamp>.age --recipient <age-recipient> --identity /media/offline/age-identity.txtpython backup_cli.py verify /backup/opensigweave-<timestamp>.age --identity /media/offline/age-identity.txtpython backup_cli.py decrypt /backup/opensigweave-<timestamp>.age --identity /media/offline/age-identity.txt --output /tmp/opensigweave.tar.gz
# Lower-level packaging of an already prepared staging directory:python backup_cli.py package --staging <prepared-directory> --output <backup.age> --recipient <age-recipient>The create command validates the Compose project, stops the application service, exports PostgreSQL, copies the signature and IDP volumes from the stopped application container, encrypts the host .env as a separate payload, creates the archive, and decrypt-verifies the encrypted result. The age identity is required for that final verification and should be supplied from removable/off-host storage. The lower-level package command remains available for an already prepared staging directory.
The initial restore implementation is intentionally limited to a fresh deployment:
- Decrypt and verify the complete archive before creating target files or Docker volumes.
- Reject unsupported format versions, missing mandatory components, path traversal entries, symlinks, unexpected file types, oversized payloads and checksum mismatches.
- Require a new target directory and a Compose project name that has no existing containers or expected named volumes.
- Restore the saved application and PostgreSQL images, encrypted host configuration, PostgreSQL custom dump, signature volume and IDP volume into newly created resources.
- Restore file ownership and restrictive permissions for application data.
- Compare the restored database’s Alembic revision with the revision recorded in the backup manifest.
- Export each restored data volume through a read-only helper container and compare its complete SHA-256 file inventory with the archive manifest.
- Start the application and wait for its healthcheck. A failed restore leaves the new target resources in place for operator inspection; it never removes pre-existing resources.
This is not an in-place replacement workflow. Existing deployment directories, Compose projects and expected volume names are refused. A cross-host restore drill is still required before relying on this as the only disaster-recovery path. Follow CROSS_HOST_RESTORE_DRILL.md and retain its evidence; same-host CI is not a substitute.
A failed restore must not automatically delete the safety snapshot. Recovery instructions must be printed and logged without exposing secret values.
6. Selective administrator component export
Section titled “6. Selective administrator component export”The web UI provides a bounded, versioned ZIP export for portable non-secret configuration, not disaster recovery. The implemented selectable components are exactly:
- signatures and immutable HTML versions;
- domains;
- signature groups and memberships;
- rules and conditions;
- identity-provider metadata only.
The export deliberately excludes provider credential files, application secrets, personal user overrides and identity caches. Rule conditions can contain operator-entered identifiers such as email addresses, so an archive must still be reviewed before sharing.
Schedules and branding are currently not part of the selective component export. They are PostgreSQL state and are therefore preserved by the full encrypted disaster-recovery backup.
Exports are capped at 20 MiB, use checksummed HTML assets and a versioned manifest, return with no-store/private cache headers, and require administrator authorization plus CSRF protection for state-changing import operations. Imports are validated and additive: existing records are not silently overwritten.
7. Key handling and retention
Section titled “7. Key handling and retention”- Encryption is mandatory for full backups. The implemented selective UI export never contains provider credentials or application secrets.
- The age recipient is public and may be configured on the host; the private identity must be kept separately.
- Losing the private identity makes encrypted backups unrecoverable. Warn during setup and document key escrow.
- Never log passwords, tokens, decrypted .env contents, or private identities.
- Temporary plaintext files and directories use restrictive permissions and are removed on completion/failure.
- Retention is configurable by count/age at the storage destination; automatic pruning must never delete the only known-good backup.
- Backups should be copied off-host. A backup stored only on the same Docker host does not protect against host loss.
8. Verification and acceptance tests
Section titled “8. Verification and acceptance tests”The feature is not complete until automated tests cover:
- Full backup includes database (including schedules/branding), signatures, IDP credentials, encrypted environment and deployment metadata.
- Docker Compose preflight rejects missing services, stopped containers and missing signature/IDP volume mounts.
- The application service is restarted after successful and failed snapshot attempts, and a restart failure is reported distinctly.
- Missing volume, invalid recipient, insufficient disk space, failed dump, failed archive, failed encryption and failed verification all fail closed.
- Application service is restarted after both successful and failed backup attempts.
- Archive traversal, symlinks, unexpected file types, unsupported versions and tampered checksums are rejected before restore.
- Restore to a fresh Compose project on a different host reproduces database records, signature versions and hashes, IDP provider files and application configuration.
- Restore compares the recorded and restored Alembic revision and independently validates the signature and IDP volume SHA-256 inventories.
- Existing deployment directories, Compose projects and expected named volumes are refused without modification.
- Selective administrator ZIP export uses component manifests, HTML asset checksums, size limits, administrator authorization, no-store responses and explicit secret/PII exclusions. A partial export must never be labelled a full backup.
- No secret values appear in logs, API responses, manifests, or test failure output.
A documented restore drill against a clean, disposable host is a release requirement. Merely testing that an archive can be created is insufficient.
9. Delivery order
Section titled “9. Delivery order”- Execute and record the two-host restore drill, including source/destination isolation and full data-integrity checks.
- Validate scheduled backups against a real NFS/SMB target, including disconnect and remount behavior.
- Add automated cross-host restore coverage when CI infrastructure permits.
- Consider destructive in-place restore only after fresh-target and cross-host recovery are proven.
The backup implementation is automated, but it must not be treated as the only recovery path until the real-host acceptance work is complete.
10. Verified retention CLI
Section titled “10. Verified retention CLI”The console supports a retention preview and an explicit deletion mode:
python backup_cli.py prune --directory /backup/opensigweave --identity /media/offline/age-identity.txt --keep 7python backup_cli.py prune --directory /backup/opensigweave --identity /media/offline/age-identity.txt --keep 7 --applyThe command considers only direct regular .age files in the selected directory. It sorts them newest first, decrypts and verifies every candidate using the supplied age identity, and retains the newest requested number. Any invalid archive aborts the operation before deletion. The default is a dry run; deletion requires –apply. Symlinks and files changed during inspection are refused.
Retention is not, by itself, off-host protection. The repository now includes a systemd service/timer that writes to a dedicated mounted remote filesystem and applies verified retention. The mount must be configured by the operator; the script refuses to write when the target is not mounted or its filesystem type is not allowlisted. Replication to object storage is not implemented.
11. Scheduled off-host backup
Section titled “11. Scheduled off-host backup”A systemd service and daily timer are provided for deployments that mount remote storage (for example NFS or SMB) at /mnt/opensigweave-backups. The script refuses to run unless that exact directory is a mount point on an approved remote filesystem type (NFS, SMB/CIFS or supported FUSE remote mounts), preventing a disconnected share or a local disk from silently being treated as off-host protection. It creates a full encrypted backup, then applies verified retention.
Deployment outline:
- Mount a dedicated remote backup share at /mnt/opensigweave-backups using the host’s normal mount configuration.
- Install the repository at /opt/opensigweave and ensure Docker Compose, Python, age, mountpoint and flock are available.
- Create /etc/opensigweave/backup.env from scripts/backup.env.example and set the actual age recipient, project path, remote mount and retention count.
- Store the matching age identity at /etc/opensigweave/age-identity.txt with owner root and mode 0600. Keep a separate offline recovery copy of that identity.
- Install the service and timer from scripts/systemd/, reload systemd, enable the timer, and inspect the first manual service run before relying on the schedule.
The service runs with root privileges because it needs the Docker socket. Its systemd unit uses a read-only system view except for the backup mount and lock directory. Review and adapt the fixed installation paths if the deployment differs. The age identity must remain outside the backup share; compromise of the scheduled host can still expose it, so this is not a substitute for offline key custody.
The Docker end-to-end test now also exercises age-verified retention in both dry-run and deletion modes. A separate CI smoke test stubs mount detection and command execution to verify that an unmounted destination and a mounted local filesystem both fail closed before invoking the backup CLI, while an allowlisted NFS filesystem proceeds through backup creation and verified retention. It also verifies that a symlinked lock file is rejected without changing the link target. The systemd unit allocates a private /run/opensigweave runtime directory (mode 0750) for the lock file. The scheduled automation has not yet been exercised against a real NFS/SMB server or a second physical host. CI validates shell syntax and the underlying backup/restore/retention CLI, not remote-storage availability, network interruption recovery or the host’s systemd configuration. The full backup, fresh-restore and retention integration coverage is part of the CI workflow. Current branch-specific CI evidence is maintained in implementation progress so this design document does not become stale. This remains same-host CI evidence; a real two-host drill and NFS/SMB host test are still outstanding.
