-
Notifications
You must be signed in to change notification settings - Fork 101
/
install_db.sh
executable file
·285 lines (249 loc) · 12.8 KB
/
install_db.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/bin/bash
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
. config/settings.ini
function database_exists () {
# /!\ Will return false if psql can't list database. Edit your pg_hba.conf
# as appropriate.
if [ -z $1 ]
then
# Argument is null
return 0
else
# Grep db name in the list of database
sudo -n -u postgres -s -- psql -tAl | grep -q "^$1|"
return $?
fi
}
if database_exists $db_name
then
if $drop_apps_db
then
echo "Drop database..."
sudo -n -u postgres -s dropdb $db_name
else
echo "Database exists but the settings file indicate that we don't have to drop it."
fi
fi
if [ ! -d "log" ]; then
mkdir log
fi
if ! database_exists $db_name
then
echo "Creating GeoNature database..."
echo "--------------------" &> log/install_db.log
echo "Creating GeoNature database" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
sudo -n -u postgres -s createdb -O $user_pg $db_name
echo "Adding PostGIS and PLPGSQL extensions..."
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "Adding PostGIS and PLPGSQL extensions" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
sudo -n -u postgres -s psql -d $db_name -c "CREATE EXTENSION IF NOT EXISTS postgis;" &>> log/install_db.log
sudo -n -u postgres -s psql -d $db_name -c "CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';" &>> log/install_db.log
sudo -n -u postgres -s psql -d $db_name -c 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp";' &>> log/install_db.log
# Mise en place de la structure de la base et des données permettant son fonctionnement avec l'application
echo "GRANT..."
cp data/grant.sql /tmp/grant.sql
sudo sed -i "s/MYPGUSER/$user_pg/g" /tmp/grant.sql
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "GRANT" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
sudo -n -u postgres -s psql -d $db_name -f /tmp/grant.sql &>> log/install_db.log
echo "Getting and creating USERS schema (utilisateurs)..."
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "Creating USERS schema (utilisateurs)" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
cd data/utilisateurs
wget https://raw.githubusercontent.com/PnEcrins/UsersHub/$usershub_release/data/usershub.sql
cd ../..
export PGPASSWORD=$user_pg_pass;psql -h $db_host -U $user_pg -d $db_name -f data/utilisateurs/usershub.sql &>> log/install_db.log
export PGPASSWORD=$user_pg_pass;psql -h $db_host -U $user_pg -d $db_name -f data/utilisateurs/create_view_utilisateurs.sql &>> log/install_db.log
echo "Download and extract taxref file..."
cd data/taxonomie/inpn
wget https://raw.githubusercontent.com/PnX-SI/TaxHub/$taxhub_release/data/inpn/data_inpn_v9_taxhub.sql
array=( TAXREF_INPN_v9.0.zip ESPECES_REGLEMENTEES_20161103.zip LR_FRANCE_20160000.zip )
for i in "${array[@]}"
do
if [ ! -f '/tmp/'$i ]
then
wget http://geonature.fr/data/inpn/taxonomie/$i -P /tmp
else
echo $i exists
fi
done
unzip /tmp/TAXREF_INPN_v9.0.zip -d /tmp
unzip /tmp/ESPECES_REGLEMENTEES_20161103.zip -d /tmp
unzip /tmp/LR_FRANCE_20160000.zip -d /tmp
cd ..
echo "Getting 'taxonomie' schema creation scripts..."
wget https://raw.githubusercontent.com/PnX-SI/TaxHub/$taxhub_release/data/taxhubdb.sql
wget https://raw.githubusercontent.com/PnX-SI/TaxHub/$taxhub_release/data/taxhubdata.sql
wget https://raw.githubusercontent.com/PnX-SI/TaxHub/$taxhub_release/data/taxhubdata_taxon_example.sql
wget https://raw.githubusercontent.com/PnX-SI/TaxHub/$taxhub_release/data/materialized_views.sql
cd ../..
echo "Creating 'taxonomie' schema..."
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "Creating 'taxonomie' schema" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
export PGPASSWORD=$user_pg_pass;psql -h $db_host -U $user_pg -d $db_name -f data/taxonomie/taxhubdb.sql &>> log/install_db.log
echo "Inserting INPN taxonomic data... (This may take a few minutes)"
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "Inserting INPN taxonomic data" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
sudo -n -u postgres -s psql -d $db_name -f data/taxonomie/inpn/data_inpn_v9_taxhub.sql &>> log/install_db.log
echo "Creating dictionaries data for taxonomic schema..."
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "Creating dictionaries data for taxonomic schema" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
export PGPASSWORD=$user_pg_pass;psql -h $db_host -U $user_pg -d $db_name -f data/taxonomie/taxhubdata.sql &>> log/install_db.log
echo "Inserting sample dataset of taxons for taxonomic schema..."
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "Inserting sample dataset of taxons for taxonomic schema" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
export PGPASSWORD=$user_pg_pass;psql -h $db_host -U $user_pg -d $db_name -f data/taxonomie/taxhubdata_taxon_example.sql &>> log/install_db.log
echo "Creating a view that represent the taxonomic hierarchy..."
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "Creating a view that represent the taxonomic hierarchy" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
export PGPASSWORD=$user_pg_pass;psql -h $db_host -U $user_pg -d $db_name -f data/taxonomie/materialized_views.sql &>> log/install_db.log
echo "Creating 'meta' schema..."
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "Creating 'meta' schema" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
export PGPASSWORD=$user_pg_pass;psql -h $db_host -U $user_pg -d $db_name -f data/core/meta.sql &>> log/install_db.log
echo "Creating 'nomenclatures' schema..."
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "Creating 'nomenclatures' schema" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
export PGPASSWORD=$user_pg_pass;psql -h $db_host -U $user_pg -d $db_name -f data/core/nomenclatures.sql &>> log/install_db.log
echo "Inserting 'nomenclatures' data..."
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "Inserting 'nomenclatures' data" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
cp data/core/data_nomenclatures.sql /tmp/data_nomenclatures.sql
sudo sed -i "s/MYDEFAULTLANGUAGE/$default_language/g" /tmp/data_nomenclatures.sql
export PGPASSWORD=$user_pg_pass;psql -h $db_host -U $user_pg -d $db_name -f /tmp/data_nomenclatures.sql &>> log/install_db.log
echo "Creating 'medias' schema..."
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "Creating 'medias' schema" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
export PGPASSWORD=$user_pg_pass;psql -h $db_host -U $user_pg -d $db_name -f data/core/medias.sql &>> log/install_db.log
echo "Creating 'synthese' schema..."
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "Creating 'synthese' schema" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
cp data/core/synthese.sql /tmp/synthese.sql
sudo sed -i "s/MYLOCALSRID/$srid_local/g" /tmp/synthese.sql
export PGPASSWORD=$user_pg_pass;psql -h $db_host -U $user_pg -d $db_name -f /tmp/synthese.sql &>> log/install_db.log
echo "Creating 'ref_geo' schema..."
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "Creating 'ref_geo' schema..." &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
cp data/core/ref_geo.sql /tmp/ref_geo.sql
sudo sed -i "s/MYLOCALSRID/$srid_local/g" /tmp/ref_geo.sql
export PGPASSWORD=$user_pg_pass;psql -h $db_host -U $user_pg -d $db_name -f /tmp/ref_geo.sql &>> log/install_db.log
if $install_sig_layers
then
echo "Insert default French municipalities (IGN admin-express)"
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "Insert default French municipalities (IGN admin-express)" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
if [ ! -f '/tmp/communes_fr_admin_express_2017-06.zip' ]
then
wget --cache=off http://geonature.fr/data/ign/communes_fr_admin_express_2017-06.zip -P /tmp
else
echo "/tmp/communes_fr_admin_express_2017-06.zip already exist"
fi
unzip /tmp/communes_fr_admin_express_2017-06.zip -d /tmp
sudo -n -u postgres -s psql -d $db_name -f /tmp/communes_fr.sql &>> log/install_db.log
fi
if $install_default_dem
then
echo "Insert default French DEM (IGN 250m BD alti)"
echo "" &>> log/install_db.log
echo "" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "Insert default French DEM (IGN 250m BD alti)" &>> log/install_db.log
echo "--------------------" &>> log/install_db.log
echo "" &>> log/install_db.log
if [ ! -f '/tmp/BDALTIV2_2-0_250M_ASC_LAMB93-IGN69_FRANCE_2017-06-21.zip' ]
then
wget --cache=off http://geonature.fr/data/ign/BDALTIV2_2-0_250M_ASC_LAMB93-IGN69_FRANCE_2017-06-21.zip -P /tmp
else
echo "/tmp/BDALTIV2_2-0_250M_ASC_LAMB93-IGN69_FRANCE_2017-06-21.zip already exist"
fi
unzip /tmp/BDALTIV2_2-0_250M_ASC_LAMB93-IGN69_FRANCE_2017-06-21.zip -d /tmp
#gdalwarp -t_srs EPSG:$srid_local /tmp/BDALTIV2_250M_FXX_0098_7150_MNT_LAMB93_IGN69.asc /tmp/dem.tif &>> log/install_db.log
export PGPASSWORD=$user_pg_pass;raster2pgsql -s $srid_local -c -C -I -M -d -t 100x100 /tmp/BDALTIV2_250M_FXX_0098_7150_MNT_LAMB93_IGN69.asc ref_geo.dem|psql -h $db_host -U $user_pg -d $db_name &>> log/install_db.log
echo "Vectorisation of dem raster. This may take a few minutes..."
sudo -n -u postgres -s psql -d $db_name -c "INSERT INTO ref_geo.dem_vector (geom, val) SELECT (ST_DumpAsPolygons(rast)).* FROM ref_geo.dem;" &>> log/install_db.log
echo "Create dem vector spatial index. This may take a few minutes..."
sudo -n -u postgres -s psql -d $db_name -c "CREATE INDEX index_dem_vector_geom ON ref_geo.dem_vector USING gist (geom);" &>> log/install_db.log
fi
# Suppression des fichiers : on ne conserve que les fichiers compressés
echo "Cleaning files..."
rm /tmp/*.txt
rm /tmp/*.csv
rm data/utilisateurs/usershub.sql
rm data/taxonomie/taxhubdb.sql
rm data/taxonomie/materialized_views.sql
rm data/taxonomie/taxhubdata.sql
rm data/taxonomie/taxhubdata_taxon_example.sql
rm data/taxonomie/inpn/data_inpn_v9_taxhub.sql
rm /tmp/communes_fr.sql
rm /tmp/BDALTIV2_250M_FXX_0098_7150_MNT_LAMB93_IGN69.asc
rm /tmp/IGNF_BDALTIr_2-0_ASC_250M_LAMB93_IGN69_FRANCE.html
# rm /tmp/dem.tif
echo "Permission on log folder..."
chmod -R 777 log
fi