2019年3月13日 星期三

【MYSQL】xtrabackup單庫/單表復原


原存在DB如下,測試目前只需要復原testmaster這個DB

mysql> show databases;

+------------------------------+
| Database                     |
+------------------------------+
| information_schema           |
| db_1                         |
| db_2                         |
| mysql                        |
| performance_schema           |
| sys                          |
| testmaster                   |  ←只復原此DB
+------------------------------+

testmaster內的table如下

mysql> show tables;

+------------------------------+
| Tables_in_testmaster         |
+------------------------------+
| aaa                          |
| test                         |
+------------------------------+

2 rows in set (0.00 sec)

在 table-test表內,有兩筆資料


mysql> select * from test ;

+----+------+-------+--------+--------+--------+
| id | str  | state | state2 | state3 | state4 |
+----+------+-------+--------+--------+--------+
|  1 | csdn |     4 |     04 |    004 |   0004 |
|  2 | csdn |     4 |     04 |    004 |   0004 |
+----+------+-------+--------+--------+--------+

2 rows in set (0.00 sec)

接著進行xtrabackup全備(/backup/xtrabackup/20190313/這是指定備份檔到此路徑下)

# /usr/bin/innobackupex /backup/xtrabackup/20190313/ --user=root --password="1234" --socket=/tmp/mysql.sock

備份完成後 ,進入MYSQL內把DB:testmaster刪除

mysql> drop database testmaster;

Query OK, 2 rows affected (0.08 sec)

接著進行復原準備工作

復原準備指令:
# /usr/bin/innobackupex --apply-log --redo-only /backup/xtrabackup/20190313/2019-03-13_10-55-01 --use-memory=500m --user=root --password='1234'

↓執行時顯示內容

xtrabackup: recognized server arguments:
xtrabackup: recognized client arguments:
190313 11:02:33 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!".

/usr/bin/innobackupex version 2.4.12 based on MySQL server 5.7.19 Linux (x86_64) (revision id: 170eb8c)
.
.略....
.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 1436926
InnoDB: Number of pools: 1
190313 11:04:25 completed OK!            OK表示完成

※--use-memory:該選項表示和--apply- log選項一起使用,prepare備份的時候,xtrabackup做crash recovery分配的內存大小,單位字節。也可(1MB,1M,1G,1GB),推薦1G。

接著進入備份檔內的testmaster資料夾

# cd /backup/xtrabackup/20190313/2019-03-13_10-55-01/testmaster

再來查看表結構(例用mysqlfrm --diagnostic查詢 資料夾內所屬副檔名是.frm)
:需安裝mysql-utilities才可以查表結構.frm是存放表結構的
指令# yum install mysql-utilities -y

# mysqlfrm --diagnostic *.frm
上述指令指查詢在testmaster資料夾內所有table的表結構

↓執行後顯示內容

# WARNING: Cannot generate character set or collation names without the --server option.
# CAUTION: The diagnostic mode is a best-effort parse of the .frm file. As such, it may not identify all of the components of the table correctly. This is especially true for damaged files. It will also not read the default values for the columns and the resulting statement may not be syntactically correct.
# Reading .frm file for aaa.frm:
# The .frm file is a TABLE.
# CREATE TABLE Statement:

CREATE TABLE `aaa` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
PRIMARY KEY `PRIMARY` (`id`)
) ENGINE=InnoDB;

# Reading .frm file for test.frm:
# The .frm file is a TABLE.
# CREATE TABLE Statement:

CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `str` varchar(255) NOT NULL,
  `state` tinyint(1) unsigned zerofill DEFAULT NULL,
  `state2` tinyint(2) unsigned zerofill DEFAULT NULL,
  `state3` tinyint(3) unsigned zerofill DEFAULT NULL,
  `state4` tinyint(4) unsigned zerofill DEFAULT NULL,
PRIMARY KEY `PRIMARY` (`id`)
) ENGINE=InnoDB;

#...done.

查出表結構後(上面紅色),進入MYSQL內執行以下步驟還原
1.建DB
2.使用DB
3.建立表(把上面黃色部份查出的表結構直接貼上)

mysql> create database testmaster;

mysql> use testmaster

把表結構建立好後 ,裡面的資料還未復原
再來需要釋放表空間,執行discard tablespace
註:在建立表結構時,MYSQL的檔案夾會產生.frm及.ibd,再執行釋放後,ibd檔會不見。
ibd是InnoDB 數據和索引,釋放也就是指刪除目前的檔案

mysql> alter table aaa discard tablespace;

Query OK, 0 rows affected (0.01 sec)


mysql> alter table test discard tablespace;

Query OK, 0 rows affected (0.00 sec)

接著COPY原xtrabackup備份好的準備
路徑(/backup/xtrabackup/20190313/.....備份時自已產生檔案所以自行查找)


1.進入/backup/xtrabackup/20190313/2019-03-13_10-55-01/testmaster
(testmastr是我要復原的DB名,在/backup/xtrabackup/20190313/2019-03-13_10-55-01/會有全部備份的資料,但我只需要復原testmastr)

2.拷貝所有副檔名ibd的檔至 MYSQL的路徑下(路徑就是my.cnf的datadir=)

3.進入MYSQL內的testmaster資料夾內(我MYSQL的路徑是/data/mysql/ 在此路徑下會有所有DB的資料夾,所以可以找到剛建立的create database)

4.把移動過來的ibd檔,更改擁有人及群組是mysql

cd /backup/xtrabackup/20190313/2019-03-13_10-55-01/testmaster

cp *.ibd /data/mysql/testmaster/


cd /data/mysql/testmaster/

chown -R mysql:mysql *.ibd

接著進入MYSQL把表空間導入數據庫內,此範例有兩個表...多個就要一個一個輸入了,也可單表

mysql> alter table aaa import tablespace;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> alter table test import tablespace;
Query OK, 0 rows affected, 1 warning (0.01 sec)


接著發現,原本不見被DROP的DB回來了,連TABLE資料也復原了
mysql> show tables;

+------------------------------+
| Tables_in_testmaster         |
+------------------------------+
| aaa                          |
| test                         |
+------------------------------+

2 rows in set (0.00 sec)


mysql> select * from test;

+----+------+-------+--------+--------+--------+
| id | str  | state | state2 | state3 | state4 |
+----+------+-------+--------+--------+--------+
|  1 | csdn |     4 |     04 |    004 |   0004 |
|  2 | csdn |     4 |     04 |    004 |   0004 |
+----+------+-------+--------+--------+--------+

2 rows in set (0.00 sec)

沒有留言:

張貼留言

【MYSQL】MYSQL的SYS表說明(版本8.0)

mysql> use sys Reading table information for completion of table and column names You can turn off this feature to get a quicker s...