2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.backup
;
20 import static org
.junit
.Assert
.assertTrue
;
22 import java
.io
.ByteArrayOutputStream
;
23 import java
.io
.PrintStream
;
24 import java
.util
.List
;
25 import org
.apache
.hadoop
.fs
.FileSystem
;
26 import org
.apache
.hadoop
.fs
.Path
;
27 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
28 import org
.apache
.hadoop
.hbase
.TableName
;
29 import org
.apache
.hadoop
.hbase
.backup
.impl
.BackupSystemTable
;
30 import org
.apache
.hadoop
.hbase
.testclassification
.LargeTests
;
31 import org
.apache
.hadoop
.hbase
.util
.EnvironmentEdge
;
32 import org
.apache
.hadoop
.hbase
.util
.EnvironmentEdgeManager
;
33 import org
.apache
.hadoop
.util
.ToolRunner
;
34 import org
.junit
.Assert
;
35 import org
.junit
.ClassRule
;
36 import org
.junit
.Test
;
37 import org
.junit
.experimental
.categories
.Category
;
38 import org
.slf4j
.Logger
;
39 import org
.slf4j
.LoggerFactory
;
41 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.collect
.Lists
;
43 @Category(LargeTests
.class)
44 public class TestBackupDelete
extends TestBackupBase
{
47 public static final HBaseClassTestRule CLASS_RULE
=
48 HBaseClassTestRule
.forClass(TestBackupDelete
.class);
50 private static final Logger LOG
= LoggerFactory
.getLogger(TestBackupDelete
.class);
53 * Verify that full backup is created on a single table with data correctly. Verify that history
56 * @throws Exception if doing the backup or an operation on the tables fails
59 public void testBackupDelete() throws Exception
{
60 LOG
.info("test backup delete on a single table with data");
61 List
<TableName
> tableList
= Lists
.newArrayList(table1
);
62 String backupId
= fullTableBackup(tableList
);
63 assertTrue(checkSucceeded(backupId
));
64 LOG
.info("backup complete");
65 String
[] backupIds
= new String
[] { backupId
};
66 BackupSystemTable table
= new BackupSystemTable(TEST_UTIL
.getConnection());
67 BackupInfo info
= table
.readBackupInfo(backupId
);
68 Path path
= new Path(info
.getBackupRootDir(), backupId
);
69 FileSystem fs
= FileSystem
.get(path
.toUri(), conf1
);
70 assertTrue(fs
.exists(path
));
71 int deleted
= getBackupAdmin().deleteBackups(backupIds
);
73 assertTrue(!fs
.exists(path
));
74 assertTrue(fs
.exists(new Path(info
.getBackupRootDir())));
75 assertTrue(1 == deleted
);
77 LOG
.info("delete_backup");
81 * Verify that full backup is created on a single table with data correctly. Verify that history
84 * @throws Exception if doing the backup or an operation on the tables fails
87 public void testBackupDeleteCommand() throws Exception
{
88 LOG
.info("test backup delete on a single table with data: command-line");
89 List
<TableName
> tableList
= Lists
.newArrayList(table1
);
90 String backupId
= fullTableBackup(tableList
);
91 assertTrue(checkSucceeded(backupId
));
92 LOG
.info("backup complete");
93 ByteArrayOutputStream baos
= new ByteArrayOutputStream();
94 System
.setOut(new PrintStream(baos
));
96 String
[] args
= new String
[] { "delete", "-l", backupId
};
100 int ret
= ToolRunner
.run(conf1
, new BackupDriver(), args
);
101 assertTrue(ret
== 0);
102 } catch (Exception e
) {
103 LOG
.error("failed", e
);
105 LOG
.info("delete_backup");
106 String output
= baos
.toString();
107 LOG
.info(baos
.toString());
108 assertTrue(output
.indexOf("Deleted 1 backups") >= 0);
112 public void testBackupPurgeOldBackupsCommand() throws Exception
{
113 LOG
.info("test backup delete (purge old backups) on a single table with data: command-line");
114 List
<TableName
> tableList
= Lists
.newArrayList(table1
);
115 EnvironmentEdgeManager
.injectEdge(new EnvironmentEdge() {
118 public long currentTime() {
119 return System
.currentTimeMillis() - 2 * 24 * 3600 * 1000 ;
122 String backupId
= fullTableBackup(tableList
);
123 assertTrue(checkSucceeded(backupId
));
125 EnvironmentEdgeManager
.reset();
127 LOG
.info("backup complete");
128 ByteArrayOutputStream baos
= new ByteArrayOutputStream();
129 System
.setOut(new PrintStream(baos
));
131 // Purge all backups which are older than 3 days
132 // Must return 0 (no backups were purged)
133 String
[] args
= new String
[] { "delete", "-k", "3" };
137 int ret
= ToolRunner
.run(conf1
, new BackupDriver(), args
);
138 assertTrue(ret
== 0);
139 } catch (Exception e
) {
140 LOG
.error("failed", e
);
141 Assert
.fail(e
.getMessage());
143 String output
= baos
.toString();
144 LOG
.info(baos
.toString());
145 assertTrue(output
.indexOf("Deleted 0 backups") >= 0);
147 // Purge all backups which are older than 1 days
148 // Must return 1 deleted backup
149 args
= new String
[] { "delete", "-k", "1" };
153 int ret
= ToolRunner
.run(conf1
, new BackupDriver(), args
);
154 assertTrue(ret
== 0);
155 } catch (Exception e
) {
156 LOG
.error("failed", e
);
157 Assert
.fail(e
.getMessage());
159 output
= baos
.toString();
160 LOG
.info(baos
.toString());
161 assertTrue(output
.indexOf("Deleted 1 backups") >= 0);