001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.snapshot; 020 021import java.util.Arrays; 022import java.util.Locale; 023 024import org.apache.hadoop.hbase.TableName; 025import org.apache.hadoop.hbase.client.Admin; 026import org.apache.hadoop.hbase.client.Connection; 027import org.apache.hadoop.hbase.client.ConnectionFactory; 028import org.apache.hadoop.hbase.client.SnapshotDescription; 029import org.apache.hadoop.hbase.client.SnapshotType; 030import org.apache.hadoop.hbase.util.AbstractHBaseTool; 031import org.apache.yetus.audience.InterfaceAudience; 032 033import org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine; 034 035/** 036 * This is a command line class that will snapshot a given table. 037 */ 038@InterfaceAudience.Private 039public class CreateSnapshot extends AbstractHBaseTool { 040 private SnapshotType snapshotType = SnapshotType.FLUSH; 041 private TableName tableName = null; 042 private String snapshotName = null; 043 044 public static void main(String[] args) { 045 new CreateSnapshot().doStaticMain(args); 046 } 047 048 @Override 049 protected void addOptions() { 050 this.addRequiredOptWithArg("t", "table", "The name of the table"); 051 this.addRequiredOptWithArg("n", "name", "The name of the created snapshot"); 052 this.addOptWithArg("s", "snapshot_type", 053 "Snapshot Type. FLUSH is default. Posible values are " 054 + Arrays.toString(SnapshotType.values())); 055 } 056 057 @Override 058 protected void processOptions(CommandLine cmd) { 059 this.tableName = TableName.valueOf(cmd.getOptionValue('t')); 060 this.snapshotName = cmd.getOptionValue('n'); 061 String snapshotTypeName = cmd.getOptionValue('s'); 062 if (snapshotTypeName != null) { 063 snapshotTypeName = snapshotTypeName.toUpperCase(Locale.ROOT); 064 this.snapshotType = SnapshotType.valueOf(snapshotTypeName); 065 } 066 } 067 068 @Override 069 protected int doWork() throws Exception { 070 Connection connection = null; 071 Admin admin = null; 072 try { 073 connection = ConnectionFactory.createConnection(getConf()); 074 admin = connection.getAdmin(); 075 admin.snapshot(new SnapshotDescription(snapshotName, tableName, snapshotType)); 076 } catch (Exception e) { 077 System.err.println("failed to take the snapshot: " + e.getMessage()); 078 return -1; 079 } finally { 080 if (admin != null) { 081 admin.close(); 082 } 083 if (connection != null) { 084 connection.close(); 085 } 086 } 087 return 0; 088 } 089 090}