#!/bin/bash # ============================================================================ # Comprehensive Corruption Fix - All Tables # ============================================================================ # This script: # 1. Creates a backup # 2. Scans all tables for corruption # 3. Prompts for confirmation # 4. Applies fixes to ALL tables # 5. Shows before/after comparison # ============================================================================ set -e # Exit on error DB_USER="root" DB_PASS="secureRootPassword" DB_NAME="cmc" CONTAINER="cmc-db" BACKUP_DIR="./backups" TIMESTAMP=$(date +%Y%m%d_%H%M%S) # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color echo -e "${BLUE}============================================================${NC}" echo -e "${BLUE}CMC Database - Comprehensive Corruption Fix${NC}" echo -e "${BLUE}============================================================${NC}" echo "" # Create backups directory mkdir -p "$BACKUP_DIR" # Step 1: Create backup echo -e "${YELLOW}Step 1: Creating full database backup...${NC}" docker exec $CONTAINER mariadb-dump \ -u $DB_USER \ -p$DB_PASS \ --default-character-set=utf8mb4 \ --single-transaction \ $DB_NAME | gzip > "$BACKUP_DIR/backup_comprehensive_fix_$TIMESTAMP.sql.gz" BACKUP_SIZE=$(du -h "$BACKUP_DIR/backup_comprehensive_fix_$TIMESTAMP.sql.gz" | cut -f1) echo -e "${GREEN}✓ Backup created: backup_comprehensive_fix_$TIMESTAMP.sql.gz (${BACKUP_SIZE})${NC}" echo "" # Step 2: Scan all tables echo -e "${YELLOW}Step 2: Scanning ALL tables for corruption...${NC}" echo "-------------------------------------------" docker exec $CONTAINER mariadb \ -u $DB_USER \ -p$DB_PASS \ --default-character-set=utf8mb4 \ $DB_NAME < scripts/scan_all_tables_for_corruption.sql | head -100 echo "" # Step 3: Prompt for confirmation echo -e "${RED}IMPORTANT:${NC}" echo "- Backup created at: $BACKUP_DIR/backup_comprehensive_fix_$TIMESTAMP.sql.gz" echo "- This will modify ALL text columns in ALL tables" echo "- Fixes mojibake patterns like: ’ → ', é → é, ° → °, etc." echo "- Tables affected: customers, principles, addresses, products, contacts," echo " emails, invoices, jobs, quotes, purchase_orders, shipments, etc." echo "" read -p "Do you want to proceed with the comprehensive fix? (yes/no): " CONFIRM if [ "$CONFIRM" != "yes" ]; then echo -e "${RED}Fix cancelled by user${NC}" exit 0 fi # Step 4: Apply comprehensive fixes echo "" echo -e "${YELLOW}Step 4: Applying comprehensive corruption fixes...${NC}" docker exec -i $CONTAINER mariadb \ -u $DB_USER \ -p$DB_PASS \ --default-character-set=utf8mb4 \ $DB_NAME < scripts/fix_all_corruption_hex.sql echo -e "${GREEN}✓ Comprehensive fix executed${NC}" echo "" # Step 5: Re-scan to show results echo -e "${YELLOW}Step 5: Re-scanning to verify fixes...${NC}" echo "---------------------------------------" docker exec $CONTAINER mariadb \ -u $DB_USER \ -p$DB_PASS \ --default-character-set=utf8mb4 \ $DB_NAME < scripts/scan_all_tables_for_corruption.sql | head -100 echo "" # Final summary echo -e "${BLUE}============================================================${NC}" echo -e "${GREEN}COMPREHENSIVE FIX COMPLETED!${NC}" echo -e "${BLUE}============================================================${NC}" echo "" echo "Summary:" echo "- Backup location: $BACKUP_DIR/backup_comprehensive_fix_$TIMESTAMP.sql.gz" echo "- Fixed corruption patterns across ALL tables" echo "- Common patterns: smart quotes, accented characters, symbols, HTML entities" echo "" echo "Next steps:" echo "1. Review the fixed data in the application" echo "2. Test critical functionality (quotes, invoices, emails, etc.)" echo "3. Run: bash scripts/report_remaining_corruption.sh if any issues remain" echo "" echo "To rollback if needed:" echo " gunzip < $BACKUP_DIR/backup_comprehensive_fix_$TIMESTAMP.sql.gz | \\" echo " docker exec -i $CONTAINER mariadb -u $DB_USER -p$DB_PASS $DB_NAME" echo ""