MySQL Database - Backup and Restore

MySQL Database - Backup and Restore

1. Overview of Mysql backup

1. The importance of data backup

Data security is paramount, and any loss of data can have serious consequences.

Reasons for data loss:

• Program errors

• Human error

• Operation error

• Disk failure

• Disasters (eg fire, earthquake) and theft

Why backup?

• It can prevent the loss of the old data due to mechanical failure and human error, such as saving the old database file in other places.

• Redundancy: There are multiple redundant copies of data, but without waiting for backup, it can only prevent the loss of old data due to mechanical failure, such as active-standby mode, database cluster.

2. Backup what must be paid attention to

• Backup content databases Binlog my.conf.

• All backup data should be kept non-database local, and multiple copies are recommended.

• Do daily recovery drills in the test environment, recovery is more important than backup.

3. Factors that must be considered during the backup process

• Data consistency

• Availability of services

Second, the classification of database backup

1. Physical backup

Backups of physical files (such as data files, log files, etc.) of the database operating system. Physical backup can be divided into offline backup (cold backup) and online backup (hot backup). This type of backup is suitable for large, important databases that need to be recovered quickly in the event of a problem.

Methods of physical backup:

1. Cold backup :

​ ▶ Backing up data files requires downtime, which is performed when the database is shut down.

​ ▶ Backup all files in the datadir directory.

2. Hot backup:

​ ▶ Online backup, the database is running, this backup method relies on the log file of the database.

​ ▶ Basically no impact on the application (application reading and writing will not block, but the performance will still be degraded, so try not to do backups on the master, and do it on the slave library).

3. Warm backup :

​ ▶ The backup operation is performed in the state where the database locks the table (not writable but readable).

​ ▶ It has a great impact on the application, usually a read lock is added.

• Logical backup: A backup of the logical components of the database (eg, database objects such as tables).

2. Logical backup

The backup of the logical components of the database (such as database objects such as tables) is expressed as the information of the logical database structure create database, createtable and other statements and contents (insert statements or split text files). This type of backup is suitable for small volumes of data where data values ​​or table structures can be edited, or data can be recreated on a different machine architecture.

- logical backup physical backup
Backup method Backup database logical content Backup database physical files
advantage The backup file is relatively small, only the data and structure in the table are backed up The recovery speed is relatively fast (the physical file recovery has basically completed the recovery)
shortcoming Recovery is slow and requires rebuilding indexes, stored procedures, etc. The backup file is relatively large (backup tablespace, including data and indexes, fragmentation)
business impact Buffer pool pollution (read all data once, read into bp), increase l/O load Increased I/O load
Representative tool mysqldump ibbackup,xtrabackup,mysqlbackup

3. The choice of backup method

• Test the backup method from the following dimensions

​ ▶ Backup speed

​ ▶ Recovery speed

​ ▶ Backup size

​ ▶ Impact on business

3. Mysql backup tool

1. ibbackup

• Official backup tool

• TOLL

• Physical backup

2. xtrabackup

• Open source community backup tool

• Open source is free, the free version of the above thing (the old version has problems, the data backed up may have problems)

• Physical backup

3. mysqldump

• The official self-contained backup tool is open source and free

• Logical backup (slow)

• do not block dml, block ddl

4. mysqlbackup

• mysql official backup tool

• The table mysqlbackupi of the innodb engine can be hot backup

• Non-innodb table mysqlbackup can only be prepared warmly

• Physical backup, fast backup and restore

• Suitable for large-scale data usage

Four, Mysql backup strategy

1. Full backup

• Perform a complete backup of data each time, that is, the backup of the entire database, database structure and file structure, which saves the database at the time the backup is completed, which is the basis of differential backup and incremental backup.

• Advantages: Backup and restore operations are simple and convenient.

• Disadvantage: There is a lot of duplication of data; takes up a lot of space: long backup and recovery time.

2. Differential backup

• Files that have been modified since the last full backup.

• Back up all files that have been modified since the last full backup. The backup time starts from the last full backup, and the amount of backup data will increase.

• When restoring data, restore only the last full backup and the most recent differential backup.

3. Incremental backup

• Only files that have been modified since the last full or incremental backup will be backed up. Taking the time of the last full backup or the last incremental backup as the point in time, only the data changes between them are backed up, so the amount of data backed up is small, the space occupied is small, and the backup speed is fast. However, during recovery, it is necessary to recover sequentially from the last full backup to the last incremental backup. If the backup data in the middle is damaged, it will cause data loss.

4. The difference between the three

Five, dedicated logical backup tool mysqldump

• mysqldump is a logical backup tool (commonly used) that comes with Mysql, which can ensure data consistency and service availability.

• Its backup principle is to connect to the MySQL database through the protocol, query the data that needs to be backed up, and convert the queried data into the corresponding insert statement. When we need to restore the data, we only need to execute these insert statements. Corresponding data restoration.

Command format:

mysqldump [选项] 库名 [表名] > 脚本名
或mysqldump [选项] --库名 [选项 表名] > 脚本名
或mysqldump [选项] --all-databases [选项] > 脚本名

Option Description :

parameter name abbreviation meaning
–host -h Server IP address
–port -P server port number
–user -in MySQL username
–password -p MySQL password
–databases -B Specify the database to be backed up
–all-databases -A Backup all databases on mysql server
–compact Compressed mode, produces less output
–comments Add comment information
–complete-insert Output the completed insert statement
–lock-tables Before backup, lock all database tables
–no-create-db/–no-create-info Suppress the generation of CREATE DATABASE statements
–force Continue backup operations when errors occur
–default-character-set Specify the default character set
–add-locks Lock database tables when backing up database tables
-single-transaction Ensure data consistency and service availability
–master-data=1|2 Usually equal to 1, record the binlog log location and file name, and append it to the backup file
–flush-logs -F Flush logs before backup
–events -AND Backup event scheduler code
–triggers -T Backup trigger
–routines -R Backup stored procedures and stored functions

END

Guess you like

Origin blog.csdn.net/tu464932199/article/details/126406080