MySQL 的物理恢复

跟逻辑恢复一样,物理恢复通常也支持两种恢复方式:完全恢复、不完全恢复,本小节结合xtrabackup开源工具,介绍这两种恢复方式的操作步骤。

1. 完全恢复

MySQL 中,物理备份的完全恢复相对比较简单,下面来看个案例:

实际案例:全量备份恢复

恢复数据一致性, 通过回滚未提交的事务及同步已经提交的事务至数据文件,使用得数据文件处于一致性状态。 innobackupex 通常还可以使用 --user-memory 选项来指定其可以使用的内存的大小,如果有足够的内存空间可用,可以多划分一些内存给 prepare 的过程,以提高其完成备份的速度。

[root@localhost ~]# innobackupex --apply-log /mysql/dbbackup/
200824 06:29:44 innobackupex: Starting the apply-log operation

IMPORTANT: Please check that the apply-log run completes successfully.
           At the end of a successful apply-log run innobackupex
           prints "completed OK!".

innobackupex version 2.4.9 based on MySQL server 5.7.13 Linux (x86_64) (revision id: a467167cdd4)
xtrabackup: cd to /mysql/dbbackup/
xtrabackup: This target seems to be not prepared yet.
InnoDB: Number of pools: 1
xtrabackup: xtrabackup_logfile detected: size=8388608, start_lsn=(14533834254)

......

InnoDB: 5.7.13 started; log sequence number 14533834773
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 14533834792
200824 06:30:35 completed OK!

恢复备份文件至数据目录:

[root@localhost ~]# service mysqld stop
Shutting down MySQL.... SUCCESS!

[root@localhost ~]# mv /mysql/data/ /mysql/data_bak

[root@localhost ~]# mkdir /mysql/data

[root@localhost ~]# innobackupex --default-file=/etc/my.cnf --copy-back --rsync /mysql/dbbackup/
200824 06:44:20 innobackupex: Starting the copy-back operation

IMPORTANT: Please check that the copy-back run completes successfully.
           At the end of a successful copy-back run innobackupex
           prints "completed OK!".

innobackupex version 2.4.9 based on MySQL server 5.7.13 Linux (x86_64) (revision id: a467167cdd4)
200824 06:44:20 [01] Copying ib_logfile0 to /mysql/data/ib_logfile0
200824 06:44:28 [01]        ...done

......

200824 06:44:59 [01] Creating directory /mysql/data/2020-09-01_06-42-14
200824 06:44:59 [01] ...done.200901 06:44:59 completed OK!

[root@localhost ~]# chown -R mysql:mysql /mysql/data
[root@localhost ~]# service mysqld start
Starting MySQL.. SUCCESS! 

恢复后检查数据一致性:

mysql> use tempdb
Database changed
mysql> select * from customer;
+----+-----------+------------+------------+--------+---------+
| id | last_name | first_name | birth_date | gender | balance |
+----+-----------+------------+------------+--------+---------+
|  1 | 111       | 111        | NULL       | 1      |      10 |
|  2 | 222       | 222        | 2020-07-15 | 1      |      20 |
+----+-----------+------------+------------+--------+---------+
2 rows in set (0.01 sec)

2. 不完全恢复

逻辑恢复中,mysqlbinlog 的不完全恢复方法,同样适用于物理备份的不完全恢复。

1.13 点,运维人员误删除表 customer,可以用备份和 binlog 日志恢复到故障前(中午12点,物理备份数据库)

从备份文件目录找到 binlog 位置文件 xtrabackup_binlog_info,查看备份结束时 binlog 的位置:

[root@localhost ~]# cd /mysql/dbbackup
[root@localhost ~]# ls -l
-rw-r-----  1 root root        433 Aug 24 12:11 backup-my.cnf
-rw-r-----  1 root root      42884 Aug 24 12:11 ib_buffer_pool
-rw-r-----  1 root root  104857600 Aug 24 12:11 ibdata1
-rw-r-----  1 root root 1048576000 Aug 24 12:11 ib_logfile0
-rw-r-----  1 root root 1048576000 Aug 24 12:11 ib_logfile1
-rw-r-----  1 root root 1048576000 Aug 24 12:11 ib_logfile2
-rw-r-----  1 root root   12582912 Aug 24 12:11 ibtmp1
drwxr-x---  2 root root       4096 Aug 24 12:11 mysql
drwxr-x---  2 root root       4096 Aug 24 12:11 performance_schema
drwxr-x---  2 root root      12288 Aug 24 12:11 sys
drwxr-x---  2 root root       4096 Aug 24 12:11 tempdb
-rw-r-----  1 root root        166 Aug 24 12:11 xtrabackup_binlog_info
-rw-r--r--  1 root root         21 Aug 24 12:11 xtrabackup_binlog_pos_innodb
-rw-r-----  1 root root        121 Aug 24 12:11 xtrabackup_checkpoints
-rw-r-----  1 root root        703 Aug 24 12:11 xtrabackup_info
-rw-r-----  1 root root    8388608 Aug 24 12:11 xtrabackup_logfile

[root@localhost ~]# cat xtrabackup_binlog_info 
mysql-bin.000022	190

查看当前的 binlog 文件

mysql> show master logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000018 | 245704317 |
| mysql-bin.000019 |      1078 |
| mysql-bin.000020 |       781 |
| mysql-bin.000021 |       483 |
| mysql-bin.000022 |       757 |
| mysql-bin.000023 |       190 |
+------------------+-----------+
6 rows in set (0.00 sec)

恢复备份文件(参考完全备份步骤),然后使用 binlog 日志跳过故障时间点,完成恢复

-- 恢复备份文件(参考完全备份步骤)
完全恢复

-- 使用binlog日志恢复到故障前
[mysql@localhost ~]$ mysqlbinlog --start-position="190" --stop-datetime="2020-08-24 12:59:59" mysql-bin.000022 mysql-bin.000023 | mysql -uroot -p tempdb
Enter password:

-- 使用binlog日志跳过故障时间点
[mysql@localhost ~]$ mysqlbinlog --start-datetime="2020-08-24 13:01:00" mysql-bin.000022 mysql-bin.000023 | mysql -uroot -p tempdb
Enter password:

3. 小结

本小节通过 xtrabackup 工具,介绍了物理恢复的两种恢复方式:完全恢复、不完全恢复。

  • 物理备份的完全恢复相对简单,恢复最新的全备文件
  • 不完全恢复相对要复杂,分为基于时间点的恢复和基于位置的恢复,通常适用于人为误操作的恢复场景