How to backup and restore MySQL database data

Table of contents

1. MySQL backup

Backup method

full backup

differential backup

incremental backup

2. Common backup methods

Physical cold standby

Special backup tools mysqldump or mysqlhotcopy

Enable binary logs for incremental backups

Third-party tool backup

3. MySQL full backup

4. Database complete backup classification

Physical cold backup and recovery

mysqldump backup and recovery

5. Physical cold backup and recovery

Check out the current database

First shut down the database service

Installation of related compression tools

Unzip data files for backup

Remove data to another file

Next we will decompress and restore the data

Switch to the directory to see if the files are restored

Start the service to see if it can start normally

Check if the data exists

6. mysqldump backup and recovery (warm backup)

To perform a full backup specify the complete repository

Make a full backup of all databases in the MySQL server

Full backup of some tables in the specified library

How to view backup files

Perform data recovery

Use source to restore the database

Simulating a database failure

Restore database

Check if the data is recovered

Use mysql statement to restore database

Perform database recovery

Log in to the database to see if it is restored

7. Incremental backup

First enable the binary log function

Restart the database MySQL service

Back up a binary log file

View binary log file contents

Refresh generates new binary log files

Enter the database to create new data

Create new data to add binary log file content

View new binary log files


1. MySQL backup

The main purpose of backup is disaster recovery. Backup can also test applications, roll back data modifications, and query historical data. , auditing, etc., and logs play a very important role in restoring backups.

The default storage location of MySQL logs is the log file in the /etc/local/mysql/data directory.

Data backup can be divided into physical backup and logical backup. Physical backup is a backup of the physical files of the database operating system (such as data files and log files, etc.). This type of backup is suitable for large databases that need to be quickly restored when problems occur. important database

Cold backup (offline backup): performed when the database is closed (tar)

Hot backup (online backup): the database is running and relies on the database's log files (MySQLbackup)

Warm backup: Perform backup operation (MySQLdump) while the database is locked (not writable but readable).

Backup method

There are three backup methods, including full backup, differential backup, and incremental backup.

full backup

Each time a complete backup of data is performed, that is, a backup of the entire database, database structure, and file structure. What is saved is the database at the time when the backup is completed. It is the basis for differential backup and incremental backup. The backup and recovery operations of the full backup are very simple and convenient. , but there is a lot of duplication in the data and it takes up a lot of disk space, and the backup time is also very long.

Performing a full backup every time will cause the backup file to occupy a huge space and contain a large amount of duplicate data. When restoring, just use the full backup file directly.

differential backup

Back up all files that have been modified since the last full backup. The backup time point is from the last full backup, and the amount of backup data will become larger and larger. When restoring data, you only need to restore the last full backup and the best differential backup

Each differential backup will back up the data after the last full backup, and duplicate data may appear. When restoring, first restore the full backup data, and then restore the differential backup data.

incremental backup

Only those files that have been modified since the last full backup or incremental backup will be backed up. The time of the last full backup or the last incremental backup is used as the time point. Only the data changes during the backup period, so the amount of data backed up is small. , takes up little space and has fast backup speed. However, when restoring, all increments between the last full backup and the last incremental backup need to be restored in sequence. If the backup data in the middle is damaged, data will be lost. Each incremental backup is a backup. Completed full backup last time

Each incremental backup backs up the data after the last full backup or incremental backup. There will be no duplication of data, and it will not occupy additional disk space to restore data. Full backups and incremental backups need to be restored in order. The data

2. Common backup methods

Physical cold standby

The database is closed during backup and the database file (tar) is packaged directly. The backup speed is fast and the recovery is also the simplest.

Special backup tools mysqldump or mysqlhotcopy

Mysqldump, the commonly used logical backup tool mysqlhotcopy, only has the ability to back up MyISAM and ARCHIVE tables.

Enable binary logs for incremental backups

To perform incremental backup, you need to refresh the binary log

MySQL supports incremental backup, and binary logs must be enabled when performing incremental backup. Binary log files provide users with a copy of the information needed to recover database changes made after the backup point was performed. If you perform an incremental backup (containing data modifications that occurred since the last full or incremental backup), you need to refresh the binary log.

Third-party tool backup

Free MySQL hot backup software Percona XtraBackup mysqlbackup

3. MySQL full backup

  • It is a backup of the entire database, database structure and file structure

  • What is saved is the database at the time when the backup is completed.

  • It is the basis of differential backup and incremental backup

  • MySQL full backup pros and cons

1. Advantages: Backup and recovery operations are simple and convenient

2. Disadvantages: There is a lot of duplication of data, which takes up a lot of backup space and takes a long time to backup and restore.

4. Database complete backup classification

Physical cold backup and recovery

Close the MySQL database and use the tar command to directly package the database folder and directly replace the existing MySQL directory.

mysqldump backup and recovery

The backup tool that comes with MySQL can easily back up MySQL. You can export the specified libraries and tables as SQL scripts. Use the command mysq| to import the backed up data.

5. Physical cold backup and recovery

Check out the current database

Command: show databases;

show tables;

First shut down the database service

Perform cold backup again

Instruction: systemctl stop firewalld

Installation of related compression tools

Command: yum -y install xz

Unzip data files for backup

命令:tar Jcvf /opt/mysql_all_$(date +%F).tar.xz /usr/local/mysql/data

Remove data to another file

Instruction: mv /usr/local/mysql/data /opt

Next we will decompress and restore the data

Instruction: mkdir /usr/local/mysql/data

tar Jxvf /opt/mysql_all_2023-07-18.tar.xz -C /usr/local/mysql/data

Switch to the directory to see if the files are restored

Instruction: cd /usr/local/mysql/data

Start the service to see if it can start normally

Command: systemctl start mysqld.service

Check if the data exists

Command: show databases;

show tables; 

6. mysqldump backup and recovery (warm backup)

To perform a full backup specify the complete repository

Command: mysqldump -u root -p [password] --databases [library name] > [backup path file name].sql

Make a full backup of all databases in the MySQL server

Command: mysqldump -u root -p[password] --all-databases > [backup path file name].sql

Full backup of some tables in the specified library

Command: mysqldump -u root -p [password] library name [table name 1] [table name 2] ... > [backup path file name].sql

How to view backup files

Command: grep -v "^--" /opt/[file name].sql | grep -v "^/" | grep -v "^$"

Perform data recovery

For files exported using mysqldump, you can use the import method
① source command
② mysql command

Use source to restore the database

Log in to the database first

Command: mysql -u [username] -p [password]

Simulating a database failure

Command: drop database [database name];

Restore database

Command: source [full path of backup file].sql;

Check if the data is recovered

Command: show databases;

show tables;

Use mysql statement to restore database

Simulate database failure again to lose data

Command: drop database [database name];

Perform database recovery

Exit the database and restore from the command line

Command: mysql -u[username] -p[password] <[full path to backup file].sql

Log in to the database to see if it is restored

Command: mysql -u[username] -p[password]

show databases;

show tables;

7. Incremental backup

First enable the binary log function

Incremental backup must be based on full backup, so before doing incremental backup, make a full backup of the current data in the database to ensure the data integrity at the beginning of the backup.

Command: vim /etc/my.cnf

Restart the database MySQL service

Command: systemctl restart mysqld

Back up a binary log file

Command: cp /usr/local/mysql/data/mysql-bin.000001 /opt

View binary log file contents

命令:mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000001

Parts of the binary log that need attention

1. at: starting position point

2. end_log_pos: end position

3. Timestamp: 210712 11:50:30

4. SQL statement

Refresh generates new binary log files

Instruction: mysqladmin -uroot -p flush-logs

Enter the database to create new data

Command: mysql -u[username] -p[password]

Create new data to add binary log file content

Instruction: create database lll;

View new binary log files

命令:mysqlbinlog --no-defaults --base64-output=decode-rows -v /usr/local/mysql/data/mysql-bin.000002

 

Guess you like

Origin blog.csdn.net/Liu_Fang_Hong/article/details/131773530