Skip to content

Commit 01d6d64

Browse files
author
Sergio Cruz
committed
Primera aproximación a la nueva disposición de directorios de descarga y ejecución de la aplicación.
1 parent b1cad2f commit 01d6d64

File tree

1 file changed

+81
-46
lines changed

1 file changed

+81
-46
lines changed

geonames_importer.sh

Lines changed: 81 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
#!/bin/bash
22

3+
# [BEGIN] CONFIGURATION FOR THE SCRIPT
4+
# -------------------------------------
35
# Default values for database variables.
46
dbhost="localhost"
57
dbport=3306
68
dbname="geonames"
7-
#dir=$( cd "$( dirname "$0" )" && pwd )
9+
dbusername= "root"
10+
dbpassword= "root"
811

9-
#download_folder="`pwd`/download"
10-
download_folder="`pwd`"
12+
# Default value for download folder
13+
download_folder="download"
14+
15+
# Default general dumps to download
16+
dumps= "allCountries.zip alternateNames.zip hierarchy.zip admin1CodesASCII.txt admin2Codes.txt featureCodes_en.txt timeZones.txt countryInfo.txt"
17+
# By default all postal codes ... You can specify a set of the files located at http://download.geonames.org/export/zip/
18+
postal_codes= "allCountries.zip"
19+
20+
# [END] CONFIGURATION FOR THE SCRIPT
21+
# -------------------------------------
1122

1223
logo() {
1324
echo " __________________________________________________________________________________________________________________ "
@@ -18,31 +29,39 @@ echo " | | |_ |/ _ \/ _ \| '_ \ / _` | '_ ` _ \ / _ \/ __| | | | |/ _` | __/ _`
1829
echo " | |__| | __/ (_) | | | | (_| | | | | | | __/\__ \ | |__| | (_| | || (_| | _| |_| | | | | | |_) | (_) | | | |_ "
1930
echo " \_____|\___|\___/|_| |_|\__,_|_| |_| |_|\___||___/ |_____/ \__,_|\__\__,_| |_____|_| |_| |_| .__/ \___/|_| \__| "
2031
echo " |_| "
21-
echo " __________________________________________________________________________________________________________________ "
32+
echo " __________________________________________________ v 2.0 _________________________________________________________ "
2233
}
2334

2435
usage() {
2536
logo
26-
echo "Usage: " $0 "-a <action> -u <user> -p <password> -h <host> -r <port> -n <dbname>"
27-
echo " This is to operate with the geographic database"
37+
echo "Usage: " $0 "-a <action> "
2838
echo " Where <action> can be one of this: "
29-
echo " download-data Downloads the last packages of data available in GeoNames. An additional parameter with a download directory should be used."
30-
echo " create-db Creates the mysql database structure with no data."
31-
echo " create-tables Creates the tables in the current database. Useful if we want to import them in an exsiting db."
32-
echo " import-dumps Imports geonames data into db. A database is previously needed for this to work."
33-
echo " drop-db Removes the db completely."
34-
echo " truncate-db Removes geonames data from db."
39+
echo " download-data: Downloads the last packages of data available in GeoNames. An additional parameter with a download directory should be used."
40+
echo " create-db: Creates the mysql database structure with no data."
41+
echo " create-tables: Creates the tables in the current database. Useful if we want to import them in an exsiting db."
42+
echo " import-dumps: Imports geonames data into db. A database is previously needed for this to work."
43+
echo " drop-db: Removes the db completely."
44+
echo " truncate-db: Removes geonames data from db."
3545
echo
36-
echo " The rest of parameters indicates the following information:"
37-
echo " -u <user> User name to access database server."
38-
echo " -p <password> User password to access database server."
39-
echo " -h <host> Data Base Server address (default: localhost)."
40-
echo " -r <port> Data Base Server Port (default: 3306)"
41-
echo " -n <dbname> Data Base Name for the geonames.org data (default: geonames)"
42-
echo "================================================================================================"
46+
# echo " The rest of parameters indicates the following information:"
47+
# echo " -u <user> User name to access database server."
48+
# echo " -p <password> User password to access database server."
49+
# echo " -h <host> Data Base Server address (default: localhost)."
50+
# echo " -r <port> Data Base Server Port (default: 3306)"
51+
# echo " -n <dbname> Data Base Name for the geonames.org data (default: geonames)"
4352
exit -1
4453
}
4554

55+
dump_db_params() {
56+
echo "Database parameters being used..."
57+
echo "Orden: " $action
58+
echo "UserName: " $dbusername
59+
echo "Password: " $dbpassword
60+
echo "DB Host: " $dbhost
61+
echo "DB Port: " $dbport
62+
echo "DB Name: " $dbname
63+
}
64+
4665
# download_geonames_data() {
4766
# echo "Downloading GeoNames.org data..."
4867
# download_folder="$1"
@@ -97,63 +116,79 @@ done
97116

98117
case $action in
99118
download-data)
119+
echo "STARTING DATA DOWNLOAD !!"
120+
# Checks if a download folder has been specified.
100121
if { [ "$3" != "" ]; } then
101122
if [ ! -d "$3" ]; then
102-
echo "Temporary download data folder '$2' doesn't exists. Creating it."
103-
mkdir "$3"
123+
echo "Temporary download data folder '$3' doesn't exists. Creating it."
124+
mkdir -p "$3"
125+
fi
126+
download_folder="`pwd`/$3"
127+
else
128+
if [ ! -d "$download_folder" ]; then
129+
echo "Temporary download data folder '$download_folder' doesn't exists. Creating it."
130+
mkdir -p "$download_folder"
104131
fi
105-
download_folder="`pwd`/$2"
106132
fi
107-
# Dumps general Data
108-
dumps="allCountries.zip alternateNames.zip hierarchy.zip admin1CodesASCII.txt admin2Codes.txt featureCodes_en.txt timeZones.txt countryInfo.txt"
133+
134+
# Dumps General data.
135+
echo "Downloading general files"
109136
for dump in $dumps; do
137+
echo "Downloading $dump into $download_folder"
110138
wget -c -P "$download_folder" http://download.geonames.org/export/dump/$dump
111139
if [ ${dump: -4} == ".zip" ]; then
112-
echo "Unzipping $dump..."
140+
echo "Unzipping $dump into `pwd`"
113141
unzip "$download_folder/$dump" -d "`pwd`"
114142
fi
115143
done
116-
# TODO: Dumps Postal Code Data.
144+
145+
# Dumps Postal Code data.
146+
echo "Downloading postal code information"
147+
if [ ! -d "$download_folder/zip_codes" ]; then
148+
echo "Temporary download data folder '$download_folder/zip_codes' doesn't exists. Creating it."
149+
mkdir -p "$download_folder/zip_codes"
150+
fi
151+
for postal_code_file in $postal_codes; do
152+
echo "Downloading $postal_code_file into $download_folder/zip_codes"
153+
wget -c -P "$download_folder/zip_codes" http://download.geonames.org/export/zip/$postal_code_file
154+
if [ ${dump: -4} == ".zip" ]; then
155+
echo "Unzipping Postal Code file $postal_code_file into $download_folder/zip_codes"
156+
unzip "$download_folder/zip_codes/$postal_code_file" -d "$download_folder/zip_codes"
157+
fi
158+
done
159+
echo "DATA DOWNLOAD FINISH !!"
117160
exit 0
118161
;;
119162
esac
120163

121-
if [ -z $dbusername ]; then
122-
echo "No user name provided for accessing the database. Please write some value in parameter -u..."
123-
exit 1
124-
fi
125-
126-
if [ -z $dbpassword ]; then
127-
echo "No user password provided for accessing the database. Please write some value in parameter -p..."
128-
exit 1
129-
fi
164+
# if [ -z $dbusername ]; then
165+
# echo "No user name provided for accessing the database. Please write some value in parameter -u..."
166+
# exit 1
167+
# fi
130168

131-
echo "Database parameters being used..."
132-
echo "Orden: " $action
133-
echo "UserName: " $dbusername
134-
echo "Password: " $dbpassword
135-
echo "DB Host: " $dbhost
136-
echo "DB Port: " $dbport
137-
echo "DB Name: " $dbname
169+
# if [ -z $dbpassword ]; then
170+
# echo "No user password provided for accessing the database. Please write some value in parameter -p..."
171+
# exit 1
172+
# fi
138173

139174
case "$action" in
140175
create-db)
141176
echo "Creating database $dbname..."
142177
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword -Bse "DROP DATABASE IF EXISTS $dbname;"
143178
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword -Bse "CREATE DATABASE $dbname DEFAULT CHARACTER SET utf8;"
144179
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword -Bse "USE $dbname;"
145-
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword $dbname < $dir/geonames_db_struct.sql
180+
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword $dbname < `pwd`/geonames_db_struct.sql
146181
;;
147182

148183
create-tables)
149184
echo "Creating tables for database $dbname..."
150185
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword -Bse "USE $dbname;"
151-
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword $dbname < $dir/geonames_db_struct.sql
186+
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword $dbname < `pwd`/geonames_db_struct.sql
152187
;;
153188

154189
import-dumps)
155190
echo "Importing geonames dumps into database $dbname"
156-
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword --local-infile=1 $dbname < $dir/geonames_import_data.sql
191+
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword --local-infile=1 $dbname < `pwd`/geonames_import_data.sql
157192
;;
158193

159194
drop-db)
@@ -163,7 +198,7 @@ case "$action" in
163198

164199
truncate-db)
165200
echo "Truncating \"geonames\" database"
166-
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword $dbname < $dir/geonames_truncate_db.sql
201+
mysql -h $dbhost -P $dbport -u $dbusername -p$dbpassword $dbname < `pwd`/geonames_truncate_db.sql
167202
;;
168203
esac
169204

0 commit comments

Comments
 (0)