-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextract_table.sh
executable file
·227 lines (204 loc) · 5.64 KB
/
extract_table.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash
#
# MySQL Dumpfile Table Extractor v1.0
#
# This script can be used to extract tables from a MySQL dumpfile
#
# Copyright 2010, Joyce Babu ( http://www.joycebabu.com/ )
# Released under the MIT, BSD and GPL Licenses.
#
# Visit http://www.joycebabu.com/blog/extract-tables-from-mysql-dumpfile.html for updates
#
# First release 2010-12-02
get_dump_file(){
echo "Enter the path to your dump file. Type QUIT to exit."
while [ -z $DUMP_FILE ]; do
echo -n "> "
read INPUT
if [ $INPUT = "QUIT" ]; then
exit_success
exit 0
else
check_dump_file $INPUT
if [ $? -eq 0 ]; then
DUMP_FILE=$INPUT
fi
fi
done
}
check_dump_file(){
local INPUT=$1
if [ -z $INPUT ]; then
echo "[Error] Filename cannot be empty"
elif [ ! -f $INPUT ]; then
echo "[Error] $INPUT does not exist"
elif [ ! -r $INPUT ]; then
echo "[Error] $INPUT is not readable"
else
return 0
fi
return 1
}
generate_table_list(){
local DUMP_FILE=$1
echo "Generating list of tables in $DUMP_FILE"
local i=0
for table in `grep 'Table structure' "$DUMP_FILE" | cut -d'\`' -f2`; do
TABLE_LIST[$i]=$table
i=$(($i+1))
done
TABLE_COUNT=$i
if [ $TABLE_COUNT -lt 1 ]; then
echo "[ERROR] $DUMP_FILE is not a valid mysqldump file."
return 1
else
return 0
fi
}
extract(){
local DUMP_FILE=$1
local INPUT1=$2
local INPUT2=$3
local i
local index1
local index2
local tmp
local count
# Check whether input is numeric
expr $INPUT1 + 1 2> /dev/null
if [ $? = 0 ]; then
# Get array index
index1=$(($INPUT1-1))
else
# Tablename is specified. Search for index.
for index1 in "${!TABLE_LIST[@]}" ""; do [[ ${TABLE_LIST[index1]} = $INPUT1 ]] && break; done
fi
# Input 2
if [ ! -z $INPUT2 ]; then
# Check whether input is numeric
expr $INPUT2 + 1 2> /dev/null
if [ $? = 0 ]; then
# Get array index
index2=$(($INPUT2-1))
else
# Tablename is specified. Search for index.
for index2 in "${!TABLE_LIST[@]}" ""; do [[ ${TABLE_LIST[index2]} = $INPUT2 ]] && break; done
fi
else
# not specified. use INPUT1
index2=$index1
fi
if [ -z $index1 ]; then
echo "[ERROR] Invalid input '$INPUT1'. Not a valid table or index."
elif [ -z $index2 ]; then
echo "[ERROR] Invalid input '$INPUT2'. Not a valid table or index."
else
# Ensure index1 <= index2
if [ $index1 -gt $index2 ]; then
tmp=$index1
index1=$index2
index2=$tmp
fi
TABLE1=${TABLE_LIST[index1]}
BEGIN_PATTERN="/-- Table structure for table \`$TABLE1\`/"
# Increment index2 to find the next tablename
index2=$(($index2+1))
count=$(($index2-$index1))
if [ $index2 -lt $TABLE_COUNT ]; then
TABLE2=${TABLE_LIST[index2]}
END_PATTERN="/-- Table structure for table \`$TABLE2\`/"
else
END_PATTERN='$'
fi
OUTPUT_FILE="$TABLE1.sql"
i=1
while [ -s $OUTPUT_FILE ]; do
OUTPUT_FILE="$TABLE1.sql.$i"
i=$(($i+1))
done
# Extract the tables
sed -ne "${BEGIN_PATTERN},${END_PATTERN}p" $DUMP_FILE > $OUTPUT_FILE
if [ -s $OUTPUT_FILE ]; then
if [ $count -eq 1 ]; then
echo "[SUCCESS] Table '$TABLE1' was extracted to $OUTPUT_FILE"
else
echo "[SUCCESS] Following $count tables were extracted to $OUTPUT_FILE"
for ((i=$(($index1+1)); i <= index2 ; i++)); do
echo "$i. ${TABLE_LIST[i-1]}"
done
fi
return 0
else
echo "[ERROR] Failed to extract table"
fi
fi
return 1
}
exit_success(){
echo "=============================================================================="
echo "| Thank you for using the script. For updates visit |"
echo "| http://www.joycebabu.com/blog/extract-tables-from-mysql-dumpfile.html |"
echo "=============================================================================="
}
TABLE_COUNT=0
echo "=============================================================================="
echo "| Welcome to mysql table extraction script. |"
echo "=============================================================================="
# If filename and tablename were specified from commandline, extract and exit
if [ $# -ge 2 ]; then
check_dump_file $1
if [ $? -eq 0 ]; then
generate_table_list $1
fi
if [ $? -eq 0 ]; then
extract $1 $2 $3
fi
exit $?
fi
# Get the filename if not already specified
if [ -z $1 ]; then
echo "You have not specified a file name to extract tables."
get_dump_file
else
DUMP_FILE=$1
fi
# Generate table list and wait for user input
while [ $TABLE_COUNT -le 0 ]; do
generate_table_list $DUMP_FILE
if [ $? -ne 0 ]; then
get_dump_file
fi
done
# List the tables
INPUT="LIST"
while [ 1 ]; do
if [ $INPUT = "QUIT" ]; then
exit_success
exit 0
elif [ $INPUT = "LIST" ]; then
for ((i=1; i <= TABLE_COUNT ; i++)); do
echo "$i. ${TABLE_LIST[i-1]}"
done
elif [ $INPUT = "EXTRACT" ]; then
echo $INPUT
echo $TABLE_COUNT
for ((i=1; i <= TABLE_COUNT ; i++)); do
echo "Extracting ${TABLE_LIST[i-1]}"
extract $DUMP_FILE ${TABLE_LIST[i-1]}
done
else
extract $DUMP_FILE $INPUT $INPUT2
fi
echo ""
echo "=============================================================================="
echo "| Usage: |"
echo "| tablename [ Extracts single table by tablename or index ] |"
echo "| tablename1 tablename2 [ Extracts all tables from table1 table2 ] |"
echo "| EXTRACT [ Extract all tables into individual files ] |"
echo "| LIST [ List all tables ] |"
echo "| QUIT [ Exit from script ] |"
echo "=============================================================================="
echo -n "> "
read INPUT INPUT2
echo ""
done