基於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.000001 120
[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)
相關文章
基於percona xtrabackup 2.4.14的增量備份恢復還原mysql 5.6
http://blog.itpub.net/9240380/viewspace-2662613/
基於percona xtrabackup之xtrabackup實現mysql增量備份及恢復資料庫
https://mp.weixin.qq.com/s?__biz=MzIwMjU2MjI1OQ==&mid=2247484166&idx=1&sn=3a838774506ed31a7a936690e9c80060&chksm=96dd8cbaa1aa05ac86340db0b5da6444fd4f4d6f0b89af21bad1f0c220918a9eee37cb935eda&token=1922309711&lang=zh_CN#rd
聯絡方式
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-2662688/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql通過percona xtrabackup全備和mysql binlog實現基於時間點的資料庫恢復MySql資料庫
- RMAN基於時間點恢復Oracle資料庫Oracle資料庫
- 備份與恢復:Polardb資料庫資料基於時間點恢復資料庫
- ORACLE資料庫基於時間點的不完全恢復Oracle資料庫
- Oracle基於時間點的恢復Oracle
- RAC 資料庫恢復到單例項下並且基於時間點恢復資料庫單例
- 基於時間執行資料庫不完全恢復資料庫
- oracle實驗記錄 (恢復-表空間基於時間點恢復(rman))Oracle
- mongodb 基於oplog的時間點恢復MongoDB
- oracle實驗記錄 (恢復-表空間基於時間點恢復(手動))Oracle
- 基於cancel的資料庫恢復資料庫
- oracle基於時間點的不完全恢復Oracle
- 基於percona xtrabackup 2.4.14的增量備份恢復還原mysql 5.6MySql
- Percona XtraBackup 2.4 innobackupex全量、增量備份恢復流程
- 【備份恢復】閃回資料庫(三)基於時間戳閃回資料庫資料庫時間戳
- 用mysqlbinlog做基於時間點的資料恢復一例MySql資料恢復
- MySQL binlog基於時間點恢復資料失敗是什麼鬼?MySql
- MySQL備份與恢復——基於Xtrabackup物理備份恢復MySql
- 【RMAN】表空間基於時間點的RMAN恢復-TSPITR
- oracle 閃回基於時間的恢復Oracle
- Xtrabackup之innobackupex備份恢復詳解薦
- 基於 Vuex 的時移操作(撤回/恢復)實現Vue
- 基於資料塊的恢復
- 【TSPITR】RMAN表空間基於時間點的自動恢復
- 基於時間戳閃回資料庫時間戳資料庫
- 使用innobackupex恢復mysql資料庫MySql資料庫
- Percona XtraBackup 實現全備&增量備份與恢復
- Xtrabackup實現資料庫備份和災難恢復資料庫
- 基於tsm的oracle資料庫備份及恢復Oracle資料庫
- 關於 RMAN 對於可插拔資料庫按時間點的恢復 (文件 ID 1984554.1)資料庫
- 【備份恢復】閃回資料庫(四)基於可靠還原點閃回資料庫資料庫
- 基於GTID恢復誤篡改資料
- 2.MongoDB 4.2副本集環境基於時間點的恢復MongoDB
- 基於MySQL資料庫討論虛擬機器資料恢復MySql資料庫虛擬機資料恢復
- 基於DRBD實現資料庫高可用資料庫
- 【備份恢復】閃回資料庫(二) 基於 SCN 閃回資料庫資料庫
- Oracle 12c PDB基於時間點的不完全恢復記錄Oracle
- 基於RMAN實現壞塊介質恢復(blockrecover)BloC