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.snapshot;
019
020import java.util.Arrays;
021import java.util.Locale;
022import org.apache.hadoop.hbase.TableName;
023import org.apache.hadoop.hbase.client.Admin;
024import org.apache.hadoop.hbase.client.Connection;
025import org.apache.hadoop.hbase.client.ConnectionFactory;
026import org.apache.hadoop.hbase.client.SnapshotDescription;
027import org.apache.hadoop.hbase.client.SnapshotType;
028import org.apache.hadoop.hbase.util.AbstractHBaseTool;
029import org.apache.yetus.audience.InterfaceAudience;
030
031import org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine;
032
033/**
034 * This is a command line class that will snapshot a given table.
035 */
036@InterfaceAudience.Private
037public class CreateSnapshot extends AbstractHBaseTool {
038  private SnapshotType snapshotType = SnapshotType.FLUSH;
039  private TableName tableName = null;
040  private String snapshotName = null;
041
042  public static void main(String[] args) {
043    new CreateSnapshot().doStaticMain(args);
044  }
045
046  @Override
047  protected void addOptions() {
048    this.addRequiredOptWithArg("t", "table", "The name of the table");
049    this.addRequiredOptWithArg("n", "name", "The name of the created snapshot");
050    this.addOptWithArg("s", "snapshot_type", "Snapshot Type. FLUSH is default. Posible values are "
051      + Arrays.toString(SnapshotType.values()));
052  }
053
054  @Override
055  protected void processOptions(CommandLine cmd) {
056    this.tableName = TableName.valueOf(cmd.getOptionValue('t'));
057    this.snapshotName = cmd.getOptionValue('n');
058    String snapshotTypeName = cmd.getOptionValue('s');
059    if (snapshotTypeName != null) {
060      snapshotTypeName = snapshotTypeName.toUpperCase(Locale.ROOT);
061      this.snapshotType = SnapshotType.valueOf(snapshotTypeName);
062    }
063  }
064
065  @Override
066  protected int doWork() throws Exception {
067    try (Connection connection = ConnectionFactory.createConnection(getConf());
068      Admin admin = connection.getAdmin()) {
069      admin.snapshot(new SnapshotDescription(snapshotName, tableName, snapshotType));
070    } catch (Exception e) {
071      System.err.println("failed to take the snapshot: " + e.getMessage());
072      return -1;
073    }
074    return 0;
075  }
076}