本文介绍当在Trafodion不小心使用delete误删除数据后的恢复方法,使用HBase的Snapshot特性。
假如我们有一个现成的Trafodion表,表中有一些样例数据,如下
SQL>select * from test_rec_del;
A B
----------- -----------
1 2
3 4
5 6
7 8
9 10
--- 5 row(s) selected.
从HBase Shell亦可以Scan数据,
hbase(main):019:0> scan 'TRAFODION.SEABASE.TEST_REC_DEL'
ROW COLUMN+CELL
\x9FZz&\xF9b\xE37 column=#1:\x01, timestamp=1499680251200, value=\x10\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x007\xE3b\xF9&zZ\x1F\x01\x00\x00\x00\x02
\x00\x00\x00
\x9FZz&\xF9c\x166 column=#1:\x01, timestamp=1499680251203, value=\x10\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x006\x16c\xF9&zZ\x1F\x03\x00\x00\x00\x04
\x00\x00\x00
\x9FZz&\xF9c"\xF4 column=#1:\x01, timestamp=1499680251205, value=\x10\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xF4"c\xF9&zZ\x1F\x05\x00\x00\x00\x06
\x00\x00\x00
\x9FZz&\xF9c'\xEF column=#1:\x01, timestamp=1499680251206, value=\x10\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xEF'c\xF9&zZ\x1F\x07\x00\x00\x00\x08
\x00\x00\x00
\x9FZz&\xF9c,\x0D column=#1:\x01, timestamp=1499680251207, value=\x10\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0D,c\xF9&zZ\x1F\x09\x00\x00\x00\x0A
\x00\x00\x00
5 row(s) in 0.0630 seconds
此时,HBase中无任何表的Snapshot,可通过list_snapshots查看,
hbase(main):025:0* list_snapshots
SNAPSHOT TABLE + CREATION TIME
0 row(s) in 0.0220 seconds
=> []
要想根据HBase Snapshot特性恢复数据,那么前提是表上已经生成了相关的Snapshot,生成Snapshot方法如下,
hbase(main):001:0> snapshot 'TRAFODION.SEABASE.TEST_REC_DEL','snapshot_test_rec_del'
0 row(s) in 0.6790 seconds
hbase(main):002:0> list_snapshots
SNAPSHOT TABLE + CREATION TIME
snapshot_test_rec_del TRAFODION.SEABASE.TEST_REC_DEL (Mon Jul 10 17:56:28 +0800 2017)
1 row(s) in 0.0520 seconds
=> ["snapshot_test_rec_del"]
现在模拟误删数据,
SQL>delete from test_rec_del;
--- 5 row(s) deleted.
SQL>select count(*) from test_rec_del;
(EXPR)
--------------------
0
--- 1 row(s) selected.
从HBase Shell中也无法Scan到数据,
hbase(main):003:0> scan 'TRAFODION.SEABASE.TEST_REC_DEL'
ROW COLUMN+CELL
0 row(s) in 0.2190 seconds
现在可以执行Snapshot恢复步骤了,步骤为:
1. disable table_name
2. restore_snapshot snapshot_name
3. enable table_name
hbase(main):002:0* disable 'TRAFODION.SEABASE.TEST_REC_DEL'
0 row(s) in 2.7230 seconds
hbase(main):003:0> restore_snapshot 'snapshot_test_rec_del'
0 row(s) in 0.3960 seconds
hbase(main):004:0> enable 'TRAFODION.SEABASE.TEST_REC_DEL'
0 row(s) in 1.2700 seconds
从Trafodion中重新查看表,此时发现表中数据已恢复,
SQL>select * from test_rec_del;
A B
----------- -----------
1 2
3 4
5 6
7 8
9 10
--- 5 row(s) selected.
注意,由于Snapshot保存的是某一时刻表的状态,所以恢复出来的数据只是备份时的那个状态信息,如果有多个时刻的Snapshot,可以选择希望恢复到离某个时间点最新的Snapshot进行恢复。有关HBase Snapshot的介绍,可以参考: http://blog.cloudera.com/blog/2013/03/introduction-to-apache-hbase-snapshots/
在Trafodion中,如果希望支持数据恢复的功能,建议使用Trafodion的“离线备份恢复”机制,定期进行数据库的备份(同样利用HBase snapshot原理),这样可以实现恢复数据到某一状态,关于“离线备份恢复”方法,可参考我的另外一篇博客:http://blog.youkuaiyun.com/post_yuan/article/details/53393267
如果是从离线备份数据集中恢复一张表,具体命令如下,
sudo -u hbase hbase org.trafodion.utility.backuprestore.TrafExportSnapshot -D hbase.rootdir=hdfs://备份目录 -snapshot 快照名字 -copy-to hdfs://hdfs root目录/hbase -mappers 0 -mr-lowlimit-mb 100