View Javadoc

1   /**
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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.hadoop.hbase.backup;
19  
20  import java.io.IOException;
21  
22  import org.apache.commons.cli.CommandLine;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.HBaseConfiguration;
27  import org.apache.hadoop.hbase.backup.impl.BackupCommands;
28  import org.apache.hadoop.hbase.backup.impl.BackupRestoreConstants;
29  import org.apache.hadoop.hbase.backup.impl.BackupRestoreConstants.BackupCommand;
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.hbase.classification.InterfaceStability;
32  import org.apache.hadoop.hbase.util.AbstractHBaseTool;
33  import org.apache.hadoop.hbase.util.LogUtils;
34  import org.apache.hadoop.util.ToolRunner;
35  import org.apache.log4j.Level;
36  import org.apache.log4j.Logger;
37  
38  @InterfaceAudience.Private
39  @InterfaceStability.Evolving
40  public class BackupDriver extends AbstractHBaseTool {
41  
42    private static final Log LOG = LogFactory.getLog(BackupDriver.class);
43    private CommandLine cmd;
44      
45    public BackupDriver() throws IOException
46    {
47      init();
48    }
49    
50    protected void init() throws IOException {
51      // define supported options
52      addOptNoArg("debug", "Enable debug loggings");
53      addOptNoArg("all", "All tables");
54      addOptWithArg("t", "Table name");
55      addOptWithArg("b", "Bandwidth (MB/s)");
56      addOptWithArg("w", "Number of workers");
57      addOptWithArg("n", "History length");
58      addOptWithArg("set", "Backup set name");
59  
60      // disable irrelevant loggers to avoid it mess up command output
61      LogUtils.disableUselessLoggers(LOG);
62    }
63  
64    private int parseAndRun(String[] args) throws IOException {
65      String cmd = null;
66      String[] remainArgs = null;
67      if (args == null || args.length == 0) {
68        BackupCommands.createCommand(getConf(),
69          BackupRestoreConstants.BackupCommand.HELP, null).execute();
70      } else {
71        cmd = args[0];
72        remainArgs = new String[args.length - 1];
73        if (args.length > 1) {
74          System.arraycopy(args, 1, remainArgs, 0, args.length - 1);
75        }
76      }
77  
78      BackupCommand type = BackupCommand.HELP;
79      if (BackupCommand.CREATE.name().equalsIgnoreCase(cmd)) {
80        type = BackupCommand.CREATE;
81      } else if (BackupCommand.HELP.name().equalsIgnoreCase(cmd)) {
82        type = BackupCommand.HELP;
83      } else if (BackupCommand.DELETE.name().equalsIgnoreCase(cmd)) {
84        type = BackupCommand.DELETE;
85      } else if (BackupCommand.DESCRIBE.name().equalsIgnoreCase(cmd)) {
86        type = BackupCommand.DESCRIBE;
87      } else if (BackupCommand.HISTORY.name().equalsIgnoreCase(cmd)) {
88        type = BackupCommand.HISTORY;
89      } else if (BackupCommand.PROGRESS.name().equalsIgnoreCase(cmd)) {
90        type = BackupCommand.PROGRESS;
91      } else if (BackupCommand.SET.name().equalsIgnoreCase(cmd)) {
92        type = BackupCommand.SET;
93      } else {
94        System.out.println("Unsupported command for backup: " + cmd);
95        return -1;
96      }
97  
98      // enable debug logging
99      Logger backupClientLogger = Logger.getLogger("org.apache.hadoop.hbase.backup");
100     if (this.cmd.hasOption("debug")) {
101       backupClientLogger.setLevel(Level.DEBUG);
102     } else {
103       backupClientLogger.setLevel(Level.INFO);
104     }
105 
106     // TODO: get rid of Command altogether?
107     BackupCommands.Command command = BackupCommands.createCommand(getConf(), type, this.cmd);
108     if( type == BackupCommand.CREATE && conf != null) {
109       ((BackupCommands.CreateCommand) command).setConf(conf);
110     }   
111     command.execute();
112     return 0;
113   }
114 
115   @Override
116   protected void addOptions() {
117   }
118 
119   @Override
120   protected void processOptions(CommandLine cmd) {
121     this.cmd = cmd;
122   }
123 
124   @Override
125   protected int doWork() throws Exception {
126     return parseAndRun(cmd.getArgs());
127   }
128 
129   public static void main(String[] args) throws Exception {
130     Configuration conf = HBaseConfiguration.create();
131     int ret = ToolRunner.run(conf, new BackupDriver(), args);    
132     System.exit(ret);    
133   }
134   
135 
136 }