How to automatically update your GeoLite databases (Linux)

I’ve been using this script to automatically update our MaxMind GeoLiteCountry and GeoLiteCity databases and thought it might be useful for others.

I am using the PHP PECL GeoIP plugin. I’m not sure if this will work for the other Piwik methods as I have not tried the other methods. I created my geoip data folder at /usr/share/geoip and pointed set my PHP “geoip.custom_directory” setting to that path. If you are using a different path, you’ll need to modify the script file in step 1.

  1. Create a script file in a location of your choice with the contents listed below. My script file is /opt/updateGeoLiteCity. Make sure that your root user has execute permissions to the script. If you use a different location, you will need to update the cron job in step 2.

#!/bin/bash

cd /usr/share/geoip
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz || { echo 'Could not download GeoLiteCountry, exiting.' ; exit 1; }
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz || { echo 'Could not download GeoLiteCity, exiting.' ; exit 1; }
gunzip -f GeoIP.dat.gz
gunzip -f GeoLiteCity.dat.gz
echo "y"|mv GeoLiteCity.dat GeoIPCity.dat

  1. Create a cron job with the following settings:

5 8 * * 3 root [ `date +\%d` -le 7 ] && /opt/updateGeoLiteCity

This will run the job on the first Wednesday of the month at 8:05am. MaxMind does not promise a specific time the updates will be ready, so I gave it enough padding so they should have the update released by the time my script runs. The script attempts to download the newest files, unzip them, and change the name of one of the files.

Hi,

i have running you´re howto. Can i check if it works correctly?
In syslog no errors appears. The line seems to be done correctly.

The bash file works manually perfect.

But the cronjob?
What means the line:

[ date +\%d -le 7 ]

LG

5kyy,

[ date +\%d -le 7 ] is a logic check to make sure the day portion of the date is less than or equal to 7. The cron job only runs on Wednesdays and this part ensures the query only runs on the first Wednesday of the month. The update script will run only if that condition is satisfied.

Hi,
GeoLite Legacy databases are discontinued since Jan 2nd, is there a way to still proceed?
Thanks!

Hi,

Since Matomo 3.5 or 3.6 (I think) the GeoIP plugin in Matomo has been replace with the new GeoIP2 plugin that supports the new format.

So upgrade to the latest Matomo version and afterwards enable the GeoIP2 plugin to migrate. The new plugin is also able to (just like before) automatically download the databases.

The easiest way is that provided by your distribution, coming with configured crontabs and shell tools for lookup and manual update.

Debian, Ubuntu:

apt install geoip-database-contrib

Redhat, Centos:

yum install geoipupdate

@tassoman

As far as I know this package (at least on Debian) only downloads the legacy database and is therefore gone from the current stable release:

https://packages.debian.org/stretch/geoip-database-contrib

Yes! You’re correct, I’ve tried fetching by geoip-database-contrib_update but I got fails.

Just install geoipupdate also on Debian. It works flawless.

1 Like

Until next week:

1 Like