思路主要是这样的
1.先写个脚本,去调用mysqldump使用命令备份数据库
2.要定时
bat脚本如下:
@echo off
setlocal
REM 设置标记文件路径和名称
set "flag_file=E:\mysql-5.7.26-winx64\beifenpeizhi\backup_flag.txt"
REM 检查标记文件是否存在
if exist "%flag_file%" (
echo Script has already been executed. Exiting...
exit /b
)
REM 创建标记文件
echo Executing script... > "%flag_file%"
REM 设置备份目录和文件名
set "backup_dir=E:\mysql-5.7.26-winx64\beifenpeizhi\hnevol-data-backup"
set "Ymd=%date:~0,4%%date:~5,2%%date:~8,2%0%time:~1,1%%time:~3,2%%time:~6,2%"
set "backup_file1=%backup_dir%\hnevol_nurse_manage_%Ymd%.sql"
REM 设置MySQL的安装路径和登录信息
set "mysql_path=E:\mysql-5.7.26-winx64\bin"
set "mysql_user=root"
set "mysql_password=123456"
set "mysql_database1=hnevol_nurse_manage"
REM 创建备份目录
if not exist "%backup_dir%" mkdir "%backup_dir%"
REM 删除旧备份文件
del /q "%backup_dir%\*.sql"
REM 使用mysqldump命令备份数据库
"%mysql_path%\mysqldump" -u %mysql_user% -p%mysql_password% %mysql_database1% > "%backup_file1%"
REM 检查备份文件是否创建成功
if exist "%backup_file%" (
echo Backup completed successfully.
) else (
echo Backup failed.
)
REM 删除标记文件
del /q "%flag_file%"
endlocal
如何定时?
因为我这里用的windows,直接使用任务计划程序创建一个任务,选择触发器定时,如何选好脚本路径即可