Regular backup of MySQL database mysqldump command

1. Build MySQL service

On the virtual machine used for backup, set up a MySQL service consistent with the version of the database to be backed up.

Backup itself does not require the full MySQL service. You only need to copy a few files from the database that needs to be backed up to complete the backup. However, for the following two reasons, it is recommended to build a MySQL service on the virtual machine used for backup (choose to directly back up the database virtual machine, which can be ignored):

1. Different MySQL versions require slightly different files when using mysqldump backup. Some only need mysqldump.exe, and some higher versions of MySQL also need to be used together with msvcp120.dll and msvcr120.dll. So, you don't need to try and find out what files the current version of the database needs.

2. The meaning of data backup is for recovery. If the backup database has a set of MySQL services, the recovery can be performed on this machine now, and it is considered for subsequent use.

2. Create a new data backup user

Create a new data backup user in the database to be backed up, and set minimum permissions.

Set up a user on the local or web version database tool, and only give the user the permission to query the current database. Other operation rights are not granted.

3. Create a new backup bat under MySQL/bin/

Confirm the existence of mysqldump.exe under MySQL/bin/, and create backup_database.bat in its same-level directory.

Here database can be changed to the name of the database you need to back up.

The code content in backup_database.bat is as follows:

mysqldump -h1.1.1.1 -uroot -proot --lock-tables --databases database_name > c:\backup\database_name\%date:~0,4%_%date:~5,2%_%date:~8,2%.sql

Notes:

mysqldump command = mysqldump [comment command] the database to be backed up > the file to be backed up

mysqldump backup using the above comment command to back up the result = original database building SQL + original database building table SQL and index + original database data are all converted to the form of insert statement

-h = -host is the intranet IP of the database, and the IP can be written together with -h, the same below.

-u = -user is the user name of the backup database

-p = -password is the user password of the backup database

--lock-tables = After setting, the backup database starts to lock (lock) the current database, and unlocks (unlock) after the backup is completed. The reason for not using the --add-locks command is: --add-locks only locks the current backup table, other tables are not locked. If there is a one-time insertion or modification of multiple tables in the business, after the previous table is inserted, the latter table is being backed up and locked, then although the backup is completed, the data will still be out of waiting to be executed, but the table business under the backup The time may be incomplete and uncorresponding. Therefore, instead of reducing the number of tables locked at the same time for fear of affecting the business database, it is better to lock the entire database for data accuracy.

*The execution efficiency of the mysqldump command is very high. For a database of 10GB+, the backup can take up to one hour.

--databases = After setting, database building SQL will be generated in the backup file to facilitate subsequent data recovery.

database_name = the name of the database to be backed up

%date:~0,4%_%date:~5,2%_%date:~8,2%.sql = The backup is 2022_05_15.sql file. %date:~0,4% means 2022 (year), %date:~5,2% means 05 (month), %date:~8,2% means 15 (days)

pause; = Special attention, do not use "pause;" here, because the bat for data backup needs to set up scheduled tasks. After setting pause, the scheduled tasks will be executed repeatedly because they cannot be executed, causing problems such as lag. Remember!

4. Set up scheduled tasks

Here we take the Windows Server virtual machine as an example. For the specific setting and verification methods, please refer to the article "Windows Server Setting Scheduled Tasks and Checking Their Running Status".

Generally, database backups are performed in the middle of the night every day when the business is almost unaffected.

5. Regularly check and clean up backup library files that are no longer needed

For daily data backup, assuming the database is 10GB+, then 100 days (more than three months) will be 1000GB+, which is about 1TB. Generally, 1TB is relatively large for a virtual machine (maybe just poverty limits my imagination), Therefore, you should check and delete the backup virtual machine at least once a month, and you can save the last day of each month or the last day of each year.

In short, if you don’t delete the old one, there is no place to back up the existing database. If something goes wrong one day and you find that the database is not backed up, you can prepare xxxxx.

Guess you like

Origin blog.csdn.net/HoD_DoH/article/details/124788929