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