码上焚香

Yahocen

Windows 自动备份 MySQL 数据库

25
2024-05-10

上代码,如何设置定时执行请移步 https://jingyan.baidu.com/article/39810a2396cef6b637fda67c.html

Mysql_Auto_BackUp.bat

rem ******MySQL 备份开始******
@echo off

::=======  设置参数 =========================

:: 备份文件储存目录  当前目录(%~dp0)
set "backup_dri=G:\Personal\Desktop\data"

:: MySQL命令bin目录
set "mysql_server_path=C:\Program Files\MySQL\MySQL Server 5.7"

::mysql用户名
set "myusername=root"

::mysql密码
set "mypassword=xxxx"

::mysql要备份的库名
set "mydatabase=xxxx"

::=======  备份逻辑 =========================

::删除过期文件 (想永久保留可注释此行命令)
forfiles /p "%backup_dri%" /m test_backup_*.sql -d -30 /c "cmd /c del /f @path"

::执行备份
set "Ymd=%date:~0,4%%date:~5,2%%date:~8,2%0%time:~1,1%%time:~3,2%%time:~6,2%"
"%mysql_server_path%\bin\mysqldump" --opt --single-transaction=TRUE --user=%myusername% --password=%mypassword% --host=127.0.0.1 --protocol=tcp --port=3306 --default-character-set=utf8 --single-transaction=TRUE --routines --events "%mydatabase%" > %backup_dri%\%mydatabase%_backup_%Ymd%.sql

@echo on
rem ******MySQL 备份结束******