MySQL数据库全量备份脚本(Windows版)
MySQL数据库全量备份bat(Windows)脚本,备份核心逻辑:
- mysqldump全量导出数据库文件;
- 压缩备份文件;
- 备份脚本由Windows计划任务定时触发;
创建Windows计划任务核心命令如下:
schtasks /create /F /tn %taskName% /tr %cd%\bin\mysql_backup.bat /sc daily /st 18:00
详细脚本如下:
rem *********备份 MySQL 开始**************
@echo off
:: 设置时间变量,格式:yyyyMMdd
set "Ymd=%date:~0,4%%date:~5,2%%date:~8,2%"
:: 安装目录(installDir占位符,在执行安装过程中会替换为实际安装目录)
set "installDir=#{installDir}"
:: 备份目录
set "backupDir=C:\smaple\backup"
:: 判断系统类型
if exist %installDir%\mysql-5.7.31-win32\bin ( set "systemType=win32")
if exist %installDir%\mysql-5.7.31-winx64\bin ( set "systemType=winx64")
:: 创建备份目录
if not exist "%backupDir%" md "%backupDir%"
:: 备份文件
set "backupFileName=%backupDir%\smaple-yd_%Ymd%"
set "backupSqlFile=%backupFileName%.sql"
set "backupZipFile=%backupFileName%.zip"
:: 执行备份操作
if %systemType% == win32 (
set "mysqldumpexe=%installDir%\mysql-5.7.31-win32\bin\mysqldump"
)
if %systemType% == winx64 (
set "mysqldumpexe=%installDir%\mysql-5.7.31-winx64\bin\mysqldump"
)
%mysqldumpexe% -uhis -p123456 --quick -R -E --databases smaple-yd --single-transaction > %backupSqlFile%
:: 压缩当前备份文件
"%installDir%\smaple-ws\7zip.exe" a %backupZipFile% %backupSqlFile% -p123
:: 删除SQL文件
del /f /q %backupSqlFile%
:: 删除一个月之前的备份文件
forfiles /p "%backupDir%" /m "smaple-yd_*.zip" -d -30 /c "cmd /c del /f @path"
@echo on
rem *********备份 MySQL 完成**************