Use percona xtraBackup8.x for MySql8.0 backup and restore (full backup, incremental backup, differential backup)

MySQL backup and restore

  • Backup goals: data consistency, service availability
  • Backup technology: physical backup (cold backup) [directly copy database files, suitable for large databases, the disadvantage is that the service needs to be stopped during operation] and logical backup (hot backup) [the backup is the SQL statement inserted into the table and database]
  • Backup method: full backup, incremental backup, differential backup
  • percona is a software that supports database hot backup

Installation
1. First, install the mysql package required by percona. You can yum list | grep mysql-community-libscheck whether it is installed. Normally, this package will be installed when installing mysql. Execute if not, yum install -y mysql-community-libs-compat.
insert image description here
2. Download the yum warehouse of percona xtraBackup: yum install -y https://repo.percona.com/yum/percona-release-latest.noarch.rpm
install percona-xtrabackup, yum -y install percona-xtrabackup-24.x86_64
and query the installation resultsyum list | grep percona-xtrabackup

Preparatory work for backup : To enable the binary log of MySQL, the operation is as follows:
it is not enabled by default, and it needs to be enabled for backup.vim /etc/my.cnf, addlog_bin=mysqlbinlog, means to start the binary log, and the file name is mysqlbinlog; add againserver-id=2, specify the serial number of the host for the cluster problem, the number Feel free to restart mysqld at last. [It is worth mentioning that the file name of the binary log of MySQL8 is mysqlbinlog, namely: mysqlbinlog.index, such as mysqlbinlog.000001, and the binary log file can be viewed by using mysqlbinlog] Log in to Mysql,mysqlbinlog -v /var/lib/mysql/mysqlbinlog.000001enter
insert image description here
show variables like 'log_bin'; view The enabled state is on.insert image description here

The full backup and restore process
backs
up xtraBackup8 using the xtrabackup command instead of innobackupex, xtrabackup --backup --target-dir=/xtrabackup/full -uroot -p'mysql-root用户的密码'[where –target-dir=/xtrabackup/full specifies the backup destination]
ls /xtrabackup/full/to query the backup result.
cat /xtrabackup/full/xtrabackup_binlog_infoYou can view the location of the binary log files.
Restoration
process: first systemctl stop mysqldstop the database
and simulate database damage here ( rm -rf /var/lib/mysql/*, rm -rf /var/log/mysqld.log)
. When restoring, check the consistency first xtrabackup --prepare --target-dir=/xtrabackup/full
and then restore xtrabackup --copy-back --target-dir=/xtrabackup/full. Then
the restored files must be re-authorized to the mysql user chown -R mysql.mysql /var/lib/mysql, and systemctl start mysqldmysqld can be started normally to complete the restore operation.

Incremental Backup Restore Process
Backup
xtrabackup -uroot -p'mysql-root用户密码' --backup --target-dir=/xtrabackup/inc1 --incremental-basedir=/xtrabackup/full Generates the first incremental backup based on the full backup.
xtrabackup -uroot -p'mysql-root用户密码' --backup --target-dir=/xtrabackup/inc2 --incremental-basedir=/xtrabackup/inc1Generate a second incremental backup based on the first incremental backup. (Subsequent incremental backups and so on.) Restore
first shut down the mysqld service and
then perform a consistency check (roll back the full package first, then roll back the first incremental package, and then roll back the second incremental package, at this time The incremental data is in the full package) [Every incremental package must be rolled back! After the rollback is complete, restore it . The final restored file needs to be re-authorized to the mysql user , and mysqld can be started normally to complete the restoration operation.systemctl stop mysqld

xtrabackup --prepare --apply-log-only --target-dir=/xtrabackup/full
xtrabackup --prepare --apply-log-only --target-dir=/xtrabackup/full --incremental-dir=/xtrabackup/inc1
xtrabackup --prepare --apply-log-only --target-dir=/xtrabackup/full --incremental-dir=/xtrabackup/inc2
xtrabackup --copy-back --target-dir=/xtrabackup/fullchown -R mysql.mysql /var/lib/mysqlsystemctl start mysqld

Differential backup restore process
backup
xtrabackup -uroot -p'mysql-root用户密码' --backup --target-dir=/xtrabackup/inc1 --incremental-basedir=/xtrabackup/full generates the first incremental backup based on the full backup, and the difference from the full backup is the differential backup.
xtrabackup -uroot -p'mysql-root用户密码' --backup --target-dir=/xtrabackup/inc2 --incremental-basedir=/xtrabackup/inc1The second incremental backup is still generated based on the full backup, and the difference from the full backup is the differential backup. (Subsequent differential backups are still based on full backups, and so on.) As shown above, two differential backups are performed for
recovery
, so rollback does not need to be the same as incremental backups. Each incremental package needs to be rolled back. You need to return to the full backup package and the last differential backup package.
xtrabackup --prepare --apply-log-only --target-dir=/xtrabackup/full
xtrabackup --prepare --apply-log-only --target-dir=/xtrabackup/full --incremental-dir=/xtrabackup/inc2
After the rollback is complete, restore it xtrabackup --copy-back --target-dir=/xtrabackup/full. The final restored file needs to be re-authorized to the mysql user chown -R mysql.mysql /var/lib/mysql, and systemctl start mysqldmysqld can be started normally to complete the restoration operation.

Guess you like

Origin blog.csdn.net/qq_41954181/article/details/131453181