Several methods of regularly backing up mysql with windows

Reprinted from: Several methods of daily scheduled backup of mysql under windows

1. Method 1:

Save the following code, file name: backup.dat

net stop mysql
xcopy "C:/Program Files/MySQL/MySQL Server 5.0/data/piaoyi/*.*" D:/db_backup/%date:~0,10%/ /y
net start mysql

Note: If there are spaces in the path in the batch command, double quotes must be added to the path!
Then use Windows "Scheduled Tasks" to execute the batch script regularly. (For example: execute backup.bat every day at 3 am)
Explanation: The backup and recovery operations are relatively simple, the integrity is relatively high, and the control of the backup cycle is relatively flexible. This method is suitable for users who have an independent host but have no management experience with mysql. The disadvantage is that it takes up a lot of space, and mysql will be disconnected for a short time during the backup (for example: it takes about 5s for a database of about 30M).
Reference about time parameters:
%date:~0,10% //Extract year, month, and day information
%date:~-3% //Extract day of week information
%time:~0,5% //Extract time and time in time Minute
%time:~0,-3% //Extract hour, minute and second information

2. Method 2: mysqldump backup to sql file (recommended)
hypothetical environment:
MySQL installation location: C:/MySQL
database name: bbs
MySQL root password: 123456
database backup destination: D:/db_backup/

@echo off
set "Ymd=%date:~,4%%date:~5,2%%date:~8,2%"
C:/MySQL/bin/mysqldump --opt -u root --password=123456 bbs > D:/db_backup/bbs_%Ymd%.sql
@echo on
#pause

Save the above code as backup_db.bat
and use the "Scheduled Task" of Windows to execute the script regularly. (For example: execute back_db.bat at 5 am every day)
Description: This method does not need to close the database, and can name the backup file according to the time of day.
The current date is obtained by combining %date:5,2%, the effect of the combination is yyyymmdd, and the date format obtained by the date command is yyyy-mm-dd by default.
If it is not in this format, you can use the pause command to pause the command line window.
The current computer date format obtained through %date:,20%, so through%date:5,2%, you can get the two characters starting from the fifth character in the date.
For example, today is 2009-02-05, and you can get 02 through %date:~5,2%. (The subscript of the date string starts from 0)

3. Method 3: Use WinRAR to regularly backup the MySQL database.

For MySQL backup, a good way is to directly back up the Data directory of the MySQL database. The following provides a method to use WinRAR to periodically back up the Data directory.

First of all, of course, WinRAR must be installed on the computer.
Write the following command into a text file, such as backup.bat

net stop mysql
"C:/Program Files/WinRAR/WinRAR.exe" a -ag -k -r -s D:/db_backup/mysql_.rar "C:/Program Files/MySQL/MySQL Server 5.0/data/"
net start mysql

Winrar parameter explanation:
a: add file to compressed file
-ag: use current date to generate compressed file name
-k: lock compressed file
-r: recursive subdirectory
-s: create solid compressed file

After executing the above files, a compressed file such as mysql_20130803004138.rar will be generated.
Enter the control panel, open the scheduled task, and double-click "Add Scheduled Task". Find the backup.bat file just now in the scheduled task wizard, and then specify a runtime and runtime account password for this task.
The disadvantage of this method is that it takes a lot of time, it takes time to compress during backup, and MySQL disconnection takes more time than the first method, but it is good for file naming.

1. Create a db_backup folder on the D drive, and create a new backdb.bat.

2. Add the following code in backdb.bat:

echo 取日期、时间变量值set yy=%date:~0,4%
set mm=%date:~5,2%
set dd=%date:~8,2%
if /i %time:~0,2% lss 10 set hh=0%time:~1,1%
if /i %time:~0,2% geq 10 set hh=%time:~0,2%
set mn=%time:~3,2%
set ss=%time:~6,2%
set date=%yy%%mm%%dd%
set time=%hh%%mn%%ss%
set filename=%date%_%time%
"C:/Program Files (x86)/MySQL/MySQL Server 5.0/bin/mysqldump.exe" -uroot -pxxx --opt --default-character-set=utf8 -e --triggers -R --hex-blob --flush-logs -x DBNAME > C:/db_backup/DBNAME%filename%.sql
echo 导出已经完成

#pause

Here you should pay attention to your MySQL installation path and the corresponding database user name and password. I used D:/sense/mysql/bin.

3. Double-click to run this script to see if the Dbname20111207_200445.sql file will be generated, if there is no error in the script.

4. Enter the control panel, add a scheduled task in the task plan, add the batch to be executed to the task plan by browsing, and set the execution time. It is best to choose to execute every day, so that the database will be automatically backed up every day.

Guess you like

Origin blog.csdn.net/qq_39004843/article/details/105952209
Recommended