forked from codigofuerte/GeoNames-MySQL-DataImport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geonames_importer.sh
executable file
·107 lines (89 loc) · 3.25 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
#!/bin/bash
# Default values for database variables.
dbhost="localhost"
dbport=3306
dbname="geonames"
dir=$( cd "$( dirname "$0" )" && pwd )
download_folder="`pwd`/download"
logo() {
echo "================================================================================================"
echo " GEONAMES US POSTAL CODE IMPORTER "
echo "================================================================================================"
}
usage() {
logo
echo "Usage: " $0 "-u <user> -p <password> -h <host> -r <port> -n <dbname>"
echo " Actions go in order: "
echo " download-data Downloads the last packages of data available in GeoNames."
echo " drop-table If table with predefined name exists."
echo " create-table Creates the table 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
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..."
download_folder="$1"
wget -c -P "$download_folder" -O "US.zip" http://download.geonames.org/export/zip/US.zip
unzip "*.zip" -d -o ./download
rm *.zip
}
if [ $# -lt 1 ]; then
usage
exit 1
fi
logo
# 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 "download_folder=$download_folder"
download_geonames_data "$download_folder"
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
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
echo "Importing postal code dumps into database $dbname"
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword --local-infile=1 $dbname < $dir/geonames_import_data.sql
if [ $? == 0 ]; then
echo "[OK]"
else
echo "[FAILED]"
fi
exit 0