Files
Xika (Agent) b8973db6ef
ai-news-writer-ci / test (pull_request) Failing after 27s
ops(ai-news-writer): backup and DR runbooks, article-index backup script, CI fixes
- Add BACKUP_STRATEGY, BACKUP_TESTING, DISASTER_RECOVERY runbooks and link them from README and docs/runbooks/README.
- Fix tools/backup-article-index.sh: JSONL line validation, correct staged-diff check before commit, current-branch push, optional Paperclip trailers, commit without hooks that inject forbidden lines.
- Ignore runtime article-index and local .backup/ in .gitignore.
- Circuit breaker: apply OPEN→HALF_OPEN timeout before recordSuccess/recordFailure so half-open probes work without a prior isAvailable call.
- Correlation IDs: generate 15-digit UTC timestamps to match the documented/tested format.
- ESLint: remove unused imports and unused test context parameters so npm run lint passes.

Paperclip-Issue: BIT-3508

Paperclip-Run: 4f18682c-8058-4c73-a14e-44988984e7e9
2026-04-17 20:48:04 +00:00

8.5 KiB
Raw Permalink Blame History

Backup and Disaster Recovery Strategy — ai-news-writer (BIT-4285)

This document defines the backup and disaster recovery (DR) strategy for the ai-news-writer service, including RTO/RPO targets, backup procedures, retention policies, and recovery testing schedules.

Infrastructure Context:

  • Deployment: LXC container (CT 223, Proxmox)
  • Critical Data: data/article-index.jsonl (JSONL article log, max 2 MB / 10K entries)
  • Runtime: Node.js 20 LTS via PM2 (ecosystem.config.cjs)
  • Config: Git-tracked sources.json, sites/*.json, ui-theme.json
  • Logs: Winston logs to logs/combined.log and logs/error.log

Criticality & RTO/RPO Targets

Service Criticality

Tier: High — Daily article publication; loss of service or recent articles affects business continuity.

RTO (Recovery Time Objective)

Target: 2 hours

Justification:

  • Article publication runs on daily schedule (configurable via AUTO_CRON)
  • Failure detected within 5 min (health check interval)
  • Manual recovery and container rebuild require ~60–90 min on Proxmox
  • 2-hour window allows recovery before next scheduled publication cycle
  • If failure occurs early morning: full recovery by mid-morning; otherwise recovered for next day's cycle

RPO (Recovery Point Objective)

Target: 1 day

Justification:

  • Article index stores publication history; loss is significant but not catastrophic
  • Recent articles can be re-fetched from RSS feeds if needed
  • Daily backup aligns with publication cycle; articles published yesterday are protected
  • Hourly snapshots (7-day retention) provide granular recovery for recent data incidents
  • Acceptable data loss: articles generated since last daily backup (max ~24 hours of unpublished work)

Backup Scope

What to Backup (Critical)

  1. Article Index: data/article-index.jsonl

    • Why: Single source of truth for publication history and editorial decisions (approved/rejected/pending)
    • Size: ~2 MB max (10K lines, auto-trimmed)
    • Frequency: Daily + hourly incremental
  2. Configuration Files: config/*.json, config/sites/*.json

    • Why: Source definitions, site targets, theme settings
    • Size: < 100 KB
    • Frequency: Daily (changes tracked in Git anyway)

What NOT to Backup (Non-Critical)

  • Logs: logs/combined.log, logs/error.log
    • Why: For troubleshooting only; not required for service recovery
    • Note: Ship to Loki/Promtail if long-term retention is needed
  • node_modules/: Re-installed from package-lock.json during recovery
  • .env secrets: Should be in a secrets manager; not committed to repo or backups
    • Recovery procedure restores from vault/manager; never embedded in backup

Exclusions in Backup

  • No compiled/generated files (rebuild from source)
  • No node_modules/
  • No .env (restore from secrets manager)
  • No logs/ directory
  • Keep backups small for fast transfer and recovery

Backup Strategy & Retention

Daily Backup (Production Snapshot)

  • Schedule: Once daily at 02:00 UTC (before early-morning publication cycle)
  • Scope: Article index + config files
  • Retention: 30 days of daily snapshots
  • Storage:
    • Primary: .backup/article-index.YYYY-MM-DD.jsonl in repo directory
    • Secondary (Offsite): Commit to Gitea as backup/article-index.YYYY-MM-DD.jsonl (Git commit + push)
    • Why Git: Leverages existing infrastructure; immutable, timestamped, searchable; no additional storage
  • Frequency: Automated via cron job (see Implementation below)

Hourly Incremental Snapshots (Development & Extended Recovery)

  • Schedule: Every hour (on the hour)
  • Scope: Article index only (config files change infrequently)
  • Retention: 7 days of hourly snapshots (168 snapshots)
  • Storage: .backup/article-index.YYYY-MM-DDTHH-00-00Z.jsonl (ISO 8601 with UTC timezone)
  • Purpose: Granular recovery for recent incidents without waiting for next daily backup
  • Cost: ~7 MB total (168 × 2 MB max); acceptable for 7-day retention

Configuration Backup (Git)

  • Strategy: All config/*.json files are Git-tracked (except .example.json)
  • Retention: Full Git history (immutable)
  • Recovery: git checkout <commit-hash> to restore any prior config version
  • Note: Ensure sensitive values (API keys, passwords) are never committed; use .env.example template instead

Backup Implementation

Manual Backup

Run the backup script anytime:

./tools/backup-article-index.sh

Output:

  • Creates .backup/article-index.YYYY-MM-DD.jsonl (daily)
  • Creates .backup/article-index.YYYY-MM-DDTHH-00-00Z.jsonl (hourly)
  • Commits to Git and pushes to Gitea
  • Logs: Backup created at .backup/article-index.YYYY-MM-DD.jsonl

Automated Backup (Production)

On the LXC container (CT 223), add to root crontab:

# Daily backup at 02:00 UTC
0 2 * * * cd /srv/ai-news-writer && ./tools/backup-article-index.sh >> /var/log/ai-news-writer-backup.log 2>&1

# Hourly backup at minute 0 (00:00, 01:00, etc.)
0 * * * * cd /srv/ai-news-writer && ./tools/backup-article-index.sh hourly >> /var/log/ai-news-writer-backup.log 2>&1

Backup Log Location: /var/log/ai-news-writer-backup.log

Verification (on LXC):

tail -20 /var/log/ai-news-writer-backup.log
ls -lah /srv/ai-news-writer/.backup/article-index.*

Storage Locations & Retention Policy

Backup Type Path Frequency Retention Storage
Daily Snapshot .backup/article-index.YYYY-MM-DD.jsonl Daily @ 02:00 UTC 30 days Repo + Gitea
Hourly Snapshot .backup/article-index.YYYY-MM-DDTHH-00-00Z.jsonl Hourly @ :00 7 days Repo (local)
Config History Git commits On change Full history Gitea

Cleanup (Automated in Backup Script)

The backup script automatically removes old snapshots:

  • Daily: keep only last 30 days (find ... -mtime +30 -delete)
  • Hourly: keep only last 7 days (find ... -mtime +7 -delete)

Gitea Retention

Committing backups to Gitea means:

  • Immutable history (can restore any prior snapshot)
  • Automatic cleanup: Gitea repo settings can enforce branch policies or archive old commits if needed
  • Recommendation: Set up Gitea Actions to prune old backup commits (future enhancement)

Recovery Point Scenarios

Scenario Time to Last Backup Data Loss
Incident same day < 1 hour (hourly snapshot) Articles since last hourly backup (max 1 hour)
Incident next morning < 24 hours (daily snapshot) Articles generated since daily backup (max 24 hours)
Incident after week 7 days (hourly snapshots roll off) Must use 1–30 day daily snapshots
Incident after month 30 days (last daily backup) Unrecoverable beyond 30 days; recommend manual mid-month backups for critical periods

For recovery procedures, see:


Scaling & Future Considerations

Current Constraints

  • Article Index Max Size: 2 MB (10K entries); auto-trims oldest
  • PM2 Memory Limit: 512 MB
  • LXC Storage: 20 GB (nvme-zfs)
  • Backup Overhead: ~30 MB disk per month (30 daily + 7 hourly snapshots × 2 MB each)

Capacity Planning

If article log approaches max size:

  1. Increase ARTICLE_LOG_MAX_LINES in env (default 10K)
  2. Increase ARTICLE_LOG_TRIM_AT_BYTES threshold (default 2 MB)
  3. Monitor disk usage: du -sh .backup/
  4. Review retention policy if backups grow beyond 1 GB total

Future Enhancements

  • Offsite Replication: Copy daily snapshots to S3 or second Gitea org (beyond 30 days)
  • Automated Recovery Testing: Monthly full-restore test in dev environment (BIT-4287)
  • Alerting: Integrate backup success/failure with Prometheus alerting (see docs/runbooks/OBSERVABILITY_BIT3601.md)
  • Database Migration: If JSONL → SQL database transition occurs, backup strategy must be re-evaluated

References

  • LXC Provisioning: tools/proxmox/create_ai_news_writer_lxc.sh
  • Deploy Script: tools/deploy-lxc.sh (includes health checks)
  • Article Storage Code: src/store/article-log.js
  • Health Endpoint: src/http/status-server.js
  • PM2 Config: ecosystem.config.cjs
  • Observability: docs/runbooks/OBSERVABILITY_BIT3601.md