温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

基于percona xtrabackup的innobackupex如何实现基于时间点数据库恢复

发布时间:2021-11-03 15:12:49 来源:亿速云 阅读:112 作者:柒染 栏目:MySQL数据库

基于percona xtrabackup的innobackupex如何实现基于时间点数据库恢复,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

前言

数据库在运行期间,可能是因为人为原因误操作或者出现存储故障,导致某些表不能正常访问,此时,可以使用基于时间点的恢复,采用数据库备份及二进制日志,把误操作或出现故障的数据还原回来,建议不要在生产系统中进行基于时间点的恢复,可以在一个临时的环境把数据恢复出来,然后让业务及开发确认,然后再把数据导回到生产数据库,这样也不影响数据库中其它业务的运行。

基于时间点或基于二进制日志位置的数据库恢复

1,数据库的当前数据库

mysql> select * from zxydb.t_go;

+----+------+

| a  | b    |

+----+------+

|  1 |    1 |

|  2 |    2 |

|  3 |    3 |

|  5 |    5 |

|  8 |    8 |

| 10 |   10 |

| 11 |   11 |

| 12 |   12 |

+----+------+

8 rows in set (0.00 sec)

2,构建数据库的基准备份

--构建存储数据库基准备份的目录

[root@standbygtid mysql]# mkdir -p /database_base_dir

[root@standbygtid mysql]# innobackupex  -uroot -psystem /database_base_dir --no-timestamp

[root@standbygtid mysql]# cd /database_base_dir/

[root@standbygtid database_base_dir]# ll

总用量 77868

-rw-r----- 1 root root      418 11月  5 00:56 backup-my.cnf

drwxr-x--- 2 root root     4096 11月  5 00:56 completedb

-rw-r----- 1 root root 79691776 11月  5 00:56 ibdata1

drwxr-x--- 2 root root     4096 11月  5 00:56 mysql

drwxr-x--- 2 root root     4096 11月  5 00:56 performance_schema

drwxr-x--- 2 root root     4096 11月  5 00:56 test

-rw-r----- 1 root root       18 11月  5 00:56 xtrabackup_binlog_info

-rw-r----- 1 root root      115 11月  5 00:56 xtrabackup_checkpoints

-rw-r----- 1 root root      507 11月  5 00:56 xtrabackup_info

-rw-r----- 1 root root     2560 11月  5 00:56 xtrabackup_logfile

drwxr-x--- 2 root root     4096 11月  5 00:56 zxydb

3,准备用于恢复上述的基准备份

(注:其实就是用在线日志应用到基准备份,确保数据一致性)

[root@standbygtid database_base_dir]# innobackupex  -uroot -psystem --apply-log  /database_base_dir

[root@standbygtid database_base_dir]# ll

总用量 196652

-rw-r----- 1 root root      418 11月  5 00:56 backup-my.cnf

drwxr-x--- 2 root root     4096 11月  5 00:56 completedb

-rw-r----- 1 root root 79691776 11月  5 01:01 ibdata1

-rw-r----- 1 root root 50331648 11月  5 01:01 ib_logfile0

-rw-r----- 1 root root 50331648 11月  5 01:01 ib_logfile1

-rw-r----- 1 root root 12582912 11月  5 01:01 ibtmp1

drwxr-x--- 2 root root     4096 11月  5 00:56 mysql

drwxr-x--- 2 root root     4096 11月  5 00:56 performance_schema

drwxr-x--- 2 root root     4096 11月  5 00:56 test

-rw-r----- 1 root root       18 11月  5 00:56 xtrabackup_binlog_info

-rw-r--r-- 1 root root       19 11月  5 01:01 xtrabackup_binlog_pos_innodb

-rw-r----- 1 root root      115 11月  5 01:01 xtrabackup_checkpoints

-rw-r----- 1 root root      507 11月  5 00:56 xtrabackup_info

-rw-r----- 1 root root  8388608 11月  5 01:01 xtrabackup_logfile

drwxr-x--- 2 root root     4096 11月  5 00:56 zxydb

4,查看上述基准备份对应的二进制日志及位置

(注:如下基于时间点恢复就是从这个二进制日志及位置开始恢复到某个时间点,很重要哟)

[root@standbygtid database_base_dir]# more xtrabackup_binlog_info

binlog.000001120

[root@standbygtid database_base_dir]# 

5,为了更好模拟基于时间点恢复,切换二进制日志,产生新的二进制日志

mysql> flush logs;

Query OK, 0 rows affected (0.01 sec)

6,查看当前的二进制日志及位置

mysql> show master status;

+---------------+----------+--------------+------------------+-------------------+

| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+---------------+----------+--------------+------------------+-------------------+

| binlog.000002 |      337 |              |                  |                   |

+---------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

--共计2个二进制日志

mysql> show binary logs;

+---------------+-----------+

| Log_name      | File_size |

+---------------+-----------+

| binlog.000001 |       164 |

| binlog.000002 |       337 |

+---------------+-----------+

2 rows in set (0.00 sec)

7,产生数据变更

mysql> use zxydb;

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 t_go;

+----+------+

| a  | b    |

+----+------+

|  1 |    1 |

|  2 |    2 |

|  3 |    3 |

|  5 |    5 |

|  8 |    8 |

| 10 |   10 |

| 11 |   11 |

| 12 |   12 |

+----+------+

8 rows in set (0.00 sec)

mysql> insert into t_go select 13,13;

Query OK, 1 row affected (0.00 sec)

Records: 1  Duplicates: 0  Warnings: 0

mysql> commit;

Query OK, 0 rows affected (0.00 sec)

8,查看当前二进制日志对应的事件信息

mysql> show binlog events in 'binlog.000002';

+---------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------+

| Log_name      | Pos | Event_type  | Server_id | End_log_pos | Info                                                                 |

+---------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------+

| binlog.000002 |   4 | Format_desc |         1 |         120 | Server ver: 5.6.25-enterprise-commercial-advanced-log, Binlog ver: 4 |

| binlog.000002 | 120 | Query       |         1 |         201 | BEGIN                                                                |

| binlog.000002 | 201 | Query       |         1 |         306 | use `zxydb`; insert into t_go select 13,13                           |

| binlog.000002 | 306 | Xid         |         1 |         337 | COMMIT /* xid=41 */                                                  |

+---------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------+

4 rows in set (0.00 sec)

9,继续变化数据

mysql> insert into t_go select 18,18;

Query OK, 1 row affected (0.02 sec)

Records: 1  Duplicates: 0  Warnings: 0

mysql> commit;

Query OK, 0 rows affected (0.00 sec)

mysql> show binlog events in 'binlog.000002';

+---------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------+

| Log_name      | Pos | Event_type  | Server_id | End_log_pos | Info                                                                 |

+---------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------+

| binlog.000002 |   4 | Format_desc |         1 |         120 | Server ver: 5.6.25-enterprise-commercial-advanced-log, Binlog ver: 4 |

| binlog.000002 | 120 | Query       |         1 |         201 | BEGIN                                                                |

| binlog.000002 | 201 | Query       |         1 |         306 | use `zxydb`; insert into t_go select 13,13                           |

| binlog.000002 | 306 | Xid         |         1 |         337 | COMMIT /* xid=41 */                                                  |

| binlog.000002 | 337 | Query       |         1 |         418 | BEGIN                                                                |

| binlog.000002 | 418 | Query       |         1 |         523 | use `zxydb`; insert into t_go select 18,18                           |

| binlog.000002 | 523 | Xid         |         1 |         554 | COMMIT /* xid=48 */                                                  |

+---------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------+

7 rows in set (0.00 sec)

10,因为基于时间点恢复要用于二进制日志,备份下述的二进制文件到另一个目录

(注:即使二进制日志非常重要,一定要定期进行备份,不然真要进行基于时间点恢复,而所需要的二进制日志又没有了,哪就会导致数据损失了)

[root@standbygtid database_base_dir]# cd /var/lib/mysql

[root@standbygtid mysql]# ll

总用量 188464

-rw-rw---- 1 mysql mysql       56 11月  4 23:52 auto.cnf

-rw-rw---- 1 mysql mysql      164 11月  5 01:06 binlog.000001

-rw-rw---- 1 mysql mysql      554 11月  5 01:12 binlog.000002

-rw-rw---- 1 mysql mysql       32 11月  5 01:06 binlog.index

drwxr-x--- 2 mysql mysql     4096 11月  4 23:48 completedb

-rw-r----- 1 mysql mysql 79691776 11月  5 01:12 ibdata1

-rw-r----- 1 mysql mysql 50331648 11月  5 01:12 ib_logfile0

-rw-r----- 1 mysql mysql 50331648 11月  4 23:48 ib_logfile1

-rw-r----- 1 mysql mysql 12582912 11月  4 23:48 ibtmp1

drwxr-x--- 2 mysql mysql     4096 11月  4 23:48 mysql

srwxrwxrwx 1 mysql mysql        0 11月  4 23:52 mysql.sock

-rw------- 1 root  root       159 11月  4 23:52 nohup.out

drwxr-x--- 2 mysql mysql     4096 11月  4 23:48 performance_schema

-rw-r----- 1 mysql root      2508 11月  4 23:52 standbygtid.err

-rw-rw---- 1 mysql mysql        6 11月  4 23:52 standbygtid.pid

drwxr-x--- 2 mysql mysql     4096 11月  4 23:48 test

drwxr-x--- 2 mysql mysql     4096 11月  4 23:48 zxydb

[root@standbygtid mysql]# mysqlbinlog  binlog.000001 binlog.000002 --start-position=120 --stop-position=337 > /base_binlog_pos.sql

[root@standbygtid mysql]# 

11,关闭数据库

 mysqladmin -uroot -psystem shutdown

12,物理删除数据文件

[root@standbygtid mysql]# pwd

/var/lib/mysql

[root@standbygtid mysql]# rm -rf *

13,还原上述的备份到数据库的数据文件目录

[root@standbygtid mysql]# innobackupex  --datadir=/var/lib/mysql --copy-back /database_base_dir

14,授权数据库的数据文件目录

[root@standbygtid mysql]# chown -Rf mysql:mysql *

[root@standbygtid mysql]# ll

总用量 188448

drwxr-x--- 2 mysql mysql     4096 11月  5 01:49 completedb

-rw-r----- 1 mysql mysql 79691776 11月  5 01:49 ibdata1

-rw-r----- 1 mysql mysql 50331648 11月  5 01:49 ib_logfile0

-rw-r----- 1 mysql mysql 50331648 11月  5 01:49 ib_logfile1

-rw-r----- 1 mysql mysql 12582912 11月  5 01:49 ibtmp1

drwxr-x--- 2 mysql mysql     4096 11月  5 01:49 mysql

drwxr-x--- 2 mysql mysql     4096 11月  5 01:49 performance_schema

drwxr-x--- 2 mysql mysql     4096 11月  5 01:49 test

-rw-r----- 1 mysql mysql       19 11月  5 01:49 xtrabackup_binlog_pos_innodb

-rw-r----- 1 mysql mysql      507 11月  5 01:49 xtrabackup_info

drwxr-x--- 2 mysql mysql     4096 11月  5 01:49 zxydb

15,删除上述与xtrabackup相关的文件

[root@standbygtid mysql]# rm -rf xtrabackup_*

16,重启数据库

[root@standbygtid mysql]# nohup mysqld_safe  --user=mysql&

17,基于时间点恢复

(注:我们就恢复到二进制日志的 337,见上述的第9步)

[root@standbygtid mysql]# mysql -uroot -psystem < /base_binlog_pos.sql

Warning: Using a password on the command line interface can be insecure.

[root@standbygtid mysql]# 

mysql> select * from zxydb.t_go;

+----+------+

| a  | b    |

+----+------+

|  1 |    1 |

|  2 |    2 |

|  3 |    3 |

|  5 |    5 |

|  8 |    8 |

| 10 |   10 |

| 11 |   11 |

| 12 |   12 |

| 13 |   13 |

+----+------+

9 rows in set (0.00 sec)

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI