Operations Backup and Restore - osama1998H/Moca GitHub Wiki

Backup & Restore

Per-tenant PostgreSQL backup/restore, gzip compression, optional encrypted backup files, and scheduled backups.

Overview

Moca provides per-site backup and restore using pg_dump/psql. Backups can be gzip-compressed and, when requested, encrypted before they are written to disk.

CLI Commands

moca backup create                    # Backup current site
moca backup create --site mysite      # Backup specific site
moca backup create --encrypt          # Create an encrypted backup (.enc)
moca backup list                      # List available backups
moca backup restore <file>            # Restore from backup
moca backup verify <file>             # Verify backup integrity
moca backup schedule --cron "0 2 * * *"

Backup Location

Backups are stored in sites/{site}/backups/ with timestamped filenames:

sites/mysite/backups/
├── 20250408_120000_mysite.sql.gz
├── 20250407_120000_mysite.sql.gz.enc
└── ...

What's Backed Up

  • All tables in the site's PostgreSQL schema
  • Document data, MetaType definitions, permissions, settings
  • Optional gzip compression
  • Optional backup-file encryption

Encrypted Backups

Use moca backup create --encrypt to wrap the backup file in an encrypted .enc envelope. The CLI expects a 64-character hex key and resolves it in this order:

  1. --encryption-key
  2. backup.encryption_key in moca.yaml
  3. MOCA_ENCRYPTION_KEY

Examples:

moca backup create --site mysite --encrypt
moca backup create --site mysite --encrypt --encryption-key 0123...cdef
moca backup restore sites/mysite/backups/20250407_120000_mysite.sql.gz.enc

Implementation details that matter operationally:

  • encrypted backups add a trailing .enc extension
  • restore auto-detects encrypted backups from the .enc suffix
  • the decrypted payload is then decompressed normally if the inner file ends in .gz
  • a wrong key fails restore before psql is invoked

Sensitive Field Encryption

When MOCA_ENCRYPTION_KEY is set for the server process, Password fields are encrypted at rest and transparently decrypted on document reads. This is separate from backup-file encryption: field encryption protects database values, while moca backup create --encrypt protects exported backup artifacts.

Verification Notes

moca backup verify validates file existence, checksums, and gzip / SQL structure for plain .sql and .sql.gz backups. Encrypted .enc files are restored directly and their integrity is enforced during decryption, but the current verifier does not decrypt .enc backups as part of verify.

Scheduled Backups

Configure automatic backups via moca backup schedule or cron.

Related

⚠️ **GitHub.com Fallback** ⚠️