How to use Xtrabackup for incremental backup of mysql
Using Xtrabackup for mysql incremental backup
Now the xtrabackup version has been upgraded to 8.0, but it is only supported for mysql8.0
. We still use 2.4
, but 2.4
has a relatively big change compared to the previous 2.1
: innobackupex
functions are all integrated into xtrabackup
, there is only onebinary
, and for compatibility reasons, innobackupex
is a soft link of xtrabackup
, that is, xtrabackup
now supports non-Innodb table backup. And Innobackupex
will be removed in the next version (already removed in 8.0). It is recommended to replace innobackupex
with xtrabackup
. There are also some other new features. For more instructions, please see the new version of xtrabackup
.
Installation
If the installation requires dependencies, install the dependencies
wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb sudo apt-get update sudo apt-get install percona-xtrabackup-24
Set up the database for backup accounts
mysql> CREATE USER 'bkpuser'@'localhost' IDENTIFIED BY '123456'; mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'bkpuser'@'localhost'; mysql> FLUSH PRIVILEGES;
Full backup
xtrabackup --user=bkpuser --password=123456 --backup --target-dir=/data/backups/mysql # 会看到输出 200603 09:55:37 Executing UNLOCK TABLES 200603 09:55:37 All tables unlocked 200603 09:55:37 [00] Copying ib_buffer_pool to /data/backups/mysql/ib_buffer_pool 200603 09:55:37 [00] ...done 200603 09:55:37 Backup created in directory '/data/backups/mysql/' 200603 09:55:37 [00] Writing /data/backups/mysql/backup-my.cnf 200603 09:55:37 [00] ...done 200603 09:55:37 [00] Writing /data/backups/mysql/xtrabackup_info 200603 09:55:37 [00] ...done xtrabackup: Transaction log of lsn (837940114) to (837940123) was copied. 200603 09:55:37 completed OK!
Prepare the backup
xtrabackup --prepare --target-dir=/data/backups/mysql
Copy the backup
In order to demonstrate the full backup, I will just do it directly Move the data directory where my blog mysql
is stored
mv /var/lib/mysql /var/lib/mysql_bak mkdir /var/lib/mysql xtrabackup --copy-back --target-dir=/data/backups/mysql # 这样会保留原始备份 他会将当时读到my.cnf的datadir设置为恢复路径 200603 10:47:42 [01] ...done 200603 10:47:42 [01] Copying ./performance_schema/mutex_instances.frm to /var/lib/mysql/performance_schema/mutex_instances.frm 200603 10:47:42 [01] ...done 200603 10:47:42 [01] Copying ./performance_schema/events_transactions_history_long.frm to /var/lib/mysql/performance_schema/events_transactions_history_long.frm 200603 10:47:42 [01] ...done 200603 10:47:42 [01] Copying ./xtrabackup_info to /var/lib/mysql/xtrabackup_info 200603 10:47:42 [01] ...done 200603 10:47:42 [01] Copying ./ibtmp1 to /var/lib/mysql/ibtmp1 200603 10:47:42 [01] ...done 200603 10:47:42 completed OK!
The backup is successful and the blog can be accessed normally after restarting it hahahahahahahahahahaha
# 将恢复目录的属主更改一下 chown -R mysql:mysql mysql /etc/init.d/mysql start
If you do not want to back up the data during recovery, you can use the xtrabackup --move-back
command
Incremental backup
Incremental backup is based on existing data Yes, that’s OK. You need to create a full backup first, and then record the recording points at that time.
Create backup
xtrabackup --user=bkpuser --password=123456 --backup --target-dir=/data/backups/base # 基于全量备份进行增量 xtrabackup --user=bkpuser --password=123456 --backup --target-dir=/data/backups/inc1 --incremental-basedir=/data/backups/base
Check the backup type to confirm that it is an incremental backup.
root@longing:/data/backups/inc1# cat xtrabackup_checkpoints backup_type = incremental from_lsn = 837943393 to_lsn = 837943393 last_lsn = 837943402 compact = 0 recover_binlog_info = 0 flushed_lsn = 837943402
from_lsn
is the starting LSN
of the backup. For incremental backup, it mustto_lsn
is the same as the previous base
backup.
In this case you can see to_lsn
(last checkpoint LSN) and last_lsn
(last copied LSN
), which means there is some traffic on the server during the backup process.
We can test the first increase and continue to create increments before creating a few pieces of data
xtrabackup --user=bkpuser --password=123456 --backup --target-dir=/data/backups/inc2 --incremental-basedir=/data/backups/inc1
-
Prepare to restore
There are already 3 backups. We need to prepare the basic data first, and then prepare the two increments
xtrabackup --user=bkpuser --password=123456 --prepare --apply-log-only --target-dir=/data/backups/base xtrabackup --user=bkpuser --password=123456 --prepare --apply-log-only --target-dir=/data/backups/base --incremental-dir=/data/backups/inc1 xtrabackup --user=bkpuser --password=123456 --prepare --target-dir=/data/backups/base --incremental-dir=/data/backups/inc2
xtrabackup --apply-log-only
Should be used when merging all increments except the last one. Once prepared, incremental backups are identical to full backups and they can be restored in the same way.
Restore
xtrabackup --copy-back --target-dir=/data/backups/base
You can see the data inserted in the middle, awesome!
Summary of questions
Incremental backup steps
Create a basic backup
Increment under certain conditions Backup creation
Prepare all backups. All incrementals based on the basic backup are equivalent to merge operations
Finally, you can restore it directly just like the full backup.
Principle
InnoDB
will maintain a redo log file internally, we can also call it a transaction log file. The transaction log will store each InnoDB Record modification of table data. When InnoDB starts, InnoDB checks the data files and transaction logs and performs two steps: it applies (rolls forward) committed transaction logs to the data files, and rolls back modified but uncommitted data.
Xtrabackup
will remember log sequence number (LSN)
at startup, and copy all data files. The copying process takes some time, so if the data files are modified during this period, the database will be at a different point in time. At this time, xtrabackup
will run a background process to monitor the transaction log and copy the latest modifications from the transaction log. Xtrabackup
This operation must be done continuously because the transaction log will be written repeatedly in rotation, and the transaction log can be reused. Therefore, xtrabackup
has continuously recorded the modifications of each data file in the transaction log since startup. The above is the backup process of xtrabackup
.
Why not use "--apply-log-only" for the last incremental backup
The last "preparation" operation does not need to skip the rollback operation, so that the data files used for recovery are local It's taken care of. When the service starts, it will not enter the rollback phase again. If this parameter is used for the last time, the server will enter the rollback phase after it starts. So this --apply-log-only
is only used to merge increments to avoid the next increment being unavailable. See See man xtrabackup
为什么备份完后要准备备份 "prepare"
一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据文件仍处理不一致状态。他的作用是使数据文件处于一致性状态,方法是回滚未提交的事务并同步已提交的事务至数据文件。
为什么选择这个做备份?
mysqldump
备份缺点
效率较低,备份和还原速度慢,份过程中,数据插入和更新操作会被挂起
MySQL
备份工具
跨平台性差,备份时间长,冗余备份,浪费存储空间
XtraBackup
备份过程中不锁库表,适合生产环境,由专业组织Percona
提供( 改进MySQL
分支 )
XtraBackup
能对表 库进行备份吗?
当然了,只不过博主太懒了,写起来太麻烦了,不过原理都是差不多,操作也差不多,大家自己搞搞!
xtrabackup --user=bkpuser --password=123456 --backup --databases="u_test" --no-timestamp --target-dir=/data/backups/u xtrabackup --prepare --target-dir=/data/backups/u
还原
先
prepare
,利用--apply-log
的作用是通过回滚未提交的事务及同步已经提交的事务至数据文件使数据文件处于一致性状态copy
,因为是部分备份,不能直接用--copy-back
,只能手动来复制需要的库,也要复制ibdata(数据字典)
cp /data/backups/u/ibdata1 /var/lib/mysql/
cp -r u/u_test /var/lib/mysql/
The above is the detailed content of How to use Xtrabackup for incremental backup of mysql. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.
