diff --git a/modules/backup/mysql-restore-to-sql.sh b/modules/backup/mysql-restore-to-sql.sh index ebdd69a..21f83f8 100755 --- a/modules/backup/mysql-restore-to-sql.sh +++ b/modules/backup/mysql-restore-to-sql.sh @@ -264,6 +264,7 @@ check_innodb_errors() { show_recovery_options() { local datadir="$1" local current_recovery="${2:-0}" + local selected_database="${3:-}" # The database user wants to restore print_banner "Recovery Options" @@ -273,10 +274,26 @@ show_recovery_options() { local corruption_detected="" local redo_incompatible="" local memory_issue="" + local missing_from_selected_db="" + local missing_from_other_dbs="" if [ -f "$error_log" ]; then if grep -qE "Cannot open tablespace|Tablespace.*missing|Tablespace.*was not found|Unable to open" "$error_log"; then missing_files="yes" + + # Check if missing files are from the selected database or other databases + if [ -n "$selected_database" ]; then + if grep -q "was not found at \./$selected_database/" "$error_log"; then + missing_from_selected_db="yes" + fi + # Check if any missing files are NOT from the selected database + while IFS= read -r line; do + if ! echo "$line" | grep -q "\./$selected_database/"; then + missing_from_other_dbs="yes" + break + fi + done < <(grep "was not found at" "$error_log") + fi fi if grep -qE "Corrupted|Database page corruption" "$error_log"; then corruption_detected="yes" @@ -291,6 +308,27 @@ show_recovery_options() { # Provide targeted guidance based on error type if [ -n "$missing_files" ]; then + # Smart detection: if missing files are ONLY from other databases (not the selected one) + if [ -z "$missing_from_selected_db" ] && [ -n "$missing_from_other_dbs" ] && [ -n "$selected_database" ]; then + print_warning "SMART DETECTION: Missing files are from OTHER databases, not '$selected_database'" + echo "" + print_success "Your selected database '$selected_database' appears to have all files!" + echo "" + print_warning "RECOMMENDED ACTION: Use Force Recovery Level 1" + echo "" + echo "The ibdata1 file contains references to databases you didn't restore." + echo "Force Recovery Level 1 will:" + echo " ✓ Ignore missing databases (safe - you don't have them anyway)" + echo " ✓ Start MySQL successfully" + echo " ✓ Allow you to dump '$selected_database' with NO data loss" + echo "" + echo "This is the CORRECT approach for selective database restoration." + echo "" + print_info "Re-run this script and select Force Recovery Level 1 in Step 4" + echo "" + return 0 + fi + print_error "DIAGNOSIS: Missing or unopenable tablespace files" echo "" @@ -1431,8 +1469,8 @@ step5_create_dump() { print_error "Failed to start second MySQL instance" echo "" - # Provide intelligent recovery guidance - show_recovery_options "$TEMP_DATADIR" "$FORCE_RECOVERY" + # Provide intelligent recovery guidance (pass selected database name) + show_recovery_options "$TEMP_DATADIR" "$FORCE_RECOVERY" "$DATABASE_NAME" press_enter return 1