Mysql uses mysqldump backup to implement scripts for sql files
first way of writing
在这里插入代码片
```@echo off
set h=%time:~0,2%
set h=%h: =0%
set hhmiss=%date:~0,4%%date:~5,2%%date:~8,2%%h%%time:~3,2%%time:~6,2%
set "folder=%date:~0,4%%date:~5,2%%date:~8,2%"
::自动创建当天文件夹(dbBack这个文件夹需要自行创建)
if not exist D:\dbBack\%folder% (md D:\dbBack\%folder%)
::(mysql路径)\bin\mysqldump --opt -u 数据库账号 -p数据库密码 数据库名称 >输出地址
"C:\Program Files\MySQL\MySQL Server 5.7\bin"\mysqldump --opt -uroot -p123456 test > D:\dbBack\%folder%\test.sql
@echo on
::正式运行环境把pause去掉,不然cmd黑窗口会一直保留
::pause
Note here, if the path of mysql is installed according to the default address, because there are spaces in the address, such as " C:\Program Files\MySQL\MySQL Server 5.7\bin ", you need to add double quotes, If there are no spaces, double quotes are not required.
second way of writing
%1 mshta vbscript:createobject("wscript.shell").run("""%~0"" rem",0)(window.close)&&exit
@echo off
forfiles /p "D:\backup_data\54" /m test_*.sql -d -7 /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%"
"C:\Program Files\MySQL\MySQL Server 5.7\bin"\mysqldump -h 127.0.0.1 --opt -u root --password="123456" --default-character-set=utf8 test > D:\backup_data\54\test_%Ymd%.sql
@echo on
Compared with the first way of writing, the second way adds that when backing up the database, historical backup data beyond seven days will be automatically deleted (but the file storage address needs to be manually created first), the specific statement here is to pay attention to here forfiles /p "D:\backup_data\54" /m zf_ahs_platform_*.sql -d -7 /c "cmd /c del /f @path"
is , everyone should modify the address according to the location they want to store. For example, the address on my side is: D:\backup_data\54, and the name of the database storage should also be changed. For example, here is: test_*., the details are shown in the figure below:
third way of writing
%1 mshta vbscript:createobject("wscript.shell").run("""%~0"" rem",0)(window.close)&&exit
@echo off
set h=%time:~0,2%
set h=%h: =0%
set hhmiss=%date:~0,4%%date:~5,2%%date:~8,2%%h%%time:~3,2%%time:~6,2%
set "folder=%date:~0,4%%date:~5,2%%date:~8,2%"
::自动创建当天文件夹(dbBack这个文件夹需要自行创建)
if not exist D:\dbBacm\%folder% (md D:\dbBacm\%folder%)
set "Ymd=%date:~0,4%%date:~5,2%%date:~8,2%0%time:~1,1%%time:~3,2%%time:~6,2%"
"C:\Program Files\MySQL\MySQL Server 5.7\bin"\mysqldump -h 127.0.0.1 --opt -u root --password="123456" --default-character-set=utf8 test > D:\dbBacm\%folder%\test_%Ymd%.sql
@echo on
`The third writing method is actually a fusion of the first two writing methods, mainly to solve the need to automatically create a data backup storage location if it does not exist.
fourth way of writing
echo 取日期、时间变量值
set "yMd=%date:~,4%%date:~5,2%%date:~8,2%"
set "hms=%time:~,2%%time:~3,2%%time:~6,2%"
set filename=%date%_%time%
echo %hms%
forfiles /p "D:\DBBack" /m test_*.sql -d -7 /c "cmd /c del /f @path"
"C:/Program Files/MySQL/MySQL Server 5.7/bin/mysqldump.exe" -uroot -p123456 --opt --default-character-set=utf8 -R -E --single-transaction test >D:/DBBack/test_%yMd%%hms%.sql
echo 导出已经完成
#pause
The fourth way of writing is mainly to add the function of Chinese comments, so as to remind everyone that the database has been backed up. This way of writing does not need to specify the relevant ip of the database.
There are many ways to write, and you can combine them as you like.