Skip to content

Instantly share code, notes, and snippets.

@josemmo
Created August 28, 2020 18:48
Show Gist options
  • Save josemmo/24e35f2b4984a4370ce2c164f5956437 to your computer and use it in GitHub Desktop.
Save josemmo/24e35f2b4984a4370ce2c164f5956437 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"
@Waheire
Copy link

Waheire commented Feb 2, 2023

Great! worked!, thank you!

@cartzedan
Copy link

cartzedan commented Apr 24, 2023

Great work. This works perfectly without errors or stress.

@MarioSwitch
Copy link

Perfect!

@DKVI
Copy link

DKVI commented Aug 2, 2023

I confirm it still works!

@ucheohams
Copy link

This still works, thank you

@rbtarthur
Copy link

thank you very much it still works!

@akimi0308
Copy link

Saved me! Thank you sooooooo much!

@spring8904
Copy link

Thank you!

@Chanaka-J
Copy link

Chanaka-J commented Jan 17, 2024

you can add a line at the beginning of the script to delete the existing "data_old" directory before creating a new backup.

# Delete existing data_old directory
Remove-Item -Path "./data_old" -Recurse -Force

@ItsSalman99
Copy link

Amazing ...

@Arman016
Copy link

Arman016 commented Feb 7, 2024

It's just awesome! Thanks very much!

@KipchirchirIan
Copy link

Marvelous! Works beautifully.

@aagirre92
Copy link

thanks! as @Chanaka-J suggests adding that line helps in case you have to run it more than once

@bismark-joe
Copy link

Thank you a thousand times.

@toperrrrrrrr
Copy link

This is amazing. Thank you!

@giang61
Copy link

giang61 commented Apr 9, 2024

Kudos. You're the best !

@gordonstevens
Copy link

Golden!

Thank you guise, this has been such an annoying beast that comes around more than once. Interesting though this has been on the books for 4 years now and still going strong.

you can add a line at the beginning of the script to delete the existing "data_old" directory before creating a new backup.

# Delete existing data_old directory
Remove-Item -Path "./data_old" -Recurse -Force

@ResilientSpring
Copy link

Thank you all!

@vivekblaze
Copy link

Thanks

@JanRomero
Copy link

Thank you!

@DrexxBoban
Copy link

If you get error as I did about directories not existing, change the file to this, and if paths are different change the location of directories to your absolute path:

Based on this answer: https://stackoverflow.com/a/61859561/1956278

Backup old data

Rename-Item -Path "C:/xampp/mysql/data" -NewName "C:/xampp/mysql/data_old"

Create new data directory

Copy-Item -Path "C:/xampp/mysql/backup" -Destination "C:/xampp/mysql/data" -Recurse
Remove-Item "C:/xampp/mysql/data/test" -Recurse
$dbPaths = Get-ChildItem -Path "C:/xampp/mysql/data_old" -Exclude ('mysql', 'performance_schema', 'phpmyadmin') -Recurse -Directory
Copy-Item -Path $dbPaths.FullName -Destination "C:/xampp/mysql/data" -Recurse
Copy-Item -Path "C:/xampp/mysql/data_old/ibdata1" -Destination "C:/xampp/mysql/data/ibdata1"

Notify user

Write-Host "Finished repairing MySQL data"
Write-Host "Previous data is located at C:/xampp/mysql/data_old"

@VictorJoshuaC
Copy link

you are the best

@robilin
Copy link

robilin commented Oct 13, 2024

Equivalent bash scripts is

`@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 inside mysql directory, save it like repair.bat , easier to save using notepad++, select bat for file type

@Hoyoll
Copy link

Hoyoll commented Nov 16, 2024

you saved my afternoon

@LaurensTOffringa
Copy link

Thank you!

@okellojohnsilas
Copy link

One of the best gists

@patelmm
Copy link

patelmm commented Dec 23, 2024

I realize existing Database was not able to access and getting error.

@Pyth3rEx
Copy link

Equivalent bash scripts is

`@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 inside mysql directory, save it like repair.bat , easier to save using notepad++, select bat for file type

I've edited this code a bit to include PWD support as my install had issues jumping between disks. ^^

@echo off
REM Change directory to the MySQL directory
cd /d "D:\Path\To\XAMPP\mysql"

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
pause

@shikhar-graycyn
Copy link

Thank you, Great work !

@h-behgam
Copy link

h-behgam commented Jan 9, 2025

@echo off
REM Change directory to the MySQL directory
cd /d "D:\Path\To\XAMPP\mysql"

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
pause

Thanks, this code works correctly.

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