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.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertNotNull; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023 024import java.util.List; 025import org.apache.hadoop.hbase.TableName; 026import org.apache.hadoop.hbase.backup.impl.BackupSystemTable; 027import org.apache.hadoop.hbase.client.Admin; 028import org.apache.hadoop.hbase.testclassification.LargeTests; 029import org.apache.hadoop.util.ToolRunner; 030import org.junit.jupiter.api.Tag; 031import org.junit.jupiter.api.Test; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034 035@Tag(LargeTests.TAG) 036public class TestFullBackupSet extends TestBackupBase { 037 038 private static final Logger LOG = LoggerFactory.getLogger(TestFullBackupSet.class); 039 040 /** 041 * Verify that full backup is created on a single table with data correctly. 042 * @throws Exception if doing the backup or an operation on the tables fails 043 */ 044 @Test 045 public void testFullBackupSetExist() throws Exception { 046 LOG.info("Test full backup, backup set exists"); 047 048 // Create set 049 try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) { 050 String name = "name"; 051 table.addToBackupSet(name, new String[] { table1.getNameAsString() }); 052 List<TableName> names = table.describeBackupSet(name); 053 054 assertNotNull(names); 055 assertTrue(names.size() == 1); 056 assertTrue(names.get(0).equals(table1)); 057 058 String[] args = new String[] { "create", "full", BACKUP_ROOT_DIR, "-s", name }; 059 // Run backup 060 int ret = ToolRunner.run(conf1, new BackupDriver(), args); 061 assertTrue(ret == 0); 062 List<BackupInfo> backups = table.getBackupHistory(); 063 assertTrue(backups.size() == 1); 064 String backupId = backups.get(0).getBackupId(); 065 assertTrue(checkSucceeded(backupId)); 066 067 LOG.info("backup complete"); 068 069 // Restore from set into other table 070 args = new String[] { BACKUP_ROOT_DIR, backupId, "-s", name, "-m", 071 table1_restore.getNameAsString(), "-o" }; 072 // Run backup 073 ret = ToolRunner.run(conf1, new RestoreDriver(), args); 074 assertTrue(ret == 0); 075 Admin hba = TEST_UTIL.getAdmin(); 076 assertTrue(hba.tableExists(table1_restore)); 077 // Verify number of rows in both tables 078 assertEquals(TEST_UTIL.countRows(table1), TEST_UTIL.countRows(table1_restore)); 079 TEST_UTIL.deleteTable(table1_restore); 080 LOG.info("restore into other table is complete"); 081 hba.close(); 082 } 083 } 084 085 @Test 086 public void testFullBackupSetDoesNotExist() throws Exception { 087 LOG.info("test full backup, backup set does not exist"); 088 String name = "name1"; 089 String[] args = new String[] { "create", "full", BACKUP_ROOT_DIR, "-s", name }; 090 // Run backup 091 int ret = ToolRunner.run(conf1, new BackupDriver(), args); 092 assertTrue(ret != 0); 093 } 094}