xtrabackup 備份mysql數據庫三:partial backup 部分備份及恢復

innobackup 部分備份恢復實驗

三種方式:
## Using the --include option
The command above will create a timestamped directory with the usual files that innobackupex creates, 
but only the data files related to the tables matched.
Note that this option is passed to xtrabackup --tables and is matched against each table of each database,
the directories of each database will be created even if they are empty.
該方式會將沒有備份表的數據庫目錄也生成,支持模式匹配


--備份test庫t_innodb開頭的表
$ innobackupex --user=bkpuser --password=s3cret --defaults-file=/etc/my.cnf --include='^test[.]t_innodb' 
--備份test庫下所有的表
$ innobackupex --user=bkpuser --password=s3cret --defaults-file=/etc/my.cnf --include='^test[.]' 




## Using the --tables-file option
Note that this option is passed to xtrabackup --tables and is matched against each table of each database, 
the directories of each database will be created even if they are empty.
該方式不能實現模式匹配,只生產需要備份表的數據庫目錄


--備份test.t_innodb,
$ echo "test.t_innodb" > /tmp/tables.txt
$ innobackupex --user=bkpuser --password=s3cret --defaults-file=/etc/my.cnf --tables-file=/tmp/tables.txt /backup




## Using the --databases option
This option is specific to innobackupex and accepts either a space-separated list of the databases and 
tables to backup - in the databasename[.tablename] form - or a file containing the list at one element per line
Currently, only .frm and non-InnoDB tables are limited by that option
當前版本,只對non-innodb 有限制作用


$ innobackupex --user=bkpuser --password=s3cret --defaults-file=/etc/my.cnf  --databases="test.innodb" /backup




## 測試表 t_innodb ,並插入10000條記錄
(root@localhost) [test]>desc t_innodb;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | bigint(20)   | NO   | PRI | NULL    | auto_increment | 
| name       | varchar(50)  | YES  |     | NULL    |                | 
| password   | varchar(150) | YES  |     | NULL    |                | 
| userstatus | int(2)       | YES  |     | NULL    |                | 
+------------+--------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)


(root@localhost) [test]>call addTest(10000,0);
Query OK, 1 row affected (4.12 sec)


(root@localhost) [test]>select count(*) from t_innodb;
+----------+
| count(*) |
+----------+
|    10000 | 
+----------+
1 row in set (0.01 sec)




## 執行一次表t_innodb的備份,作爲全備
$ echo "test.t_innodb" > /tmp/tables.txt
$ innobackupex --user=bkpuser --password=s3cret --defaults-file=/etc/my.cnf --tables-file=/tmp/tables.txt /backup/full_innodb --no-timestamp




## 全備後,再次插入10000條記錄
(root@localhost) [test]>call addTest(10000,0);
Query OK, 1 row affected (4.12 sec)


(root@localhost) [test]>select count(*) from t_innodb;
+----------+
| count(*) |
+----------+
|    20000 | 
+----------+
1 row in set (0.01 sec)




## 執行一次增量備份
$ innobackupex --user=bkpuser --password=s3cret --defaults-file=/etc/my.cnf --incremental --incremental-basedir=/backup/full_innodb --tables-file=/tmp/tables.txt /backup/inc_innodb --no-timestamp




## 刪除原庫中的標
(root@localhost) [test]>drop table t_innodb;
Query OK, 0 rows affected (0.01 sec)




## 針對全備做一次apply-log,需要使用redo-only 選項
innobackupex --apply-log --redo-only --export /backup/full_innodb




## 增量備份,做一次apply-log,作爲最後一個增量,不要使用redo-only
innobackupex --defaults-file=/etc/my.cnf --apply-log --incremental-dir=/backup/full_innodb /backup/full_innodb




## 再次執行apply-log, 是用export 生成exp、cfg 文件
innobackupex --apply-log --export /backup/full_innodb




## 在數據庫中新建表t_innodb
(root@localhost) [test]>drop table t_innodb;


## 可以是用mysqlfrm 從t_innodb 中獲取建表語句

#  顯示frm的建表語句
mysqlfrm --server=root:mysql@localhost:3306 /backup/full_innodb/test/t_innodb.frm --port=3333


(root@localhost) [test]>CREATE TABLE `t_innodb` (
    ->   `id` bigint(20) NOT NULL AUTO_INCREMENT,
    ->   `name` varchar(50) DEFAULT NULL,
    ->   `password` varchar(150) DEFAULT NULL,
    ->   `userstatus` int(2) DEFAULT NULL,
    ->   PRIMARY KEY (`id`)
    -> ) ENGINE=InnoDB AUTO_INCREMENT=20001 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)




## 刪除當前的.ibd文件
(root@localhost) [test]>ALTER TABLE test.t_innodb DISCARD TABLESPACE;
Query OK, 0 rows affected (0.00 sec)




## 把cfg、ibd、exp文件放到test數據庫目錄中
[mysql@rhel5 test]$ cp t_innodb.cfg /usr/local/mysql/data/test/
[mysql@rhel5 test]$ cp t_innodb.ibd /usr/local/mysql/data/test/
[mysql@rhel5 test]$ cp t_innodb.exp /usr/local/mysql/data/test/




## 把備份的.ibd文件還原到表中
(root@localhost) [test]>ALTER TABLE test.t_innodb  IMPORT TABLESPACE;




## 查詢恢復的數據
(root@localhost) [test]>select count(*) from t_innodb;                                    
+----------+
| count(*) |
+----------+
|    20000 | 
+----------+
1 row in set (0.01 sec)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章