-
Notifications
You must be signed in to change notification settings - Fork 0
/
take_snapshot.sh
168 lines (144 loc) · 4.99 KB
/
take_snapshot.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# DISCLAIMER
# Please note: This script is released for use "AS IS" without any warranties
# of any kind, including, but not limited to their installation, use, or
# performance. We disclaim any and all warranties, either express or implied,
# including but not limited to any warranty of noninfringement,
# merchantability, and/ or fitness for a particular purpose. We do not warrant
# that the technology will meet your requirements, that the operation thereof
# will be uninterrupted or error-free, or that any errors will be corrected.
#
# Any use of these scripts and tools is at your own risk. There is no guarantee
# that they have been through thorough testing in a comparable environment and
# we are not responsible for any damage or data loss incurred with their use.
#
# You are responsible for reviewing and testing any scripts you run thoroughly
# before use in any non-testing environment.
export TS=$(date "+%s")
export WORKING_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export LOGS=$WORKING_DIR/logs
export currentDate=$(date '+%Y-%m-%d-%H-%M-%S')
export UTILS=$WORKING_DIR/utils
mkdir -p $LOGS
function generate_snapshots_list_file() {
#create utils folder
mkdir -p $UTILS
if [ -f $UTILS/tableSnapshotsList.txt ]; then
echo "$currentDate $UTILS/tables.txt already exist, remove it"
rm -f $UTILS/tableSnapshotsList.txt
echo "$currentDate $UTILS/tables.txt is removed"
fi
#get hbase tables
echo 'list_snapshots' | hbase shell >>$UTILS/tableSnapshotsListTemp.txt
status=$?
if [ $status -ne 0 ]; then
echo "$currentDate Command = list_snapshots may have failed."
exit $status
fi
sed -i '/row/,$d' $UTILS/tableSnapshotsListTemp.txt
sed -i '1,/TABLE/d' $UTILS/tableSnapshotsListTemp.txt
awk '{print $1}' $UTILS/tableSnapshotsListTemp.txt>> $UTILS/tableSnapshotsList.txt
tableSnapshotsList="$UTILS/tableSnapshotsList.txt"
echo "$currentDate Stored snapshots list on $tableSnapshotsList" >>$LOGS/makesnapshot.log
}
function make_table_snapshot() {
tableName=$1
echo "$currentDate Create snapshot for table $tableName: $tableName-$currentDate" >>$LOGS/makesnapshot.log
#make snapshot for table
echo "snapshot '$tableName', '$tableName-$TS'" | hbase shell -n 2>>$LOGS/makesnapshot.log
status=$?
if [ $status -ne 0 ]; then
echo "$currentDate Snapshot for table $tableName may have failed: $status" >>$LOGS/makesnapshot.log
#if the snapshot of a table fails, quit the program
exit $status
fi
}
function slow_make_all_table_snapshot() {
echo "$currentDate Start making snapshot for all tables" >>$LOGS/makesnapshot.log
while IFS= read -r line; do
make_table_snapshot $line
done <$1
echo "$currentDate Finished making snapshot for all tables" >>$LOGS/makesnapshot.log
exit 0
}
function fast_make_all_table_snapshot() {
hbase shell $1 >>$LOGS/makesnapshot.log
}
function generate_all_tablesnapshot_command() {
if [ -f $UTILS/snapshot_commands.txt ]; then
echo "$currentDate $UTILS/snapshot_commands.txt already exist, remove it"
rm -f $UTILS/snapshot_commands.txt
echo "$currentDate $UTILS/snapshot_commands.txt is removed"
fi
while IFS= read -r line; do
echo "snapshot '$line', '$line-$TS'" >>$UTILS/snapshot_commands.txt
done <$1
echo "exit" >>$UTILS/snapshot_commands.txt
snapshotCommands=$UTILS/snapshot_commands.txt
}
function list_hbase_tables_in_file() {
#create utils folder
mkdir -p $UTILS
if [ -f $UTILS/tables.txt ]; then
echo "$currentDate $UTILS/tables.txt already exist, remove it"
rm -f $UTILS/tables.txt
echo "$currentDate $UTILS/tables.txt is removed"
fi
#get hbase tables
echo 'list' | hbase shell >>$UTILS/tables.txt
status=$?
if [ $status -ne 0 ]; then
echo "$currentDate Command = list may have failed."
exit $status
fi
#remove TABLE from the first line of file
sed -i '1,/TABLE/d' $UTILS/tables.txt
sed -i '/row/,$d' $UTILS/tables.txt
tableList="$UTILS/tables.txt"
echo "$currentDate Stored table list on $tableList" >>$LOGS/makesnapshot.log
}
if [ -z $1 ] || [ $1 == '-h' ]; then
echo "Usage: $WORKING_DIR/$0 <table>"
echo "Usage: $WORKING_DIR/$0 allfast"
echo "Usage: $WORKING_DIR/$0 allslow"
echo "Usage: $WORKING_DIR/$0 multifast /file/tables/list/tables.txt"
echo "Usage: $WORKING_DIR/$0 multislow /file/tables/list/tables.txt"
exit 1
fi
case $1 in
'allfast')
#get tables list
list_hbase_tables_in_file
#generate snapshot commands and save it in a file
generate_all_tablesnapshot_command $tableList
#Start making snapshots process
fast_make_all_table_snapshot $snapshotCommands
;;
'allslow')
#get tables list
list_hbase_tables_in_file
#Start making snapshots process
make_all_table_snapshot $tableList
;;
'multislow')
#Multi table snapshot
#Expected file of table list
make_all_table_snapshot $2
;;
'multifast')
#Multi table snapshot
#Expected file of table list
generate_all_tablesnapshot_command $2
fast_make_all_table_snapshot $snapshotCommands
;;
'generatesnapshotlist')
generate_snapshots_list_file
;;
'generatetablelist')
list_hbase_tables_in_file
;;
*)
#Make a single table snapshot
make_table_snapshot $1
exit 0
;;
esac