1. Write a backup script
This is a very common Windows batch script file, and I will briefly explain the key parts:
forfiles is used to delete expired backups in the backup directory. "E:\mysql\MySQL BackUp" is the path where the backup file is located, which can be modified by yourself. backup_*.sql refers to all database backup files that start with "backup_" and are suffixed with ".sql". The number "30" after indicates that the 30-day expiration date.
Use the set command to define a variable called "Ymd", and the value of this variable is a large set of rules, simply put, the current date and time
This line of command is to call the backup tool that comes with MySQL, note that this path must be written as the path where your own "mysqldump.exe" is located, usually in the /bin directory of the MySQL installation path. This line of command is followed by a large list of parameters, let's pick a few important ones to explain:
Accounts connected to the MySQL database service must have permission to perform database backup operations. For the sake of simplicity, we use root, but it is not recommended to use root accounts in the actual production environment to avoid account and password leakage, which will cause unnecessary trouble.
This is the password to connect to the MySQL database service
This is the IP address of the server where the database service is located
This is the port number of the server where the database service is located
events parameter to back up the database to a specified file. "yumi_website" is the database that needs to be backed up, and the right side of the sign ">" is the server directory and file name where our backup file is stored.
2. Set Windows tasks
Once we have completed the above steps, we need to add the Windows scheduled task.
In Windows Server 2008, we go to the server management panel, click "Tools" in the upper right menu bar, and select "Task Scheduler" in it:
After opening the task scheduler, we click on "Create basic task" on the right:
Then, we need to fill in the name of the task, as well as the description information:
After clicking Next, we need to set the frequency of the task, and I chose "Daily":
Click "Next" again, set the time for the task execution, I chose 1 o'clock in the dead of night:
In "Next" we select "Launch program":
In the following dialog, we need to select the batch file we just wrote:
Once these steps are completed, Windows will give us an overview of the entire task:
Once you're sure, click "Done". At this point, we will see that there is a new task in the Windows task list:
At this point, the automatic backup of MySQL in the Windows environment is all set.
|