Skip to content

Instantly share code, notes, and snippets.

@syneart
Last active May 4, 2023 06:38
Show Gist options
  • Save syneart/22dd6b84bf4abe5d02d7247cf8e4d32c to your computer and use it in GitHub Desktop.
Save syneart/22dd6b84bf4abe5d02d7247cf8e4d32c to your computer and use it in GitHub Desktop.
Backup mysql databases into split sql file
@echo off&cls
SET z="C:\Program Files\7-Zip\7z.exe"
SET MYSQL_BIN_PATH="[REPLACE HERE BY YOURSELF, FOR EXAMPLE: C:\mysql\bin]"
SET LOCAL_STORE_BASE_PATH="[REPLACE HERE BY YOURSELF, FOR EXAMPLE: C:\]"
SET DATABASE_USERNAME="[REPLACE HERE BY YOURSELF, FOR EXAMPLE: root]"
SET DATABASE_PASSWORD="[REPLACE HERE BY YOURSELF, FOR EXAMPLE: password]"
SET TodayYear=%date:~0,4%
SET TodayMonthP0=%date:~5,2%
SET TodayDayP0=%date:~8,2%
SET TMP_PATH="%tmp%\Jed-%RANDOM%"
MKDIR %TMP_PATH%
FOR /f "tokens=1* delims=:" %%s IN ('%MYSQL_BIN_PATH:"=%\mysql -u%DATABASE_USERNAME:"=% -p%DATABASE_PASSWORD:"=% -e "show databases;"') DO (
ECHO %%s | FINDSTR /r "mysql phpmyadmin Database information_schema performance_schema" > NUL
IF ERRORLEVEL 1 (
ECHO Export %%s.sql
REM Same issue, commented out (#) the no-beep line in the MySQL my.ini, solved the problem.
%MYSQL_BIN_PATH:"=%\mysqldump -u%DATABASE_USERNAME% -p%DATABASE_PASSWORD% --skip-extended-insert %%s > %TMP_PATH%\%%s.sql
)
)
ECHO FILE IN %TMP_PATH:"=%
%z% a %LOCAL_STORE_BASE_PATH%\%TodayYear%\%TodayMonthP0%\%TodayYear%%TodayMonthP0%%TodayDayP0%_sql.zip %TMP_PATH%\*
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment