Skip to content

Database Backup

Mantis stores all deployment configuration, audit logs, tenant data, and operational state in PostgreSQL. Regular database backups are essential for disaster recovery and operational continuity.

pg_dump creates a logical backup that can be restored to any compatible PostgreSQL version. This is the recommended method for most deployments.

Terminal window
# Full database dump (custom format -- compressed, supports parallel restore)
pg_dump -Fc -h localhost -p 5432 -U mantis -d mantis \
-f /opt/mantis/backups/db/mantis-$(date +%Y%m%d-%H%M%S).dump
# SQL format (human-readable, larger file)
pg_dump -h localhost -p 5432 -U mantis -d mantis \
-f /opt/mantis/backups/db/mantis-$(date +%Y%m%d-%H%M%S).sql
# Schema only (for documentation or migration planning)
pg_dump --schema-only -h localhost -p 5432 -U mantis -d mantis \
-f /opt/mantis/backups/db/mantis-schema-$(date +%Y%m%d).sql

For large databases, physical backups are faster and support point-in-time recovery (PITR):

Terminal window
pg_basebackup -h localhost -p 5432 -U mantis \
-D /opt/mantis/backups/db/base-$(date +%Y%m%d) \
-Ft -z -P

Physical backups require restoring to the same PostgreSQL major version.

/opt/mantis/scripts/backup-database.sh
#!/bin/bash
set -euo pipefail
DB_HOST="${MANTIS_DB_HOST:-localhost}"
DB_PORT="${MANTIS_DB_PORT:-5432}"
DB_USER="${MANTIS_DB_USER:-mantis}"
DB_NAME="${MANTIS_DB_NAME:-mantis}"
BACKUP_DIR="/opt/mantis/backups/db"
RETENTION_DAYS=30
DATE=$(date +%Y%m%d-%H%M%S)
BACKUP_FILE="$BACKUP_DIR/mantis-$DATE.dump"
mkdir -p "$BACKUP_DIR"
# Create backup
echo "Starting database backup..."
pg_dump -Fc -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" \
-f "$BACKUP_FILE"
# Verify backup is valid
pg_restore -l "$BACKUP_FILE" > /dev/null 2>&1
BACKUP_SIZE=$(du -h "$BACKUP_FILE" | cut -f1)
echo "Backup completed: $BACKUP_FILE ($BACKUP_SIZE)"
# Clean old backups
DELETED=$(find "$BACKUP_DIR" -name "mantis-*.dump" -mtime +$RETENTION_DAYS -delete -print | wc -l)
if [ "$DELETED" -gt 0 ]; then
echo "Cleaned $DELETED old backups (older than $RETENTION_DAYS days)"
fi
Terminal window
# Daily backup at 2:00 AM
0 2 * * * /opt/mantis/scripts/backup-database.sh >> /var/log/mantis/db-backup.log 2>&1
# Hourly backup for high-value environments
0 * * * * /opt/mantis/scripts/backup-database.sh >> /var/log/mantis/db-backup.log 2>&1

Avoid interactive password prompts by using a .pgpass file:

Terminal window
# Create .pgpass for the mantis user
echo "localhost:5432:mantis:mantis:your-password" >> ~/.pgpass
chmod 600 ~/.pgpass
Terminal window
# Drop and recreate the database (destructive!)
dropdb -h localhost -p 5432 -U postgres mantis
createdb -h localhost -p 5432 -U postgres -O mantis mantis
# Restore from custom format
pg_restore -h localhost -p 5432 -U mantis -d mantis \
/opt/mantis/backups/db/mantis-20260115-020000.dump
# Restore with parallel jobs (faster for large databases)
pg_restore -h localhost -p 5432 -U mantis -d mantis \
-j 4 /opt/mantis/backups/db/mantis-20260115-020000.dump
Terminal window
psql -h localhost -p 5432 -U mantis -d mantis \
-f /opt/mantis/backups/db/mantis-20260115-020000.sql

Restore specific tables if you only need certain data:

Terminal window
# List tables in backup
pg_restore -l /opt/mantis/backups/db/mantis-20260115-020000.dump | grep TABLE
# Restore a single table
pg_restore -h localhost -p 5432 -U mantis -d mantis \
-t audit_log_entries /opt/mantis/backups/db/mantis-20260115-020000.dump

Database backups contain encrypted data (credentials, secrets, sensitive variables). The data remains encrypted in the backup because Mantis uses application-level encryption (AES-256-GCM).

To restore encrypted data, you need:

  1. The database backup
  2. The encryption master key (MANTIS_ENCRYPTION_KEY)

Without the master key, encrypted fields cannot be decrypted. See Encryption at Rest for details.

Periodically verify backups can actually be restored:

Terminal window
# Create a temporary database for verification
createdb -h localhost -p 5432 -U postgres mantis_verify
# Restore the backup
pg_restore -h localhost -p 5432 -U mantis -d mantis_verify \
/opt/mantis/backups/db/mantis-latest.dump
# Run a basic verification query
psql -h localhost -p 5432 -U mantis -d mantis_verify \
-c "SELECT count(*) FROM tenants; SELECT count(*) FROM deployment_history;"
# Clean up
dropdb -h localhost -p 5432 -U postgres mantis_verify
FeatureConfiguration
Automated backupsEnable with retention period (1-35 days)
Manual snapshotsCreate via AWS Console or CLI
Point-in-time recoveryRestore to any second within retention window
Cross-region copyCopy snapshots to another region for DR
FeatureConfiguration
Automated backupsEnabled by default (7-35 day retention)
Geo-redundant backupEnable for cross-region protection
Point-in-time restoreRestore within retention period
FeatureConfiguration
Automated backupsConfigure backup window and retention
On-demand backupsCreate via Console or gcloud CLI
Point-in-time recoveryEnable binary logging