Backup and Restore
This guide covers what state to backup and how to restore it.
What to Backup
Section titled “What to Backup”Critical (Must Backup)
Section titled “Critical (Must Backup)”| Item | Location | Purpose |
|---|---|---|
| tenement.toml | Project root | Service configuration |
| Instance data | {data_dir}/{service}/{id}/ | Per-instance persistent data |
| Auth tokens | In SQLite DB | API authentication |
Optional (Nice to Have)
Section titled “Optional (Nice to Have)”| Item | Location | Purpose |
|---|---|---|
| SQLite database | {data_dir}/tenement.db | Instance metadata, logs |
| ACME certificates | {data_dir}/acme/ | TLS certificates (auto-renewed) |
Not Needed
Section titled “Not Needed”| Item | Why |
|---|---|
| Logs | Stored in memory, ship to external service |
| Metrics | Ephemeral, stored in external Prometheus |
| Sockets | Recreated on startup |
Backup Procedure
Section titled “Backup Procedure”Simple: Full Directory Backup
Section titled “Simple: Full Directory Backup”# Stop tenement (for consistency)systemctl stop tenement
# Backup everythingtar -czvf tenement-backup-$(date +%Y%m%d).tar.gz \ /etc/tenement/tenement.toml \ /var/lib/tenement/
# Restartsystemctl start tenementOnline: Hot Backup
Section titled “Online: Hot Backup”For minimal downtime, backup while running:
# Backup config (always safe)cp /etc/tenement/tenement.toml ~/backups/
# Backup SQLite with sqlite3 backup commandsqlite3 /var/lib/tenement/tenement.db ".backup ~/backups/tenement.db"
# Backup instance data (may be inconsistent if actively writing)rsync -av /var/lib/tenement/ ~/backups/tenement-data/Note: Instance data backup may be inconsistent if instances are actively writing. For consistency, stop specific instances before backing up their data.
Per-Instance Backup
Section titled “Per-Instance Backup”For multi-tenant scenarios, backup individual instances:
# Stop the instanceten stop api:customer123
# Backup its datatar -czvf customer123-$(date +%Y%m%d).tar.gz \ /var/lib/tenement/api/customer123/
# Restartten spawn api --id customer123Restore Procedure
Section titled “Restore Procedure”Full Restore
Section titled “Full Restore”# Stop tenementsystemctl stop tenement
# Restore from backuptar -xzvf tenement-backup-20240115.tar.gz -C /
# Start tenementsystemctl start tenement
# Verify instancesten psRestore Specific Instance
Section titled “Restore Specific Instance”# Stop instanceten stop api:customer123
# Restore datatar -xzvf customer123-20240115.tar.gz -C /
# Restartten spawn api --id customer123Restore to New Server
Section titled “Restore to New Server”# On new servercargo install tenement-cli
# Copy backupscp backup-server:~/backups/tenement-backup.tar.gz .
# Restoretar -xzvf tenement-backup.tar.gz -C /
# Install systemd serviceten install --domain example.com
# Verifyten psAutomated Backups
Section titled “Automated Backups”Cron Job
Section titled “Cron Job”#!/bin/bashBACKUP_DIR="/backups/tenement"DATE=$(date +%Y%m%d)
# Create daily backupsqlite3 /var/lib/tenement/tenement.db ".backup $BACKUP_DIR/tenement-$DATE.db"cp /etc/tenement/tenement.toml $BACKUP_DIR/tenement-$DATE.tomltar -czf $BACKUP_DIR/data-$DATE.tar.gz /var/lib/tenement/
# Keep last 7 daysfind $BACKUP_DIR -name "*.tar.gz" -mtime +7 -deletefind $BACKUP_DIR -name "*.db" -mtime +7 -deleteS3 Backup with Litestream
Section titled “S3 Backup with Litestream”For continuous SQLite replication:
# Install litestreamwget https://github.com/benbjohnson/litestream/releases/latest/download/litestream-linux-amd64.tar.gz
# Configure /etc/litestream.ymldbs: - path: /var/lib/tenement/tenement.db replicas: - url: s3://my-bucket/tenement
# Run as servicelitestream replicateDisaster Recovery
Section titled “Disaster Recovery”Complete Loss Recovery
Section titled “Complete Loss Recovery”- Provision new server
- Install tenement
- Restore config and data from backup
- Update DNS to point to new server
- Verify all instances running
Partial Recovery
Section titled “Partial Recovery”If only some data is lost:
# Identify affected instancesten ps
# Restore from backuptar -xzf backup.tar.gz -C / var/lib/tenement/api/customer123/
# Restart affected instancesten restart api:customer123Testing Backups
Section titled “Testing Backups”Regularly verify backups work:
# Create test environmentmkdir /tmp/tenement-test
# Restore to test locationtar -xzf backup.tar.gz -C /tmp/tenement-test
# Verify config parsescd /tmp/tenement-test/etc/tenementten config
# Verify database integritysqlite3 /tmp/tenement-test/var/lib/tenement/tenement.db "PRAGMA integrity_check"Next Steps
Section titled “Next Steps”- Upgrading - Version upgrades
- Monitoring Setup - Observability
- Troubleshooting - Common issues