diff --git a/modules/backup/mysql-restore-to-sql.sh b/modules/backup/mysql-restore-to-sql.sh index 7087862..6fa3f53 100755 --- a/modules/backup/mysql-restore-to-sql.sh +++ b/modules/backup/mysql-restore-to-sql.sh @@ -878,8 +878,22 @@ start_second_instance() { # Check if socket already exists (instance already running) if [ -S "$datadir/socket.mysql" ]; then print_warning "Socket file already exists. Attempting to shut down existing instance..." + + # Use proper shutdown validation (same as stop_second_instance) mysqladmin -h localhost -S "$datadir/socket.mysql" shutdown 2>/dev/null || true - sleep 2 + + # Wait for socket to disappear (up to 5 seconds) + local cleanup_wait=0 + while [ -S "$datadir/socket.mysql" ] && [ "$cleanup_wait" -lt 5 ]; do + sleep 1 + cleanup_wait=$((cleanup_wait + 1)) + done + + # If socket still exists, try force removal + if [ -S "$datadir/socket.mysql" ]; then + print_warning "Existing instance didn't shut down cleanly. Force removing socket..." + rm -f "$datadir/socket.mysql" "$datadir/mysql.lock" 2>/dev/null || true + fi fi # Build mysqld command @@ -924,7 +938,22 @@ start_second_instance() { print_error "InnoDB initialization encountered errors" echo "" print_warning "Attempting to shut down second instance..." + + # Use proper shutdown validation instead of fire-and-forget mysqladmin -h localhost -S "$datadir/socket.mysql" shutdown 2>/dev/null || true + + # Wait for socket to disappear (up to 5 seconds) + local error_cleanup_wait=0 + while [ -S "$datadir/socket.mysql" ] && [ "$error_cleanup_wait" -lt 5 ]; do + sleep 1 + error_cleanup_wait=$((error_cleanup_wait + 1)) + done + + # Remove stale socket/lock if still present + if [ -S "$datadir/socket.mysql" ]; then + rm -f "$datadir/socket.mysql" "$datadir/mysql.lock" 2>/dev/null || true + fi + echo "" print_info "Review full error log:" echo " tail -100 $datadir/mysql.err"