Preparing development version 2.4.4-SNAPSHOT
[hbase.git] / bin / hbase-cleanup.sh
blob3a764df9b99067d949dcb3ab3940c1b736c0783b
1 #!/usr/bin/env bash
3 #/**
4 # * Licensed to the Apache Software Foundation (ASF) under one
5 # * or more contributor license agreements. See the NOTICE file
6 # * distributed with this work for additional information
7 # * regarding copyright ownership. The ASF licenses this file
8 # * to you under the Apache License, Version 2.0 (the
9 # * "License"); you may not use this file except in compliance
10 # * with the License. You may obtain a copy of the License at
11 # *
12 # * http://www.apache.org/licenses/LICENSE-2.0
13 # *
14 # * Unless required by applicable law or agreed to in writing, software
15 # * distributed under the License is distributed on an "AS IS" BASIS,
16 # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # * See the License for the specific language governing permissions and
18 # * limitations under the License.
19 # */
21 # cleans hbase related data from zookeeper and hdfs if no hbase process is alive.
23 # Environment Variables
25 # HBASE_REGIONSERVERS File naming remote hosts.
26 # Default is ${HADOOP_CONF_DIR}/regionservers
27 # HADOOP_CONF_DIR Alternate conf dir. Default is ${HADOOP_HOME}/conf.
28 # HBASE_CONF_DIR Alternate hbase conf dir. Default is ${HBASE_HOME}/conf.
29 # HBASE_SLAVE_SLEEP Seconds to sleep between spawning remote commands.
30 # HBASE_SLAVE_TIMEOUT Seconds to wait for timing out a remote command.
31 # HBASE_SSH_OPTS Options passed to ssh when running remote commands.
34 usage="Usage: hbase-cleanup.sh (--cleanZk|--cleanHdfs|--cleanAll|--cleanAcls)"
36 bin=`dirname "$0"`
37 bin=`cd "$bin">/dev/null; pwd`
39 # This will set HBASE_HOME, etc.
40 . "$bin"/hbase-config.sh
42 case $1 in
43 --cleanZk|--cleanHdfs|--cleanAll|--cleanAcls)
44 matches="yes" ;;
45 *) ;;
46 esac
47 if [ $# -ne 1 -o "$matches" = "" ]; then
48 echo $usage
49 exit 1;
52 format_option=$1;
54 zparent=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.parent`
55 if [ "$zparent" == "null" ]; then zparent="/hbase"; fi
57 hrootdir=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool hbase.rootdir`
58 if [ "$hrootdir" == "null" ]; then hrootdir="file:///tmp/hbase-${USER}/hbase"; fi
60 check_for_znodes() {
61 command=$1;
62 case $command in
63 regionservers)
64 zchild=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.rs`
65 if [ "$zchild" == "null" ]; then zchild="rs"; fi
67 backupmasters)
68 zchild=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.backup.masters`
69 if [ "$zchild" == "null" ]; then zchild="backup-masters"; fi
71 esac
72 znodes=`"$bin"/hbase zkcli ls $zparent/$zchild 2>&1 | tail -1 | sed "s/\[//" | sed "s/\]//"`
73 if [ "$znodes" != "" ]; then
74 echo -n "ZNode(s) [${znodes}] of $command are not expired. Exiting without cleaning hbase data."
75 echo #force a newline
76 exit 1;
77 else
78 echo -n "All ZNode(s) of $command are expired."
80 echo #force a newline
83 execute_zk_command() {
84 command=$1;
85 "$bin"/hbase zkcli $command 2>&1
88 execute_hdfs_command() {
89 command=$1;
90 "$bin"/hbase org.apache.hadoop.fs.FsShell $command 2>&1
93 execute_clean_acls() {
94 command=$1;
95 "$bin"/hbase org.apache.hadoop.hbase.zookeeper.ZkAclReset $command 2>&1
98 clean_up() {
99 case $1 in
100 --cleanZk)
101 execute_zk_command "rmr ${zparent}";
103 --cleanHdfs)
104 execute_hdfs_command "-rm -R ${hrootdir}"
106 --cleanAll)
107 execute_zk_command "rmr ${zparent}";
108 execute_hdfs_command "-rm -R ${hrootdir}"
110 --cleanAcls)
111 execute_clean_acls;
115 esac
118 check_znode_exists() {
119 command=$1
120 "$bin"/hbase zkcli stat $command 2>&1 | grep "Node does not exist\|Connection refused"
123 check_znode_exists $zparent
124 if [ $? -ne 0 ]; then
125 # make sure the online region server(s) znode(s) have been deleted before continuing
126 check_for_znodes regionservers
127 # make sure the backup master(s) znode(s) has been deleted before continuing
128 check_for_znodes backupmasters
129 # make sure the master znode has been deleted before continuing
130 zmaster=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.master`
131 if [ "$zmaster" == "null" ]; then zmaster="master"; fi
132 zmaster=$zparent/$zmaster
133 check_znode_exists $zmaster
134 if [ $? -ne 0 ]; then
135 echo -n "Master ZNode is not expired. Exiting without cleaning hbase data."
136 echo #force a new line
137 exit 1
138 else
139 echo "Active Master ZNode also expired."
141 echo #force a newline
142 else
143 echo "HBase parent znode ${zparent} does not exist."
146 # cleans zookeeper and/or hdfs data.
147 clean_up $format_option