diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/backup/impl/BackupCommands.java hbase-client/src/main/java/org/apache/hadoop/hbase/backup/impl/BackupCommands.java index 63f7a55..010f64c 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/backup/impl/BackupCommands.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/backup/impl/BackupCommands.java @@ -78,8 +78,9 @@ public final class BackupCommands { private static final String DESCRIBE_CMD_USAGE = "Usage: hbase backup decsribe \n" + " backupId backup image id\n"; - private static final String HISTORY_CMD_USAGE = "Usage: hbase backup history [-n N]\n" - + " -n N show up to N last backup sessions, default - 10;\n"; + private static final String HISTORY_CMD_USAGE = "Usage: hbase backup history [-n N] [-t table]\n" + + " -n N show up to N last backup sessions, default - 10;\n" + + " -t table table name; "; private static final String DELETE_CMD_USAGE = "Usage: hbase backup delete \n" + " backupId backup image id;\n"; @@ -396,16 +397,28 @@ public final class BackupCommands { public void execute() throws IOException { int n = parseHistoryLength(); + TableName tableName = getTableName(); Configuration conf = getConf() != null? getConf(): HBaseConfiguration.create(); try(final Connection conn = ConnectionFactory.createConnection(conf); final BackupAdmin admin = conn.getAdmin().getBackupAdmin();){ - List history = admin.getHistory(n); + List history = admin.getHistory(n, tableName); for(BackupInfo info: history){ System.out.println(info.getShortDescription()); } } } + private TableName getTableName() { + String value = cmdline.getOptionValue("t"); + if (value == null) return null; + try{ + return TableName.valueOf(value); + } catch (Exception e){ + System.out.println("Illegal argument: "+ value); + return null; + } + } + private int parseHistoryLength() { String value = cmdline.getOptionValue("n"); if (value == null) return DEFAULT_HISTORY_LENGTH; diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/client/BackupAdmin.java hbase-client/src/main/java/org/apache/hadoop/hbase/client/BackupAdmin.java index 7a411cb..8c6f9f7 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/client/BackupAdmin.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/client/BackupAdmin.java @@ -104,6 +104,17 @@ public interface BackupAdmin extends Closeable{ */ public List getHistory(int n) throws IOException; + + /** + * Show backup history command for table + * @param n - last n backup sessions + * @param naem - table's name + * @return list of backup infos + * @throws IOException exception + */ + public List getHistory(int n, TableName name) throws IOException; + + /** * Backup sets list command - list all backup sets. Backup set is * a named group of tables. diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseBackupAdmin.java hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseBackupAdmin.java index 81413c6..63c1611 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseBackupAdmin.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseBackupAdmin.java @@ -139,6 +139,24 @@ public class HBaseBackupAdmin implements BackupAdmin { } @Override + public List getHistory(int n, TableName name) throws IOException { + if(name == null) return getHistory(n); + + try (final BackupSystemTable table = new BackupSystemTable(conn)) { + List history = table.getBackupHistory(); + List list = new ArrayList(); + n = Math.min(n, history.size()); + for(int i=0; i < n; i++){ + BackupInfo info = history.get(i); + if(info.getTableNames().contains(name)){ + list.add(history.get(i)); + } + } + return list; + } + } + + @Override public List listBackupSets() throws IOException { try (final BackupSystemTable table = new BackupSystemTable(conn)) { List list = table.listBackupSets(); diff --git hbase-server/src/test/java/org/apache/hadoop/hbase/backup/TestBackupShowHistory.java hbase-server/src/test/java/org/apache/hadoop/hbase/backup/TestBackupShowHistory.java index a7d2750..145a060 100644 --- hbase-server/src/test/java/org/apache/hadoop/hbase/backup/TestBackupShowHistory.java +++ hbase-server/src/test/java/org/apache/hadoop/hbase/backup/TestBackupShowHistory.java @@ -89,4 +89,34 @@ public class TestBackupShowHistory extends TestBackupBase { LOG.info(baos.toString()); assertTrue(output.indexOf(backupId) > 0); } + + + @Test + public void testBackupHistoryOneTable() throws Exception { + + LOG.info("test backup history on a single table with data"); + + List tableList = Lists.newArrayList(table1); + String backupId1 = fullTableBackup(tableList); + assertTrue(checkSucceeded(backupId1)); + LOG.info("backup complete: "+table1); + + tableList = Lists.newArrayList(table2); + String backupId2 = fullTableBackup(tableList); + assertTrue(checkSucceeded(backupId2)); + LOG.info("backup complete: "+ table2); + + List history = getBackupAdmin().getHistory(10, table1); + assertTrue(history.size() > 0); + boolean success = true; + for(BackupInfo info: history){ + if(!info.getTableNames().contains(table1)){ + success = false; break; + } + } + assertTrue(success); + LOG.info("show_history"); + + } + } \ No newline at end of file