-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_sql.sh
executable file
·62 lines (48 loc) · 1.79 KB
/
batch_sql.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
#!/bin/bash
#-------------------------------------------------------------------------------
# This script loads the stack exchange community data contained in the XML files
# into MySQL. It uses the name of the directory (community) as the root name of
# the database. It checks for existing databases, so it can be used to update
# MySQL as new community data sets are downloaded, effectively syncing any
# uncompressed files the directory.
# Use stackex_unpacker.sh to uncompress the .7z files
# into appropriately named directories.
# The MySQL commands are contained in stack_xml_to_mysql.sh which must
# be in the directory. It contains a generic string, "community", which is
# updated with the name of the target directory and used to make the DATABASE.
#-------------------------------------------------------------------------------
# loop through all of the directories
for dir in */ ;
do
# get the directory name
dir="${dir%*/}"
echo "-----------------"
echo "$dir"
# make the database name
db=$dir"_stackexchange_com"
# check if database exists already
RESULT=`mysqlshow --login-path=local $db| grep -v Wildcard | grep -o $db`
if [ "$RESULT" == "$db" ];
# if database exists, report that
then
echo $'\n'
echo " Database $db already exists."
echo $'\n'
# else run the stack_xml_to_mysql.sql
# batch script and report progress
else
# report
echo $'\n'
echo "Creating Database $db."
# edit the database name
sed -i "s/community/"$dir"/g" stack_xml_to_mysql.sql
# execute SQL script
echo "Loading data..."
mysql --login-path=local < "stack_xml_to_mysql.sql"
# reset database name
sed -i "s/"$dir"/community/g" stack_xml_to_mysql.sql
# report
echo "Complete!"
echo $'\n'
fi
done