Skip to content

Instantly share code, notes, and snippets.

@prateek
Created October 21, 2014 03:39
Show Gist options
  • Save prateek/8ab1885306ff51eb151c to your computer and use it in GitHub Desktop.
Save prateek/8ab1885306ff51eb151c to your computer and use it in GitHub Desktop.
#!/bin/bash
# Adapted from
# a) http://blog.cloudera.com/blog/2013/11/approaches-to-backup-and-disaster-recovery-in-hbase/#snapshots
# b) https://blog.cloudera.com/blog/2013/03/introduction-to-apache-hbase-snapshots/
# c) https://groups.google.com/forum/#!topic/nosql-databases/osC58F5PDsE
# 1. create table
hbase shell <<EOF
disable 'testTable'
drop 'testTable'
t = create 'testTable', 'cf1'
$(seq 1 10 | xargs -I{} echo "put 'testTable', 'r{}', 'cf1', 'v1'")
EOF
# 2. create snapshot
hbase shell <<EOF
snapshot 'testTable', 'testSnapshot'
list_snapshots
EOF
# NOTE, for the sake of this example
# we export to a hdfs directory and then re-import
# onto the same cluster. it could be exported out
# to s3 and imported onto the second cluster for recovery
HBASE_EXPORT_DIR=hdfs:////user/ec2-user/hbase-export
# 3. export snapshot
hbase org.apache.hadoop.hbase.snapshot.ExportSnapshot \
-snapshot testSnapshot -overwrite \
-copy-to $HBASE_EXPORT_DIR
# 4. trash table and snapshot on current cluster
hbase shell <<EOF
delete_snapshot 'testSnapshot'
disable 'testTable'
drop 'testTable'
EOF
# 5.0 HACK to enable write access for current user to hbase folder
HADOOP_USER_NAME=hdfs hdfs dfs -chmod -R 777 /hbase
# 5.1 recover snapshot to hbase
hbase org.apache.hadoop.hbase.snapshot.ExportSnapshot \
-Dhbase.rootdir=$HBASE_EXPORT_DIR -snapshot testSnapshot \
-copy-to hdfs:////hbase
# 6. recover table from snapshot
hbase shell <<EOF
clone_snapshot 'testSnapshot', 'recoveredTable'
scan 'recoveredTable'
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment