MySQL增量备份

时间:2024-07-18 14:07:29
增备1

做增量备份前,是需要进行一次完成备份的

1、做数据修改

创建一个add1.t1

t1 包含:id,name

加2条数据

id | name |
+----+------+
| 1  | add1 |
| 2  | add2 |
+----+------+

操作如下:

MySQL root@(none):(none)> show databases;
+--------------------+
| Database           |
+--------------------+
| db711              |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

5 rows in set
Time: 0.022s
MySQL root@(none):(none)> create database add1;
Query OK, 1 row affected
Time: 0.006s

MySQL root@(none):(none)> use add1;
You are now connected to database "add1" as user "root"
Time: 0.000s
MySQL root@(none):add1> create table t1 (id int,name varchar(10));
Query OK, 0 rows affected
Time: 0.024s
MySQL root@(none):add1> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | YES  |     | <null>  |       |
| name  | varchar(10) | YES  |     | <null>  |       |
+-------+-------------+------+-----+---------+-------+

2 rows in set
Time: 0.017s
MySQL root@(none):add1> insert into t1 values (1,'add1'),(2,'add2');
Query OK, 2 rows affected
Time: 0.026s
MySQL root@(none):add1> select * from t1;
+----+------+
| id | name |
+----+------+
| 1  | add1 |
| 2  | add2 |
+----+------+

2 rows in set
Time: 0.012s

MySQL root@(none):add1> show databases;
+--------------------+
| Database           |
+--------------------+
| add1               |
| db711              |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+


2、开始增量备份

xtrabackup --defaults-file=/etc/my.cnf --backup --target-dir=/data/backup/inc1/  --incremental-basedir=/data/backup/base/ -u root -pLixinyi@123 -H localhost -P 3306 --no-server-version-check

注释:

--defaults-file 		默认文件
--backup        		备份
--target-dir    		备份存放的目录,要单独存放 此时为/data/backup/inc1/
						注意此时就不要放在完全备份的文件夹(data/backup/base)中
						如果还放在这里,无法进行区分,会产生冲突
incremental-basedir  	增量备份参照的文件(参照谁进行增量)

显示如下,表示增量备份完成:
请添加图片描述

增备2:

1、进行数据修改,

在上面用到过的表add1.t1,添加数据

id 3, name “增量2”

id 4 , name “增量2”

MySQL root@(none):(none)> use add1;
You are now connected to database "add1" as user "root"
Time: 0.001s
MySQL root@(none):add1> select * from t1;
+----+------+
| id | name |
+----+------+
| 1  | add1 |
| 2  | add2 |
+----+------+

2 rows in set
Time: 0.012s
MySQL root@(none):add1> insert into t1 values (3,'增量2'),(4,'增量2');
Query OK, 2 rows affected
Time: 0.004s
MySQL root@(none):add1> select * from t1;
+----+-------+
| id | name  |
+----+-------+
| 1  | add1  |
| 2  | add2  |
| 3  | 增量2 |
| 4  | 增量2 |
+----+-------+

2、开始增量备份

xtrabackup --defaults-file=/etc/my.cnf --backup --target-dir=/data/backup/inc2/  --incremental-basedir=/data/backup/inc1/ -u root  -pLixinyi@123 -H localhost -P 3306 --no-server-version-check

--target-dir=/data/backup/inc2/ 			用了存储第二次的增量备份文件
incremental-basedir=/data/backup/inc1/  	第二次要参照第一次进行备份(增量备份参照上一次)

恢复增量备份
  • 还原的时候,要先准备好完整备份

  • 然后再还原增量备份,这样数据才是完整的

注意最后一次把增量备份恢复到全部不要加 --apply-log-only

应用完整备份:

xtrabackup --defaults-file=/etc/my.cnf --prepare --apply-log-only --target-dir=/data/backup/base/ --no-server-version-check

应用增量1:

xtrabackup --defaults-file=/etc/my.cnf --prepare --apply-log-only --target-dir=/data/backup/base/ --incremental-dir=/data/backup/inc1/  --no-server-version-check

应用增量2:

注意不要偷懒

注意,这是最后一次的增量,就 不要输入–apply-log-only

xtrabackup --defaults-file=/etc/my.cnf --prepare --target-dir=/data/backup/base/ --incremental-dir=/data/backup/inc2 --no-server-version-check

这几步 --defaults-file=““ 选项可以不用加

恢复到数据目录

全库级恢复要先停止数据库并清空数据目录

如果不这样做,当前数据库内有某张表,又还原进同样的表,会产生冲突

 systemctl stop mysqld
 rm -rf /var/lib/mysql/*
 xtrabackup --defaults-file=/etc/my.cnf --copy-back --target-dir=/data/backup/base/
 chown -R mysql.mysql /var/lib/mysql
 systemctl start mysqld
 mycli -uroot -pLxinyi@123

查看数据的完整性:

成功!

[root@localhost ~]# mycli -uroot -pLixinyi@123
Connecting to socket /var/lib/mysql/mysql.sock, owned by user mysql
MySQL
mycli 1.27.2
Home: http://mycli.net
Bug tracker: https://github.com/dbcli/mycli/issues
Thanks to the contributor - Jerome Provensal
MySQL root@(none):(none)> show  databases;
+--------------------+
| Database           |
+--------------------+
| add1               |
| db711              |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

6 rows in set
Time: 0.020s
MySQL root@(none):(none)> use add1;
You are now connected to database "add1" as user "root"
Time: 0.000s
MySQL root@(none):add1> select * from t1;
+----+-------+
| id | name  |
+----+-------+
| 1  | add1  |
| 2  | add2  |
| 3  | 增量2 |
| 4  | 增量2 |
+----+-------+

4 rows in set
Time: 0.029s