二话不说,上代码:
@echo off & setlocal ENABLEEXTENSIONS
set BACKUP_PATH=D:\Backup\
set USERNAME=root
set PASSWORD=123456
set MYSQL=”D:\Huweishen.com\PHPWEB\MySQL Server 5.5\bin\”
set WINRAR=”D:\Program Files\WinRAR\Rar.exe”
set YEAR=%date:~0,4%
set MONTH=%date:~5,2%
set DAY=%date:~8,2%
set HOUR=%time:~0,2%
set MINUTE=%time:~3,2%
set SECOND=%time:~6,2%
set DIR=%BACKUP_PATH%%YEAR%\%MONTH%\%DAY%\
set ADDON=%YEAR%%MONTH%%DAY%%HOUR%%MINUTE%%SECOND%
:: create dir
if not exist %DIR% (
mkdir %DIR% 2>nul
)
if not exist %DIR% (
echo Backup path: %DIR% not exists, createdir failed.
goto exit
)
cd /d %DIR%
echo select db from mysql.db > %BACKUP_PATH%”sql.sql”
::set DATABASES=
%MYSQL%MySQL -u%USERNAME% -p%PASSWORD% < %BACKUP_PATH%sql.sql > out.sql
set mysqldump=%MYSQL%”mysqldump”
:: backup
echo Start dump databases…
for /f “skip=1 tokens=* delims=” %%D in (‘FINDSTR /l /v “ECHO ” out.sql’) do (
echo Dumping database %%D …
%mysqldump% -u%USERNAME% -p%PASSWORD% %%D > %%D.%ADDON%.sql 2>nul
:: winrar
if exist %WINRAR% (
%WINRAR% a -k -r -s -m1 -ep1 %%D.%ADDON%.rar %%D.%ADDON%.sql 2>nul
del /F /S /Q %%D.%ADDON%.sql 2>nul
)
)
)
echo Done
:exit