Skip to content

Backup and Restore

A robust backup strategy is critical for disaster recovery and business continuity. This guide outlines the recommended procedures for backing up the Champa Intelligence application data.


What to Back Up

There are two primary components that contain persistent data and need to be backed up:

  1. System PostgreSQL Database: This is the most critical component. It stores all user accounts, roles, permissions, audit logs, and other application configurations.
  2. Redis Data (Optional but Recommended): Redis is primarily a cache, but it also stores active user sessions. Backing it up can provide a better user experience during a restore, as users may not be logged out.

What NOT to Back Up

  • The Champa Intelligence Application Container: The application itself is stateless. You should rely on your Docker image or source code for restoration.
  • The Camunda Database: This should be covered by your organization's primary database backup strategy for the Camunda engine. Champa Intelligence only has read-only access to it.

Backup Procedures

These procedures assume you are using the recommended docker-compose.server.yml setup.

1. Backing Up the System PostgreSQL Database

This should be performed regularly (e.g., daily).

# Define backup file name with a timestamp
BACKUP_FILE="champa_system_db_backup_$(date +%Y-%m-%d_%H-%M-%S).sql.gz"

# Execute pg_dump inside the container and compress the output
docker exec champa-system-db pg_dump -U champa_user -d champa_system | gzip > /path/to/your/backups/${BACKUP_FILE}

echo "Database backup created: ${BACKUP_FILE}"

# Optional: Prune old backups (e.g., keep the last 7 days)
find /path/to/your/backups -name "champa_system_db_backup_*.sql.gz" -mtime +7 -delete

Automate Backups

Add the backup command to a cron job to automate daily backups.

Example cron entry (runs daily at 2 AM):

0 2 * * * /path/to/backup-script.sh >> /var/log/champa-backup.log 2>&1

2. Backing Up Redis Data

Redis data can be backed up by copying its snapshot file (dump.rdb). This is less critical than the database but useful for session persistence.

# Ensure Redis persists its data to disk first
docker exec champa-redis redis-cli -a ${REDIS_PASSWORD} SAVE

# Define backup file name
REDIS_BACKUP_FILE="champa_redis_backup_$(date +%Y-%m-%d_%H-%M-%S).rdb"

# Copy the RDB file from the Docker volume
# First, find the volume path:
VOLUME_PATH=$(docker volume inspect champa-intelligence_redis | grep Mountpoint | awk '{print $2}' | sed 's/"//g' | sed 's/,//g')

# Now copy the file
cp ${VOLUME_PATH}/dump.rdb /path/to/your/backups/${REDIS_BACKUP_FILE}

echo "Redis backup created: ${REDIS_BACKUP_FILE}"

Restore Procedures

In the event of a failure, follow these steps to restore the application.

1. Restoring the System PostgreSQL Database

Step 1: Start a fresh database container. If the old volume is corrupted, remove it and start the champa-system-db service.

docker-compose -f docker-compose.server.yml up -d champa-system-db

Wait for the database to be ready.

Step 2: Restore the backup:

# Decompress the backup and pipe it to psql inside the container
gunzip < /path/to/your/backups/latest_backup.sql.gz | docker exec -i champa-system-db psql -U champa_user -d champa_system

Database Must Be Empty

If the database already has tables, you may need to drop them first or create a fresh database:

docker exec -it champa-system-db psql -U champa_user -d postgres -c "DROP DATABASE IF EXISTS champa_system;"
docker exec -it champa-system-db psql -U champa_user -d postgres -c "CREATE DATABASE champa_system;"

2. Restoring Redis Data

Step 1: Stop the application and Redis containers:

docker-compose -f docker-compose.server.yml stop champa-intelligence champa-redis

Step 2: Copy the backup file into the Redis volume:

# Find the volume path
VOLUME_PATH=$(docker volume inspect champa-intelligence_redis | grep Mountpoint | awk '{print $2}' | sed 's/"//g' | sed 's/,//g')

# Copy the backup file
cp /path/to/your/backups/latest_redis_backup.rdb ${VOLUME_PATH}/dump.rdb

Step 3: Restart the services:

docker-compose -f docker-compose.server.yml up -d

Complete Backup Script

Here's a complete backup script you can use:

#!/bin/bash

# Configuration
BACKUP_DIR="/path/to/your/backups"
RETENTION_DAYS=7

# Create backup directory if it doesn't exist
mkdir -p ${BACKUP_DIR}

# Timestamp
TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S)

# Backup PostgreSQL
echo "Starting PostgreSQL backup..."
BACKUP_FILE="${BACKUP_DIR}/champa_system_db_backup_${TIMESTAMP}.sql.gz"
docker exec champa-system-db pg_dump -U champa_user -d champa_system | gzip > ${BACKUP_FILE}

if [ $? -eq 0 ]; then
    echo "PostgreSQL backup completed: ${BACKUP_FILE}"
else
    echo "ERROR: PostgreSQL backup failed!"
    exit 1
fi

# Backup Redis
echo "Starting Redis backup..."
docker exec champa-redis redis-cli -a ${REDIS_PASSWORD} SAVE

VOLUME_PATH=$(docker volume inspect champa-intelligence_redis | grep Mountpoint | awk '{print $2}' | sed 's/"//g' | sed 's/,//g')
REDIS_BACKUP_FILE="${BACKUP_DIR}/champa_redis_backup_${TIMESTAMP}.rdb"

cp ${VOLUME_PATH}/dump.rdb ${REDIS_BACKUP_FILE}

if [ $? -eq 0 ]; then
    echo "Redis backup completed: ${REDIS_BACKUP_FILE}"
else
    echo "WARNING: Redis backup failed (non-critical)"
fi

# Cleanup old backups
echo "Cleaning up old backups (older than ${RETENTION_DAYS} days)..."
find ${BACKUP_DIR} -name "champa_system_db_backup_*.sql.gz" -mtime +${RETENTION_DAYS} -delete
find ${BACKUP_DIR} -name "champa_redis_backup_*.rdb" -mtime +${RETENTION_DAYS} -delete

echo "Backup process completed!"

Testing Your Backups

Critical: Test Your Backups Regularly

A backup is only useful if it can be restored. Test your backup and restore procedures regularly (e.g., monthly) in a non-production environment.

Testing Procedure

  1. Set up a test environment (separate Docker host or VMs)
  2. Copy your backup files to the test environment
  3. Follow the restore procedures above
  4. Verify the application starts correctly
  5. Log in and check that data is intact
  6. Document any issues or improvements

Backup Best Practices

  1. 3-2-1 Rule: Keep 3 copies of your data, on 2 different media, with 1 copy off-site
  2. Encrypt Backups: Especially if storing off-site or in cloud storage
  3. Monitor Backup Jobs: Set up alerts if backups fail
  4. Document Procedures: Keep restore procedures up-to-date and accessible
  5. Version Control: Keep multiple backup versions (not just the latest)

Off-Site Backup Example

# Example: Sync backups to AWS S3
aws s3 sync /path/to/your/backups s3://your-backup-bucket/champa-backups/ \
    --storage-class STANDARD_IA \
    --delete

# Example: Sync to remote server via rsync
rsync -avz --delete /path/to/your/backups/ user@backup-server:/backups/champa/