To periodically back up database data on a Windows server, you can use the combination of Windows task scheduler and database backup script. Where MySQL database backup, play a key role is mysqldump. The mysqldump command can be used in the MySQL documentation.

1 Backup Script

@echo off

echoExample Set the connection information of the MySql databasesetThe host = 127.0.0.1set port=3306
set user=root
set pass=admin

echoSet the name of the MySql database to be backed upset dbname=test

echoGet the current day date format, for example, 20200902231300setHour time = % : % ~ 0, 2if "% % time: ~ 0, 1"= ="" setHour = 0% time: ~ % 1, 1setBackup_date = % Date: Date: ~ 0, 4% % % % ~ 5, 2 Date: ~ 8, 2 hour % % % % Time: ~ % % Time: 3, 2 ~ 6, 2%echoExample Set the backup file pathset backupfile=D:\mysql\beifen\%dbname%-%backup_date%.sql


echoUse mysqldump to back up the specified MySqlechoNote use double quotation marks for paths with Spaces"C: \ Program Files \ MySQL \ MySQL Server 5.7 \ bin \ mysqldump" -h%host% -P%port% -u%user% -p%pass% -c --add-drop-table %dbname% > %backupfile%

echoForfiles /p D:\mysql\beifen /s /m *.sql/d-30 /c"cmd /c del @file /f"
Copy the code

2 Snapshot of successful backup