Skip to content

Instantly share code, notes, and snippets.

@Snawoot
Last active August 29, 2015 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Snawoot/10965216 to your computer and use it in GitHub Desktop.
Save Snawoot/10965216 to your computer and use it in GitHub Desktop.
#!/bin/bash
cp geo.conf geo.conf.old
wget -OGeoIPCountryCSV.zip -q http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country-CSV.zip
csv_file=`unzip -qq -l GeoIPCountryCSV.zip */GeoLite2-Country-Blocks.csv | awk '{ print $4 }'`
csv_date=`unzip -qq -l GeoIPCountryCSV.zip */GeoLite2-Country-Blocks.csv | awk '{ print $2 }'`
unzip -p GeoIPCountryCSV.zip */GeoLite2-Country-Blocks.csv | tail -n +2 > GeoLite2-Country-Blocks.csv
unzip -p GeoIPCountryCSV.zip */GeoLite2-Country-Locations.csv | tail -n +2 > GeoLite2-Country-Locations.csv
tmpfile="$(mktemp)"
sqlite3 "$tmpfile" > geo.conf.new <<EOF
create table locations (geoname_id integer, code_continent text,continent_name text,country_iso_code text,country_name text,subdivision_iso_code text,subdivision_name text,city_name text,metro_code text,time_zone text);
.separator ","
.import GeoLite2-Country-Locations.csv locations
create table blocks (network_start_ip text,network_mask_length text,geoname_id text,registered_country_geoname_id text,represented_country_geoname_id text,postal_code text,latitude integer,longitude integer,is_anonymous_proxy integer,is_satellite_provider integer);
.import GeoLite2-Country-Blocks.csv blocks
select substr(network_start_ip, 8)||'/'||cast(network_mask_length-96 as text) || ' ' || country_iso_code || ';'from blocks left outer join locations on blocks.geoname_id = locations.geoname_id where network_start_ip like "::ffff:%";
EOF
if diff geo.conf.new geo.conf.old >/dev/null 2>&1
then
echo "Files are the same. No action." | mailx -s 'Geo IP data' vladislav.yarmak@tvigle.ru
else
echo "Files are different. New version dated $csv_date. Copy geo.conf.new to geo.conf and restart nginx to apply changes.
" `diff geo.conf.new geo.conf.old` | mailx -s 'Geo IP data' vladislav.yarmak@tvigle.ru
fi
rm GeoIPCountryCSV.zip "$tmpfile" GeoLite2-Country-Blocks.csv GeoLite2-Country-Locations.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment