MySQL 的物理恢復(fù)
跟邏輯恢復(fù)一樣,物理恢復(fù)通常也支持兩種恢復(fù)方式:完全恢復(fù)、不完全恢復(fù),本小節(jié)結(jié)合xtrabackup開源工具,介紹這兩種恢復(fù)方式的操作步驟。
1. 完全恢復(fù)
MySQL 中,物理備份的完全恢復(fù)相對比較簡單,下面來看個案例:
實(shí)際案例:全量備份恢復(fù)
恢復(fù)數(shù)據(jù)一致性, 通過回滾未提交的事務(wù)及同步已經(jīng)提交的事務(wù)至數(shù)據(jù)文件,使用得數(shù)據(jù)文件處于一致性狀態(tài)。 innobackupex 通常還可以使用 --user-memory 選項(xiàng)來指定其可以使用的內(nèi)存的大小,如果有足夠的內(nèi)存空間可用,可以多劃分一些內(nèi)存給 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!
恢復(fù)備份文件至數(shù)據(jù)目錄:
[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!
恢復(fù)后檢查數(shù)據(jù)一致性:
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. 不完全恢復(fù)
邏輯恢復(fù)中,mysqlbinlog 的不完全恢復(fù)方法,同樣適用于物理備份的不完全恢復(fù)。
1.13 點(diǎn),運(yùn)維人員誤刪除表 customer,可以用備份和 binlog 日志恢復(fù)到故障前(中午12點(diǎn),物理備份數(shù)據(jù)庫)
從備份文件目錄找到 binlog 位置文件 xtrabackup_binlog_info,查看備份結(jié)束時 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
查看當(dāng)前的 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)
恢復(fù)備份文件(參考完全備份步驟),然后使用 binlog 日志跳過故障時間點(diǎn),完成恢復(fù)
-- 恢復(fù)備份文件(參考完全備份步驟)
完全恢復(fù)
-- 使用binlog日志恢復(fù)到故障前
[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日志跳過故障時間點(diǎn)
[mysql@localhost ~]$ mysqlbinlog --start-datetime="2020-08-24 13:01:00" mysql-bin.000022 mysql-bin.000023 | mysql -uroot -p tempdb
Enter password:
3. 小結(jié)
本小節(jié)通過 xtrabackup 工具,介紹了物理恢復(fù)的兩種恢復(fù)方式:完全恢復(fù)、不完全恢復(fù)。
- 物理備份的完全恢復(fù)相對簡單,恢復(fù)最新的全備文件
- 不完全恢復(fù)相對要復(fù)雜,分為基于時間點(diǎn)的恢復(fù)和基于位置的恢復(fù),通常適用于人為誤操作的恢復(fù)場景