HBase內建的export/import機制可以實現Backup Restore功能。而且可以實現增量備份。 下面設增量備份的python指令碼 在這個指令碼中,每天做增量備份,每個月15日做全備份。
- import time
- import datetime
- from datetime import date
- import sys
- import os
-
- tablename=sys.argv[1]
- backupDst=sys.argv[2]
- today=date.today()
- if today.day == 15: //every month, we do a full backup
- backupSubFolder=backupDst+today.isoformat()+"-full"
- cmd="hbase org.apache.hadoop.hbase.mapreduce.Export %s %s"%(tablename,backupSubFolder)
- else:
-
- yesterday=datetime.date.today()- datetime.timedelta(days=1)
- todayTimeStamp=time.mktime(today.timetuple())
- yesTimeStamp=time.mktime(yesterday.timetuple())
- backupSubFolder=backupDst+today.isoformat()
- cmd="hbase org.apache.hadoop.hbase.mapreduce.Export %s %s %s"%(tablename,backupSubFolder,str(int(todayTimeStamp)*1000)
-
- print cmd
-
- os.system(cmd)
而Restore機制就更加簡單
- hbase org.apache.hadoop.hbase.mapreduce.Import tablename restorefolder
要注意的是,做Restore時候,原始的表必須是建立好的。所以如果表本身被損壞,需要重新建立一個新的空表,再做Restore。
還有,必須在Hadoop-env.sh中配置好hbase和Zookeeper的路徑,加上這樣的語句
- export HADOOP_CLASSPATH="/usr/lib/hadoop-hbase/hbaseXXX.jar:/usr/lib/hadoop-hbase/lib/zookeeperXXX.jar:/etc/hadoop-hbase/conf"