Windows environment settings mysql automatic backup

windows environment settings mysql automatic backup

To achieve automatic database backup, you need the following two steps:

1. Use the backup command mysqldump provided by MySQL

2. Combine with Windows Task Scheduler

Implementation steps:

Script

Note: This script will not close the database, and the backup file can be named according to the time of each day.

Create a new directory for storing database backup files, such as: C:\mysql_backup

Note: Do not include spaces in the directory address!

Create a new batch file, such as: mysql_backup_tool.bat, the file content is as follows:

rem ******MySQL backup start******

@echo off

::删除一周前的备份数据

forfiles /p "c:\mysql_backup" /m backup_*.sql -d -7 /c "cmd /c del /f @path"

::设置时间变量

set "Ymd=%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%"

::进入mysql安装目录的bin目录下

cd C:\Program Files\MySQL\MySQL Server 5.7\bin\

::执行备份操作

mysqldump --opt --single-transaction=TRUE --user=root --password=123456 --host=127.0.0.1 --protocol=tcp --port=3306 --default-character-set=utf8 --single-transaction=TRUE --routines --events "testdba" > c:\mysql_backup\backup_%Ymd%.sql

@echo on

rem ******MySQL backup end******
forfiles 为删除过期文件的命令,-d -7 删除7天的文件;

关于时间参数的参考:

%date:~0,10%      //提取年月日信息

%date:~-3%         //提取星期几信息

%time:~0,5%         //提取时间中的时和分

%time:~0,-3%       //提取时和分和秒信息

mysqldump 为MySQL提供的备份命令,该命令所在目录为MySQL安装目录下的bin文件夹中,此处直接使用该命令的前提是该命令所在bin文件夹已被设置为系统环境变量,如未设置,则要写全路径;

 

–user=root 为 MySQL 用户名;

–password=123456 为 MySQL 密码;

–host=127.0.0.1 为 MySQL 主机名;

“testdba” 为要备份的数据库的名称;

backup_%Ymd%.sql 是根据当前时间规则生成备份文件的名称。

Add a scheduled task

Insert image description here
After opening the task scheduler, we click "Create Basic Task" on the right:
Insert image description here
Then, we need to fill in the name of the task and the description information:
Insert image description here
After clicking Next, we need to set the execution frequency of the task. I chose " every day":

Insert image description here
Click "Next" again to set the task execution time. I chose 2 o'clock in the dead of night:
Insert image description here
After continuing to "Next", we select "Start Program":
Insert image description here
In the subsequent dialog box, we need to select the batch we just wrote. Process the file:
Insert image description here
After completing these steps, Windows will show us an overview of the entire task:
Insert image description here
After confirming that it is correct, click "Finish". At this point we will see a new task added to the Windows task list:
Insert image description here
At this point, the settings for automatically backing up MySQL in the Windows environment are complete.

Guess you like

Origin blog.csdn.net/u010797364/article/details/132498186