1 @echo off 2 rem MySQL备份脚本 支持全备份 3 rem 增量备份需要MySQL服务加--log-bin参数执行 4 rem 数据还原 mysql -u root -p < backup.sql 5 rem ===================== 6 rem 全局变量 7 set dump=mysqldump.exe 8 set rand=%random% 9 rem ===================== 10 11 rem 检测MySQL路径是否添加到PATH变量 12 echo =========================================== 13 echo "数据还原命令: mysql -u root -p < backup.sql" 14 echo =========================================== 15 echo. 16 echo 检测 MySQL 是否添加到环境变量... 17 echo %PATH% | findstr MySQL > nul 18 19 if %ERRORLEVEL% EQU 0 goto dobackup 20 if %ERRORLEVEL% EQU 1 goto notinpath 21 22 :dobackup 23 echo. 24 echo ==备份选项== 25 set /p uname=输入用户名: 26 echo. 27 echo 是否备份所有数据库? 28 choice 29 if %ERRORLEVEL% EQU 1 goto backupalldb 30 if %ERRORLEVEL% EQU 2 goto backupspecialdb 31 pause 32 exit 33 34 :backupalldb 35 echo. 36 echo 备份所有数据库 37 %dump% -u root -p --all-databases > backup_%DATE%_%rand%.sql 38 echo. 39 if %ERRORLEVEL% EQU 2 (del backup_%DATE%_%rand%.sql && goto pwderror) 40 echo 备份完成,备份文件为 backup_%DATE%_%rand%.sql 41 pause 42 exit 43 44 :backupspecialdb 45 echo. 46 echo 备份指定数据库 47 set /p db=输入数据库名称: 48 %dump% -u root -p %db% > "%db%_%DATE%_%rand%".sql 49 echo. 50 if %ERRORLEVEL% EQU 2 (del %db%_%DATE%_%rand%.sql && goto pwderror) 51 echo 备份完成,备份文件为 %db%_%DATE%_%rand%.sql 52 pause 53 exit 54 55 :pwderror 56 echo. 57 echo ***密码错误! 58 echo. 59 echo 任意键退出程序 60 pause>nul 61 exit 62 63 :notinpath 64 echo **MySQL路径不在环境变量中 65 echo. 66 echo 检测当前工作路径... 67 dir | findstr %dump% > nul 68 if %ERRORLEVEL% EQU 0 goto dobackup 69 if %ERRORLEVEL% EQU 1 goto notinbindir 70 pause 71 exit 72 73 :notinbindir 74 echo **当前路径不包含 %dump% 75 echo. 76 echo 请把MySQL路径添加到环境变量,或将该脚本复制到MySQL/bin 文件夹下 77 echo. 78 echo 任意键退出程序 79 pause>nul 80 exit
()