forked from codigofuerte/GeoNames-MySQL-DataImport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geonames_importer.sh
executable file
·151 lines (131 loc) · 5.19 KB
/
geonames_importer.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
#!/bin/bash
# Default values for database variables.
dbhost="localhost"
dbport=3306
dbname="geonames"
dir=$( cd "$( dirname "$0" )" && pwd )
logo() {
echo "================================================================================================"
echo " G E O N A M E S D A T A I M P O R T E R "
echo "================================================================================================"
}
usage() {
logo
echo "Usage 1: " $0 "--download-data"
echo "In this mode the current GeoNames.org's dumps are downloaded to the local machine."
echo
echo "Usage 2: " $0 "-a <action> -u <user> -p <password> -h <host> -r <port> -n <dbname>"
echo " This is to operate with the geographic database"
echo " Where <action> can be one of this: "
echo " download-data Downloads the last packages of data available in GeoNames."
echo " create-db Creates the mysql database structure with no data."
echo " create-tables Creates the tables in the current database. Useful if we want to import them in an exsiting db."
echo " import-dumps Imports geonames data into db. A database is previously needed for this to work."
echo " drop-db Removes the db completely."
echo " truncate-db Removes geonames data from db."
echo
echo " The rest of parameters indicates the following information:"
echo " -u <user> User name to access database server."
echo " -p <password> User password to access database server."
echo " -h <host> Data Base Server address (default: localhost)."
echo " -r <port> Data Base Server Port (default: 3306)"
echo " -n <dbname> Data Base Name for the geonames.org data (default: geonames)"
echo "================================================================================================"
exit -1
}
download_geonames_data() {
echo "Downloading GeoNames.org data..."
wget http://download.geonames.org/export/dump/allCountries.zip
wget http://download.geonames.org/export/dump/alternateNames.zip
wget http://download.geonames.org/export/dump/hierarchy.zip
wget http://download.geonames.org/export/dump/admin1CodesASCII.txt
wget http://download.geonames.org/export/dump/admin2Codes.txt
wget http://download.geonames.org/export/dump/featureCodes_en.txt
wget http://download.geonames.org/export/dump/timeZones.txt
wget http://download.geonames.org/export/dump/countryInfo.txt
wget -O allCountries_zip.zip http://download.geonames.org/export/zip/allCountries.zip
unzip allCountries.zip
unzip alternateNames.zip
unzip hierarchy.zip
unzip allCountries_zip.zip -d ./zip/
rm allCountries.zip
rm alternateNames.zip
rm hierarchy.zip
rm allCountries_zip.zip
}
if [ $# -lt 1 ]; then
usage
exit 1
fi
logo
# Deals operation mode 1 (Download data bundles from geonames.org)
if { [ $# == 1 ] && [ "$1" == "--download-data" ]; } then
download_geonames_data
exit 0
fi
# Deals with operation mode 2 (Database issues...)
# Parses command line parameters.
while getopts "a:u:p:h:r:n:" opt;
do
case $opt in
a) action=$OPTARG ;;
u) dbusername=$OPTARG ;;
p) dbpassword=$OPTARG ;;
h) dbhost=$OPTARG ;;
r) dbport=$OPTARG ;;
n) dbname=$OPTARG ;;
esac
done
case $action in
download-data)
download_geonames_data
exit 0
;;
esac
if [ -z $dbusername ]; then
echo "No user name provided for accessing the database. Please write some value in parameter -u..."
exit 1
fi
if [ -z $dbpassword ]; then
echo "No user password provided for accessing the database. Please write some value in parameter -p..."
exit 1
fi
echo "Database parameters being used..."
echo "Orden: " $action
echo "UserName: " $dbusername
echo "Password: " $dbpassword
echo "DB Host: " $dbhost
echo "DB Port: " $dbport
echo "DB Name: " $dbname
case "$action" in
create-db)
echo "Creating database $dbname..."
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword -Bse "DROP DATABASE IF EXISTS $dbname;"
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword -Bse "CREATE DATABASE $dbname DEFAULT CHARACTER SET utf8;"
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword -Bse "USE $dbname;"
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword $dbname < $dir/geonames_db_struct.sql
;;
create-tables)
echo "Creating tables for database $dbname..."
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword -Bse "USE $dbname;"
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword $dbname < $dir/geonames_db_struct.sql
;;
import-dumps)
echo "Importing geonames dumps into database $dbname"
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword --local-infile=1 $dbname < $dir/geonames_import_data.sql
;;
drop-db)
echo "Dropping $dbname database"
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword -Bse "DROP DATABASE IF EXISTS $dbname;"
;;
truncate-db)
echo "Truncating \"geonames\" database"
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword $dbname < $dir/geonames_truncate_db.sql
;;
esac
if [ $? == 0 ]; then
echo "[OK]"
else
echo "[FAILED]"
fi
exit 0