xtrabackup增量备份实例

安装xtrabackup

解压xtrabackup安装包并安装

[root@localhost /]# mkdir xtrabackup
[root@localhost /]# cd xtrabackup/
[root@localhost xtrabackup]# ls
Percona-XtraBackup-2.4.22-rc99a781-el8-x86_64-bundle.tar
[root@localhost xtrabackup]# tar xf Percona-XtraBackup-2.4.22-rc99a781-el8-x86_64-bundle.tar 
[root@localhost xtrabackup]# ls
Percona-XtraBackup-2.4.22-rc99a781-el8-x86_64-bundle.tar
percona-xtrabackup-24-2.4.22-1.el8.x86_64.rpm
percona-xtrabackup-24-debuginfo-2.4.22-1.el8.x86_64.rpm
percona-xtrabackup-24-debugsource-2.4.22-1.el8.x86_64.rpm
percona-xtrabackup-test-24-2.4.22-1.el8.x86_64.rpm
percona-xtrabackup-test-24-debuginfo-2.4.22-1.el8.x86_64.rpm
[root@localhost xtrabackup]# dnf -y install percona-xtrabackup-24-2.4.22-1.el8.x86_64.rpm

将mysql的套接字文件链接到/var/lib/mysql/mysql.scok下

[root@localhost xtrabackup]# mkdir /var/lib/mysql
[root@localhost xtrabackup]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock

创建备份目录

[root@localhost /]# mkdir /backups
[root@localhost /]# mkdir /backups/all
[root@localhost /]# mkdir /backups/lnc1
[root@localhost /]# mkdir /backups/lnc2

将登录密码写在配置文件中

[root@localhost /]# vim ~/.my.cnf
[client]
user=root
password=Ha153624....
[innobackupex]
user=root
password=Ha153624....

增量备份

查看数据库内容

[root@localhost /]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 40
Server version: 5.7.33-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use hanao;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  7 | lisi        |   50 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |  100 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
10 rows in set (0.00 sec)

首先,进行全量备份

[root@localhost /]# xtrabackup --backup --target-dir /backups/all/
[root@localhost /]# ll /backups/all/
total 12340
-rw-r-----. 1 root root      487 五月    8 21:42 backup-my.cnf
drwxr-x---. 2 root root       58 五月    8 21:42 hanao
-rw-r-----. 1 root root      562 五月    8 21:42 ib_buffer_pool
-rw-r-----. 1 root root 12582912 五月    8 21:42 ibdata1
drwxr-x---. 2 root root     4096 五月    8 21:42 mysql
drwxr-x---. 2 root root     8192 五月    8 21:42 performance_schema
drwxr-x---. 2 root root     8192 五月    8 21:42 sys
-rw-r-----. 1 root root       24 五月    8 21:42 xtrabackup_binlog_info
-rw-r-----. 1 root root      135 五月    8 21:42 xtrabackup_checkpoints
-rw-r-----. 1 root root      467 五月    8 21:42 xtrabackup_info
-rw-r-----. 1 root root     2560 五月    8 21:42 xtrabackup_logfile
[root@localhost /]# cat /backups/all/xtrabackup_checkpoints 
backup_type = full-backuped
from_lsn = 0
to_lsn = 6046317
last_lsn = 6046326
compact = 0
recover_binlog_info = 0
flushed_lsn = 6046326

向表中插入数据

[root@localhost /]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 45
Server version: 5.7.33-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use hanao;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> insert into student(name,age) values('hanao',22);
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  7 | lisi        |   50 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |  100 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | hanao       |   22 |
+----+-------------+------+
11 rows in set (0.00 sec)

然后,在全备的基础上进行增备

[root@localhost /]# xtrabackup --backup --target-dir /backups/lnc1/ --incremental-basedir /backups/all/
[root@localhost /]# ll /backups/lnc1/
total 216
-rw-r-----. 1 root root    487 五月    8 21:48 backup-my.cnf
drwxr-x---. 2 root root     88 五月    8 21:48 hanao
-rw-r-----. 1 root root    562 五月    8 21:48 ib_buffer_pool
-rw-r-----. 1 root root 163840 五月    8 21:48 ibdata1.delta
-rw-r-----. 1 root root     60 五月    8 21:48 ibdata1.meta
drwxr-x---. 2 root root   4096 五月    8 21:48 mysql
drwxr-x---. 2 root root   8192 五月    8 21:48 performance_schema
drwxr-x---. 2 root root   8192 五月    8 21:48 sys
-rw-r-----. 1 root root     24 五月    8 21:48 xtrabackup_binlog_info
-rw-r-----. 1 root root    139 五月    8 21:48 xtrabackup_checkpoints
-rw-r-----. 1 root root    510 五月    8 21:48 xtrabackup_info
-rw-r-----. 1 root root   2560 五月    8 21:48 xtrabackup_logfile

再次插入数据

[root@localhost /]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 48
Server version: 5.7.33-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use hanao;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> insert into student(name,age) values('ha',22);
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  7 | lisi        |   50 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |  100 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | hanao       |   22 |
| 13 | ha          |   22 |
+----+-------------+------+
12 rows in set (0.00 sec)

然后在第一次增备的基础上做第二次增备

[root@localhost /]# xtrabackup --backup --target-dir /backups/lnc2/ --incremental-basedir /backups/lnc1/
[root@localhost /]# ll /backups/lnc1/
total 216
-rw-r-----. 1 root root    487 五月    8 21:48 backup-my.cnf
drwxr-x---. 2 root root     88 五月    8 21:48 hanao
-rw-r-----. 1 root root    562 五月    8 21:48 ib_buffer_pool
-rw-r-----. 1 root root 163840 五月    8 21:48 ibdata1.delta
-rw-r-----. 1 root root     60 五月    8 21:48 ibdata1.meta
drwxr-x---. 2 root root   4096 五月    8 21:48 mysql
drwxr-x---. 2 root root   8192 五月    8 21:48 performance_schema
drwxr-x---. 2 root root   8192 五月    8 21:48 sys
-rw-r-----. 1 root root     24 五月    8 21:48 xtrabackup_binlog_info
-rw-r-----. 1 root root    139 五月    8 21:48 xtrabackup_checkpoints
-rw-r-----. 1 root root    510 五月    8 21:48 xtrabackup_info
-rw-r-----. 1 root root   2560 五月    8 21:48 xtrabackup_logfile

恢复备份

模拟误删数据库

[root@localhost /]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 51
Server version: 5.7.33-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> drop database hanao;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

使用prepare和apply-log-only参数防止全备和增量备份日志回滚(除了最后一次的增量备份外)

[root@localhost /]# xtrabackup --prepare --apply-log-only --target-dir /backups/all/
[root@localhost /]# xtrabackup --prepare --apply-log-only --target-dir /backups/all/ --incremental-dir /backups/lnc1/

prepare最后一次增量备份这里不使用apply-log-only参数

[root@localhost /]# xtrabackup --prepare --target-dir /backups/all/ --incremental-dir /backups/lnc2/
[root@localhost /]# cat /backups/all/xtrabackup_checkpoints 
backup_type = full-prepared
from_lsn = 0
to_lsn = 6047046
last_lsn = 6047055
compact = 0
recover_binlog_info = 0
flushed_lsn = 6047055
[root@localhost /]# cat /backups/lnc1/xtrabackup_checkpoints 
backup_type = incremental
from_lsn = 6046317
to_lsn = 6046675
last_lsn = 6046684
compact = 0
recover_binlog_info = 0
flushed_lsn = 6046684
[root@localhost /]# cat /backups/lnc2/xtrabackup_checkpoints 
backup_type = incremental
from_lsn = 6046675
to_lsn = 6047046
last_lsn = 6047055
compact = 0
recover_binlog_info = 0
flushed_lsn = 6047055

恢复数据

[root@localhost /]# service mysqld stop
Shutting down MySQL.. SUCCESS! 
[root@localhost /]# rm -rf /opt/mysql_data/*
[root@localhost /]# xtrabackup --copy-back --target-dir /backups/all/
[root@localhost /]# chown -R mysql.mysql /opt/mysql_data
[root@localhost /]# ll /opt/mysql_data
total 122924
drwxr-x---. 2 mysql mysql       58 五月    8 22:01 hanao
-rw-r-----. 1 mysql mysql      562 五月    8 22:01 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 五月    8 22:01 ibdata1
-rw-r-----. 1 mysql mysql 50331648 五月    8 22:01 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 五月    8 22:01 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 五月    8 22:01 ibtmp1
drwxr-x---. 2 mysql mysql     4096 五月    8 22:01 mysql
drwxr-x---. 2 mysql mysql     8192 五月    8 22:01 performance_schema
drwxr-x---. 2 mysql mysql     8192 五月    8 22:01 sys
-rw-r-----. 1 mysql mysql       24 五月    8 22:01 xtrabackup_binlog_pos_innodb
-rw-r-----. 1 mysql mysql      511 五月    8 22:01 xtrabackup_info
-rw-r-----. 1 mysql mysql        1 五月    8 22:01 xtrabackup_master_key_id
[root@localhost /]# service mysqld start
Starting MySQL.Logging to '/opt/mysql_data/localhost.localdomain.err'.
.. SUCCESS! 
[root@localhost /]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use hanao;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  7 | lisi        |   50 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |  100 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | hanao       |   22 |
| 13 | ha          |   22 |
+----+-------------+------+
12 rows in set (0.00 sec)

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值