批处理文件备份数据库表

最近在定时备份mysql数据库时使用到了批处理文件进行定时导出,并保存为SQL脚本。之所以没有采用数据库备份计划,是因为我要备份数据库中的指定的两个表,并且不是整表备份,是根据条件进行筛选后备份。
原理是调用mysql的mysqldump.exe。
此处是在批处理里面定义函数backup进行备份,这里要分别备份三个门店的数据并且单独存放。
call:backup 1019
call:backup 9001
call:backup 001。

函数内容示例:
:backup
md “D:\BACKUP\%1\%1”
“C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump.exe” –opt -h%i% -u%u% -p%p% –skip-lock-tables %d% hs_esl -w” store_id=(select id from hs_store where store_code=%1)” >D:/BACKUP/%1/%1/hs_esl.sql
“C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump.exe” –opt -h%i% -u%u% -p%p% –skip-lock-tables %d% hs_goods_esl_pr -w” store_id=(select id from hs_store where store_code=%1)” >D:/BACKUP/%1/%1/hs_goods_esl_pr.sql
goto:eof

最后将写好的批处理文件加入windows系统的任务计划程序中。win10系统为windows管理工具>任务计划程序>任务计划程序库>新建文件夹>创建任务。在创建任务界面录入名称,新建触发器,新建操作就可以了。
@echo off
set i=127.0.0.1
set u=root
set p=123456
set d=dbname
echo.going to execute myDosFunc with different arguments
call:backup 1019
call:backup 9001
call:backup 001
@exit
:backup
md “D:\BACKUP\%1\%1”
“C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump.exe” –opt -h%i% -u%u% -p%p% –skip-lock-tables %d% hs_esl -w” store_id=(select id from hs_store where store_code=%1)” >D:/BACKUP/%1/%1/hs_esl.sql
“C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump.exe” –opt -h%i% -u%u% -p%p% –skip-lock-tables %d% hs_goods_esl_pr -w” store_id=(select id from hs_store where store_code=%1)” >D:/BACKUP/%1/%1/hs_goods_esl_pr.sql
goto:eof
“`

猜你喜欢

转载自blog.csdn.net/mubing870825/article/details/78902246