Files
igny8/scripts/ops/restore-db.sh
IGNY8 VPS (Salman) 4a200822bb Dev ops prep
2026-01-21 17:53:42 +00:00

143 lines
4.4 KiB
Bash

#!/bin/bash
# =============================================================================
# IGNY8 Database Restore Script
# =============================================================================
# Usage: ./restore-db.sh <backup_file> [target_db]
# Restores database from a backup file (.sql or .sql.gz)
# =============================================================================
set -e
# Configuration
BACKUP_FILE="${1}"
TARGET_DB="${2:-igny8_db}"
DB_CONTAINER="postgres"
DB_USER="igny8"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log() {
echo -e "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}
log_success() {
log "${GREEN}$1${NC}"
}
log_warn() {
log "${YELLOW}⚠️ $1${NC}"
}
log_error() {
log "${RED}$1${NC}"
}
# Validate input
if [[ -z "${BACKUP_FILE}" ]]; then
echo "Usage: ./restore-db.sh <backup_file> [target_db]"
echo ""
echo "Arguments:"
echo " backup_file Path to backup file (.sql or .sql.gz)"
echo " target_db Target database (default: igny8_db)"
echo ""
echo "Available backups:"
ls -la /data/backups/latest_db.sql.gz 2>/dev/null || echo " No latest backup found"
echo ""
find /data/backups -name "*.sql.gz" -type f 2>/dev/null | head -10
exit 1
fi
if [[ ! -f "${BACKUP_FILE}" ]]; then
log_error "Backup file not found: ${BACKUP_FILE}"
exit 1
fi
echo "=========================================="
echo "IGNY8 Database Restore"
echo "=========================================="
echo ""
echo "Backup file: ${BACKUP_FILE}"
echo "Target DB: ${TARGET_DB}"
echo ""
# Safety confirmation
log_warn "This will REPLACE the database '${TARGET_DB}' with backup data!"
read -p "Are you sure? Type 'yes' to continue: " confirm
if [[ "${confirm}" != "yes" ]]; then
log "Restore cancelled"
exit 0
fi
# Check if postgres container is running
if ! docker ps --format '{{.Names}}' | grep -q "^${DB_CONTAINER}$"; then
log_error "PostgreSQL container '${DB_CONTAINER}' is not running"
exit 1
fi
# Create pre-restore backup
log "Creating pre-restore backup of current database..."
PRE_RESTORE_BACKUP="/data/backups/pre-restore_${TARGET_DB}_$(date +%Y%m%d_%H%M%S).sql.gz"
if docker exec "${DB_CONTAINER}" pg_dump -U "${DB_USER}" "${TARGET_DB}" 2>/dev/null | gzip > "${PRE_RESTORE_BACKUP}"; then
log_success "Pre-restore backup: ${PRE_RESTORE_BACKUP}"
else
log_warn "Could not create pre-restore backup (database might not exist)"
fi
# Stop application containers to prevent connections
log "Stopping application containers..."
docker compose -f /data/app/igny8/docker-compose.app.yml -p igny8-app stop igny8_backend igny8_celery_worker igny8_celery_beat 2>/dev/null || true
# Drop and recreate database
log "Dropping existing database..."
docker exec "${DB_CONTAINER}" psql -U postgres -c "DROP DATABASE IF EXISTS ${TARGET_DB};" || true
log "Creating fresh database..."
docker exec "${DB_CONTAINER}" psql -U postgres -c "CREATE DATABASE ${TARGET_DB} OWNER ${DB_USER};"
# Restore from backup
log "Restoring database from backup..."
if [[ "${BACKUP_FILE}" == *.gz ]]; then
# Compressed backup
gunzip -c "${BACKUP_FILE}" | docker exec -i "${DB_CONTAINER}" psql -U "${DB_USER}" -d "${TARGET_DB}"
else
# Uncompressed backup
docker exec -i "${DB_CONTAINER}" psql -U "${DB_USER}" -d "${TARGET_DB}" < "${BACKUP_FILE}"
fi
if [[ $? -eq 0 ]]; then
log_success "Database restore complete"
else
log_error "Database restore failed"
exit 1
fi
# Restart application containers
log "Restarting application containers..."
docker compose -f /data/app/igny8/docker-compose.app.yml -p igny8-app start igny8_backend igny8_celery_worker igny8_celery_beat 2>/dev/null || true
# Wait for backend to be healthy
log "Waiting for backend to be healthy..."
sleep 10
# Verify restoration
log "Verifying database..."
TABLE_COUNT=$(docker exec "${DB_CONTAINER}" psql -U "${DB_USER}" -d "${TARGET_DB}" -t -c "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public';")
log_success "Database restored with ${TABLE_COUNT} tables"
echo ""
echo "=========================================="
echo "RESTORE COMPLETE"
echo "=========================================="
echo "Database '${TARGET_DB}' has been restored"
echo ""
echo "Pre-restore backup saved to:"
echo " ${PRE_RESTORE_BACKUP}"
echo ""
echo "To rollback this restore, run:"
echo " ./restore-db.sh ${PRE_RESTORE_BACKUP} ${TARGET_DB}"
echo "=========================================="