Forked from sindresorhus/mysql-backup-windows.bat
Last active
September 19, 2015 11:20
-
-
Save pachecoder/cf91d844634bf5973749 to your computer and use it in GitHub Desktop.
Backup MySQL databases in separate gzipped sql files on Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
set dbUser=root | |
set dbPassword=yourPassword | |
set backupDir="C:\Documents and Settings\user\Desktop\backup\mysql" | |
set mysqldump="C:\Program Files\MySQL\MySQL Workbench 5.2 CE\mysqldump.exe" | |
set mysqlDataDir="C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.1\data" | |
set zip="C:\Program Files\7-Zip\7z.exe" | |
::Change date delimiter to - | |
:: get date | |
for /F "tokens=2-4 delims=- " %%i in ('date /t') do ( | |
set mm=%%i | |
set dd=%%j | |
set yy=%%k | |
) | |
:: get time | |
for /F "tokens=5-8 delims=:. " %%i in ('echo.^| time ^| find "current" ') do ( | |
set hh=%%i | |
set mm=%%j | |
) | |
set dirName=%yy%%mm%%dd%_%hh%%mm% | |
:: switch to the "data" folder | |
pushd %mysqlDataDir% | |
:: iterate over the folder structure in the "data" folder to get the databases | |
for /d %%f in (*) do ( | |
if not exist %backupDir%\%dirName%\ ( | |
mkdir %backupDir%\%dirName% | |
) | |
%mysqldump% --host="localhost" --user=%dbUser% --password=%dbPassword% --single-transaction --add-drop-table --databases %%f > %backupDir%\%dirName%\%%f.sql | |
%zip% a -tgzip %backupDir%\%dirName%\%%f.sql.gz %backupDir%\%dirName%\%%f.sql | |
del %backupDir%\%dirName%\%%f.sql | |
) | |
echo delete old backup | |
::Change the route of backup dir. | |
forfiles /p D:\GoogleDrive\DirLocal /s /m *.* /d -3 /c "cmd /c del @file : date >= 3days" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment