批处理 mysql5.7 备份及还原数据库(Windows)
备份数据库.bat
@setlocal enabledelayedexpansion @echo off color f0 title 备份数据库 rem 下面为自定义参数可修改 set mysqlServiceName=MySQL4506 set mysqlPort=4506 set rootUser=root set rootPwd=test1 set ip=localhost set databases=test @REM --all-databases set backFileName=familyxiao_rbac rem 下面为系统设置 set basedir=%~dp0 set bin=%~dp0\bin cd /d %~dp0 set "Ymd=%date:~,4%%date:~5,2%%date:~8,2%" set backF=%basedir%\AutoBackup IF EXIST %backF% ( echo %backF% 目录存在 ) else ( md %backF% ) %bin%\mysqldump -h %ip% --port %mysqlPort% --opt -u %rootUser% --password=%rootPwd% %databases% > %backF%\%backFileName%_%Ymd%.sql :end pause
@echo on
还原数据库.bat
@setlocal enabledelayedexpansion @echo off color f0 title 还原数据库 rem 下面为自定义参数可修改 set mysqlServiceName=MySQL4506 set mysqlPort=4506 set rootUser=root set rootPwd=test1 set ip=localhost set databases=test @REM --all-databases set backFileAllName=familyxiao_rbac_20220319 rem 下面为系统设置 set basedir=%~dp0 set bin=%~dp0\bin cd /d %~dp0 set "Ymd=%date:~,4%%date:~5,2%%date:~8,2%" set backF=%basedir%\AutoBackup IF EXIST %backF% ( echo %backF% 目录存在 ) else ( md %backF% ) %bin%\mysql -h %ip% --port %mysqlPort% -u %rootUser% --password=%rootPwd% %databases% < %backF%\%backFileAllName%.sql :end pause @echo on