#!/bin/bash # Database Safety Script for BCT Infrastructure # Provides safe database operations with confirmations set -e RED='\033[0;31m' YELLOW='\033[1;33m' GREEN='\033[0;32m' NC='\033[0m' # No Color PROJECT_DIR="/var/www/bct-whitelabel" BACKUP_DIR="$PROJECT_DIR/backups" # Ensure we're in the right directory cd "$PROJECT_DIR" # Create backup directory if it doesn't exist mkdir -p "$BACKUP_DIR" function backup_database() { echo -e "${GREEN}Creating database backup...${NC}" TIMESTAMP=$(date +%Y%m%d_%H%M%S) BACKUP_FILE="$BACKUP_DIR/directus_backup_$TIMESTAMP.sql" if docker ps | grep -q "bct-postgres"; then docker exec bct-postgres pg_dump -U directus directus > "$BACKUP_FILE" echo -e "${GREEN}✅ Backup created: $BACKUP_FILE${NC}" else echo -e "${RED}❌ PostgreSQL container not running${NC}" exit 1 fi } function restore_database() { echo -e "${YELLOW}Available backups:${NC}" ls -la "$BACKUP_DIR"/*.sql 2>/dev/null || echo "No backups found" read -p "Enter backup filename: " BACKUP_FILE if [ ! -f "$BACKUP_DIR/$BACKUP_FILE" ]; then echo -e "${RED}❌ Backup file not found${NC}" exit 1 fi echo -e "${RED}⚠️ WARNING: This will overwrite the current database!${NC}" read -p "Type 'CONFIRM' to proceed: " CONFIRM if [ "$CONFIRM" != "CONFIRM" ]; then echo "Operation cancelled" exit 1 fi # Stop Directus to prevent conflicts docker-compose -f docker-compose.infrastructure.yml stop directus # Restore database docker exec bct-postgres dropdb -U directus directus --if-exists docker exec bct-postgres createdb -U directus directus docker exec -i bct-postgres psql -U directus directus < "$BACKUP_DIR/$BACKUP_FILE" # Restart Directus docker-compose -f docker-compose.infrastructure.yml up -d directus echo -e "${GREEN}✅ Database restored from $BACKUP_FILE${NC}" } function reset_database() { echo -e "${RED}⚠️ WARNING: This will PERMANENTLY DELETE all database data!${NC}" echo -e "${RED}This includes:${NC}" echo -e "${RED}- All Directus content and collections${NC}" echo -e "${RED}- All user accounts${NC}" echo -e "${RED}- All uploaded files${NC}" echo -e "${RED}- All extensions${NC}" echo "" echo -e "${YELLOW}Volumes that will be deleted:${NC}" echo "- bct-whitelabel_postgres_data" echo "- bct-whitelabel_directus_uploads" echo "- bct-whitelabel_directus_extensions" echo "" read -p "Type 'DELETE_EVERYTHING' to confirm: " CONFIRM if [ "$CONFIRM" != "DELETE_EVERYTHING" ]; then echo "Operation cancelled" exit 1 fi # Create final backup before deletion echo -e "${YELLOW}Creating final backup before deletion...${NC}" backup_database # Stop and remove containers docker-compose -f docker-compose.infrastructure.yml down # Remove volumes docker volume rm bct-whitelabel_postgres_data bct-whitelabel_directus_uploads bct-whitelabel_directus_extensions echo -e "${GREEN}✅ Database completely reset${NC}" echo -e "${YELLOW}To recreate infrastructure: npm run docker:infrastructure:up${NC}" } function check_status() { echo -e "${GREEN}Infrastructure Status:${NC}" echo "" # Check containers if docker ps | grep -q "bct-postgres"; then echo -e "PostgreSQL: ${GREEN}✅ Running${NC}" else echo -e "PostgreSQL: ${RED}❌ Not running${NC}" fi if docker ps | grep -q "bct-directus"; then echo -e "Directus: ${GREEN}✅ Running${NC}" else echo -e "Directus: ${RED}❌ Not running${NC}" fi echo "" # Check volumes echo -e "${GREEN}Data Volumes:${NC}" docker volume ls | grep bct-whitelabel || echo "No volumes found" echo "" # Check recent backups echo -e "${GREEN}Recent Backups:${NC}" ls -la "$BACKUP_DIR"/*.sql 2>/dev/null | tail -5 || echo "No backups found" } # Main menu case "$1" in backup) backup_database ;; restore) restore_database ;; reset) reset_database ;; status) check_status ;; *) echo "BCT Database Safety Script" echo "" echo "Usage: $0 {backup|restore|reset|status}" echo "" echo "Commands:" echo " backup - Create database backup" echo " restore - Restore from backup (with confirmation)" echo " reset - Complete database reset (with confirmation)" echo " status - Check infrastructure status" echo "" exit 1 ;; esac