Automatically Update Maxmind's GeoIP Databases

This tutorial contains an .sh script used to automatically update the maxmind GeoIP databases.

1646 views
d

By. Jacob

Edited: 2022-02-24 00:13

Note. This no longer works, explanation from MaxMind's blog:

Starting December 30, 2019, downloads will no longer be served from our public GeoLite2 page, from geolite.maxmind.com/download/geoip/database/\*, or from any other public URL. See the section below for steps on how to migrate to the new download mechanism.

Source: https://blog.maxmind.com/2019/12/significant-changes-to-accessing-and-using-geolite2-databases

You can automatically download maxmind IP geolocation databases and have them moved to the desired local location, to do this, simply make a .sh script to complete the task for you.

If needed, you can even setup a cron job to run the script automatically, but I do not recommend doing this without including some verification in the script. Currently, you should manually verify that the downloaded files has been installed in the correct location. This can be done with a simple ls -l command:

ls -l /usr/share/GeoIP/

You might also want to verify the databases themselves, but that is beyond this tutorial.

Automating maxmind database updates

I already created a semi-automatic script for updating my maxmind databases. I prefer to call this manually, since I can not be sure that mixmind will not change something on their part.

The script will automatically download maxmind's City and Country databases and try to extract- and move them to /usr/share/GeoIP/

The home (~) folder is used for temporary files. If you do not have a Downloads folder, one will be created.

The script

#!/bin/sh

# Create work directories if they are missing
mkdir -p ~/Downloads/GeoIPtmp/

# Download .gz files
wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz -P ~/Downloads/
wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz -P ~/Downloads/

# Extract files to a temp location
tar -xvzf ~/Downloads/GeoLite2-City.tar.gz -C ~/Downloads/GeoIPtmp/
tar -xvzf ~/Downloads/GeoLite2-Country.tar.gz -C ~/Downloads/GeoIPtmp/

# Remove downloaded .gz files
rm ~/Downloads/GeoLite2-City.tar.gz
rm ~/Downloads/GeoLite2-Country.tar.gz

# Remove old databases
rm /usr/share/GeoIP/*.*

# Move the database files to /usr/share/GeoIP/
find ~/Downloads/GeoIPtmp/ -type f -print0 | xargs -0 mv -t /usr/share/GeoIP/ --backup=numbered

# Remove the temporary files
rm ~/Downloads/GeoIPtmp/ -R

Tell us what you think:

  1. An in-dept look at the use of headings (h1-h6) and sections in HTML pages.
  2. Pagination can be a confusing thing to get right both practically and programmatically. I have put a lot of thought into this subject, and here I am giving you a few of the ideas I have been working with.
  3. The best way to deal with a trailing question mark is probably just to make it a bad request, because it is a very odd thing to find in a request URL.
  4. How to optimize image-loading and automatically include width and height attributes on img elements with PHP.
  5. HTTP headers are not case-sensitive, so we are free to convert them to all-lowercase in our applications.

More in: Web development