Skip to content

Instantly share code, notes, and snippets.

@robilin
Forked from josemmo/repair-mysql-data.ps1
Created October 13, 2024 14:39
Show Gist options
  • Select an option

  • Save robilin/af5796ed1cd9039b7fb1a1db072d75e6 to your computer and use it in GitHub Desktop.

Select an option

Save robilin/af5796ed1cd9039b7fb1a1db072d75e6 to your computer and use it in GitHub Desktop.
Repair MySQL data directory (for XAMPP)
# Based on this answer: https://stackoverflow.com/a/61859561/1956278
# Backup old data
Rename-Item -Path "./data" -NewName "./data_old"
# Create new data directory
Copy-Item -Path "./backup" -Destination "./data" -Recurse
Remove-Item "./data/test" -Recurse
$dbPaths = Get-ChildItem -Path "./data_old" -Exclude ('mysql', 'performance_schema', 'phpmyadmin') -Recurse -Directory
Copy-Item -Path $dbPaths.FullName -Destination "./data" -Recurse
Copy-Item -Path "./data_old/ibdata1" -Destination "./data/ibdata1"
# Notify user
Write-Host "Finished repairing MySQL data"
Write-Host "Previous data is located at ./data_old"
@robilin
Copy link
Author

robilin commented Oct 13, 2024

Btch script version if you have complications with powershell

@echo off
REM Backup old data
rename "data" "data_old"

REM Create new data directory
xcopy "backup" "data" /E /I
rmdir /S /Q "data\test"

REM Copy directories excluding specific ones (mysql, performance_schema, phpmyadmin)
for /d %%i in (data_old\*) do (
    if /i not "%%~nxi"=="mysql" (
        if /i not "%%~nxi"=="performance_schema" (
            if /i not "%%~nxi"=="phpmyadmin" (
                xcopy "%%i" "data\%%~nxi" /E /I
            )
        )
    )
)

REM Copy ibdata1 file
copy "data_old\ibdata1" "data\ibdata1"

REM Notify user
echo Finished repairing MySQL data
echo Previous data is located at ./data_old

#run this in mysql folder as batch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment