Bat批处理使用mysqldump批量备份MySql数据库并打包

注:多数据库备份,统一使用root用户导出,文件起始部分内容为需要配置的内容。

非MySql数据库的自动备份只需要修改mysqldump口令为其他数据库口令(如Oracle的Exp)。

循环时只校验db_length长度,所以db_length长度必须小于或等于db数组的长度。

@echo off

rem 定义必要目录
set mysql_dir=G:\mysql-5.7.15-winx64\bin\
set winrar_dir=C:\"Program Files"\WinRAR\
set bak_dir=D:\mysql_bak\
set host=127.0.0.1
set port=3306

rem 打印目录信息
echo ---------------------------------
echo mysql backup at %date% %time%
echo mysql_dir=%mysql_dir%
echo winrar_dir=%winrar_dir%
echo bak_dir=%bak_dir%
echo host=%host%:%port%
echo ---------------------------------

rem 获取时间
set year=%date:~0,4%
set month=%date:~5,2%
set day=%date:~8,2%
set hour=%time:~0,2%
set hour=%hour: =0%
set minutes=%time:~3,2%
set seconds=%time:~6,2%
set fulldate=%year%%month%%day%%hour%%minutes%%seconds%

rem 定义数据库
set db_length=2
set db[0]=diancar
set db[1]=g1tic

rem 开始数据库备份
set db_index=0
:LoopStart
if %db_index% equ %db_length% goto :eof
set db_curr=0
for /f "usebackq delims==. tokens=1-3" %%i in (`set db[%db_index%]`) do (
    set db_curr=%%j
)
echo database [%db_curr%] begin backup.
%mysql_dir%mysqldump --no-defaults -h%host% -P%port% -uroot -proot %db_curr% > %bak_dir%%db_curr%.sql
%winrar_dir%rar.exe a -ep %bak_dir%MySql_Bak_Auto_%fulldate%.rar %bak_dir%%db_curr%.sql
del %bak_dir%%db_curr%.sql
echo database [%db_curr%] already backup.
set /a db_index=%db_index% + 1
goto LoopStart

echo ---------------------------------
echo mysql auto backup end
echo ---------------------------------
@echo off

你可能感兴趣的:(MySql)