Xtrabackup使用方法:https://blog.csdn.net/cyt0906/article/details/107310431


实验过程:

为了达到实验效果,按照下面方式构建实验环境。

1、创建数据库test1、test2、test3、test4,然后这个时候执行完整备份,此时备份文件中有4个数据库。然后创建test5、test6,并执行flush logs 生产新的binlog日志文件。

2、创建数据库test7、test8,再次执行 flush logs 生成新的binlog日志文件。

3、最后创建数据库test9、test10、test11、test12。


构建实验环境

1、
(1)创建数据库test1、test2、test3、test4

mysql> create database test1;
Query OK, 1 row affected (0.00 sec)

mysql> create database test2;
Query OK, 1 row affected (0.00 sec)

mysql> create database test3;
Query OK, 1 row affected (0.00 sec)

mysql> create database test4;
Query OK, 1 row affected (0.00 sec)


mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
| test2              |
| test3              |
| test4              |
+--------------------+
8 rows in set (0.00 sec)


mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     1294 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)


(2)执行完整备份

xtrabackup --backup --target-dir=/data/`date +%Y-%m-%d-%H-%M` --user=root --password=pwd123 --parallel=2

目录结构

[root@localhost 2020-07-14-13-52]# ll
总用量 12340
-rw-r-----. 1 root root      487 714 13:52 backup-my.cnf
-rw-r-----. 1 root root      317 714 13:52 ib_buffer_pool
-rw-r-----. 1 root root 12582912 714 13:52 ibdata1
drwxr-x---. 2 root root     4096 714 13:52 mysql
drwxr-x---. 2 root root     8192 714 13:52 performance_schema
drwxr-x---. 2 root root     8192 714 13:52 sys
drwxr-x---. 2 root root       20 714 13:52 test1
drwxr-x---. 2 root root       20 714 13:52 test2
drwxr-x---. 2 root root       20 714 13:52 test3
drwxr-x---. 2 root root       20 714 13:52 test4
-rw-r-----. 1 root root       21 714 13:52 xtrabackup_binlog_info
-rw-r-----. 1 root root      113 714 13:52 xtrabackup_checkpoints
-rw-r-----. 1 root root      513 714 13:52 xtrabackup_info
-rw-r-----. 1 root root     2560 714 13:52 xtrabackup_logfile

配置文件注释:
	(1) backup-my.cnf              —— 备份命令用到的配置选项信息;
	(2) ibdata1                    —— 备份的表空间文件;
	(3) xtrabackup_info            —— 备份信息的总合;
	(4) xtrabackup_binlog_info     —— mysql服务器当前正在使用的二进制日志文件及至备份这一刻为止二进制日志事件的位置;
	(5) xtrabackup_logfile         —— 备份的重做日志文件。
	(6) xtrabackup_checkpoints     —— 备份类型(如完全或增量)、备份状态(如是否已经为prepared状态)和LSN(日志序列号)范围信息;
#cat xtrabackup_info 
uuid = 272ca07a-c596-11ea-a2c7-000c29ff3ef3
name = 
tool_name = xtrabackup
tool_command = --backup --target-dir=/data/2020-07-14-13-52 --user=root --password=... --parallel=2
tool_version = 2.4.11
ibbackup_version = 2.4.11
server_version = 5.7.30-log
start_time = 2020-07-14 13:52:06
end_time = 2020-07-14 13:52:08
lock_time = 0
binlog_pos = filename 'mysql-bin.000001', position '1294'
innodb_from_lsn = 0
innodb_to_lsn = 2631227
partial = N
incremental = N
format = file
compact = N
compressed = N
encrypted = N

(3)创建test5 、6 并生成新的binlog日志

mysql> create database test5;
Query OK, 1 row affected (0.00 sec)


mysql> create database test6;
Query OK, 1 row affected (0.00 sec)


mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     1606 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)


mysql> flush logs;
Query OK, 0 rows affected (0.00 sec)


mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)


2、
(1)创建数据库test7、test8,并再次执行 flush logs

mysql> create database test7;
Query OK, 1 row affected (0.00 sec)

mysql> create database test8;
Query OK, 1 row affected (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      466 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> flush logs;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

3、最后创建数据库test9、test10、test11、test12。

mysql> create database test9;
Query OK, 1 row affected (0.00 sec)

mysql> create database test10;
Query OK, 1 row affected (0.01 sec)

mysql> create database test11;
Query OK, 1 row affected (0.00 sec)

mysql> create database test12;
Query OK, 1 row affected (0.00 sec)

实验环境的构建到此结束…


想象这么一个场景

在一次完整备份后,此时的binlog日志位置在 mysql-bin.000001 | 1294 (此时的binlog日志文件还没到达指定大小不会产生新的binlog日志文件),然后数据库又写入了新的数据,此时的位置是在 mysql-bin.000001 | 1606 ,然后这个binlog文件写满了,生产了新的binlog日志文件。

此时的位置是在mysql-bin.000002 | 154,然后又写入了很多数据,生成了新的mysql-bin.000003。

此时数据库又写入了一些数据,但是mysql-bin.000003未写满。

然后突然间数据库挂掉了,如何恢复


准备备份
xtrabackup --prepare --target-dir=/data/***

完整备份恢复

1、移出binlog日志文件

mv /var/lib/mysql/mysql-bin.00000* /binlog/

可以只复制恢复需要用到的binlog日志,前提是你知道需要需要哪些。

进到备份目录,可以看到备份到了那个binlog日志文件,以及它的位置

#cat xtrabackup_binlog_info 
mysql-bin.000001	1294

2、关闭mysql

systemctl stop mysqld

3、清除datadir目录

rm -rf /var/lib/mysql/* 

4、恢复完整备份

xtrabackup --copy-back --target-dir=/data/***  --parallel=2 

5、授权

chown -R mysql:mysql /var/lib/mysql

6、启动mysql

systemctl start mysqld

使用binlog恢复

1、查看完整备份的时候binlog的位置信息

#cat xtrabackup_binlog_info 
mysql-bin.000001	1294

备份到1294,所以我们要从下一个开始恢复。

找到下一个位置信息

 mysqlbinlog /var/lib/mysql/mysql-bin.000001 | less
# at 1294
#200714 13:52:43 server id 1  end_log_pos 1359 CRC32 0x45f9b8f5 	Anonymous_GTID	last_committed=7	sequence_number=8	rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 1359
#200714 13:52:56 server id 1  end_log_pos 1450 CRC32 0xb86b41f8 	Query	thread_id=10	exec_time=0	error_code=0
SET TIMESTAMP=1594783238/*!*/;
create database test5
/*!*/;
# at 1450
#200714 13:53:08 server id 1  end_log_pos 1515 CRC32 0x874fbff0 	Anonymous_GTID	last_committed=8	sequence_number=9	rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;

2、恢复mysql-bin.000001 中未备份的数据

mysqlbinlog --no-defaults --start-position='1359' /binlog/mysql-bin.000002 | mysql -u root -p

从1359之后开始恢复,包含1359


3、恢复整个mysql-bin.000002

mysqlbinlog --no-defaults  /binlog/mysql-bin.000002 | mysql -u root -p 

4、恢复mysql-bin.000003到指定位置

例如只恢复test9和test10

# at 219
#200714 13:54:01 server id 1  end_log_pos 316 CRC32 0xd670815c 	Query	thread_id=2	exec_time=0	error_code=0
SET TIMESTAMP=1594705981/*!*/;
SET @@session.pseudo_thread_id=2/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1436549152/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create database test9
/*!*/;
# at 316
#200714 13:54:04 server id 1  end_log_pos 381 CRC32 0xefb6cd20 	Anonymous_GTID	last_committed=1	sequence_number=2	rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 381
#200714 13:54:04 server id 1  end_log_pos 481 CRC32 0x00604e4b 	Query	thread_id=2	exec_time=0	error_code=0
SET TIMESTAMP=1594705984/*!*/;
create database test10
/*!*/;
# at 481
#200714 13:53:07 server id 1  end_log_pos 546 CRC32 0xb09d971c 	Anonymous_GTID	last_committed=2	sequence_number=3	rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 546
#200714 13:53:07 server id 1  end_log_pos 646 CRC32 0x5a0dce59 	Query	thread_id=2	exec_time=0	error_code=0
SET TIMESTAMP=1594705987/*!*/;
create database test11
/*!*/;
mysqlbinlog --no-defaults --stop-position='546' /binlog/mysql-bin.000003 | mysql -u root -p

恢复到546之前的一个数据,不包含546

至此完成恢复…


使用binlog的其他恢复方法

show binlog events

命令详解:https://blog.csdn.net/weixin_44377973/article/details/107122020


生成新的binlog日志文件

mysql> FLUSH LOGS; 

查看binlog日志内容

[root@localhost mysql]# mysqlbinlog --no-defaults mysql-bin.000002

基于位置的恢复

恢复到指定的地方
[root@localhost mysql]# mysqlbinlog --no-defaults --stop-position='521' mysql-bin.000002 | mysql -u root -p
#恢复到521之前的一个数据,不包含521


从指定的位置向后开始恢复
[root@localhost mysql]# mysqlbinlog --no-defaults --start-position='521' mysql-bin.000002 | mysql -u root -p #从521之后开始恢复,包含521


恢复指定范围内的数据
[root@localhost mysql]# mysqlbinlog --no-defaults --start-position='521' --stop-position='729' mysql-bin.000002  | mysql -u root -p

注释:
–no-defaults:不读取任何选项文件,其后需要直接跟日志文件或开始停止位置


基于时间点恢复

恢复到指定的时间
[root@localhost mysql]# mysqlbinlog --no-defaults --stop-datetime='2019-03-08 21:01:55' mysql-bin.000002  | mysql -u root -p


从指定的时间开始恢复
[root@localhost mysql]# mysqlbinlog --no-defaults --start-datetime='2019-03-08 21:01:53' mysql-bin.000002  | mysql -u root -p


恢复指定时间范围内的数据
[root@localhost mysql]# mysqlbinlog --no-defaults --start-datetime='2019-03-08 21:01:53' --stop-datetime='2019-03-08 21:01:55' mysql-bin.000002  | mysql -u root -p
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐