001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.backup;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertTrue;
023
024import java.util.List;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
028import org.apache.hadoop.hbase.client.Admin;
029import org.apache.hadoop.hbase.testclassification.LargeTests;
030import org.apache.hadoop.util.ToolRunner;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037@Category(LargeTests.class)
038public class TestFullBackupSetRestoreSet extends TestBackupBase {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestFullBackupSetRestoreSet.class);
043
044  private static final Logger LOG = LoggerFactory.getLogger(TestFullBackupSetRestoreSet.class);
045
046  @Test
047  public void testFullRestoreSetToOtherTable() throws Exception {
048
049    LOG.info("Test full restore set");
050
051    // Create set
052    try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
053      String name = "name";
054      table.addToBackupSet(name, new String[] { table1.getNameAsString() });
055      List<TableName> names = table.describeBackupSet(name);
056
057      assertNotNull(names);
058      assertTrue(names.size() == 1);
059      assertTrue(names.get(0).equals(table1));
060
061      String[] args = new String[] { "create", "full", BACKUP_ROOT_DIR, "-s", name };
062      // Run backup
063      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
064      assertTrue(ret == 0);
065      List<BackupInfo> backups = table.getBackupHistory();
066      assertTrue(backups.size() == 1);
067      String backupId = backups.get(0).getBackupId();
068      assertTrue(checkSucceeded(backupId));
069
070      LOG.info("backup complete");
071
072      // Restore from set into other table
073      args = new String[] { BACKUP_ROOT_DIR, backupId, "-s", name, "-m",
074        table1_restore.getNameAsString(), "-o" };
075      // Run backup
076      ret = ToolRunner.run(conf1, new RestoreDriver(), args);
077      assertTrue(ret == 0);
078      Admin hba = TEST_UTIL.getAdmin();
079      assertTrue(hba.tableExists(table1_restore));
080      // Verify number of rows in both tables
081      assertEquals(TEST_UTIL.countRows(table1), TEST_UTIL.countRows(table1_restore));
082      TEST_UTIL.deleteTable(table1_restore);
083      LOG.info("restore into other table is complete");
084      hba.close();
085    }
086  }
087
088  @Test
089  public void testFullRestoreSetToSameTable() throws Exception {
090
091    LOG.info("Test full restore set to same table");
092
093    // Create set
094    try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
095      String name = "name1";
096      table.addToBackupSet(name, new String[] { table1.getNameAsString() });
097      List<TableName> names = table.describeBackupSet(name);
098
099      assertNotNull(names);
100      assertTrue(names.size() == 1);
101      assertTrue(names.get(0).equals(table1));
102
103      String[] args = new String[] { "create", "full", BACKUP_ROOT_DIR, "-s", name };
104      // Run backup
105      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
106      assertTrue(ret == 0);
107      List<BackupInfo> backups = table.getBackupHistory();
108      String backupId = backups.get(0).getBackupId();
109      assertTrue(checkSucceeded(backupId));
110
111      LOG.info("backup complete");
112      int count = TEST_UTIL.countRows(table1);
113      TEST_UTIL.deleteTable(table1);
114
115      // Restore from set into other table
116      args = new String[] { BACKUP_ROOT_DIR, backupId, "-s", name, "-o" };
117      // Run backup
118      ret = ToolRunner.run(conf1, new RestoreDriver(), args);
119      assertTrue(ret == 0);
120      Admin hba = TEST_UTIL.getAdmin();
121      assertTrue(hba.tableExists(table1));
122      // Verify number of rows in both tables
123      assertEquals(count, TEST_UTIL.countRows(table1));
124      LOG.info("restore into same table is complete");
125      hba.close();
126
127    }
128
129  }
130
131}