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.


cd /usr/share/geoip
wget || { echo 'Could not download GeoLiteCountry, exiting.' ; exit 1; }
wget || { 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.



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 ]




[ 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.