Home / How to update the GeoIP.dat file on Debian 5

How to update the GeoIP.dat file on Debian 5

As part of looking at geotargeting with PHP and GeoIP the other day I posted how to enable the PHP GeoIP functions on Debian 5. Unfortunately there doesn’t seem to be any way to update the GeoIP.dat file automatically with more up to date IP address mapping so this post shows how it can be done manually, and provides a BASH script for automation.

Manually updating the GeoIP.dat file on Debian 5

It’s important to keep the data file up to date as directories get updated etc in order to keep as accurate as possible. On Debian 5 the file is located at /usr/share/GeoIP/GeoIP.dat

The most up to date version of the file is currently located at http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz Note that this is the current location of the file at the time of posting this article and may be subject to relocation at a later time.

Change to the /tmp directory and use wget or something similar to download the file:

cd /tmp
wget -q http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz

Then decompress it:

gzip -d GeoIP.dat.gz

and assuming it downloaded OK and decompressed OK then overwrite the existing file:

mv -f GeoIP.dat /usr/share/GeoIP/GeoIP.dat

That’s it – you now have an up to date GeoIP.dat file. At this stage it’s a really good idea to check it works as expected. Ideally you should test this out on a non-production server first just to be on the safe side.

BASH script

The following BASH script downloads the file using wget. If the was an issue downloading it there won’t be a file at /tmp hence the if [ -f ] check. If it downloaded then it’s decompressed, the old file is deleted and the new one moved where the old one was.

In my original script I didn’t have the "rm -f" line but despite the -f flag for the mv command the root aliases made it prompt anyway to see if I really did want to delete the file. Not much use for an automated cron script…

cd /tmp
wget -q http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
if [ -f GeoIP.dat.gz ]
then
    gzip -d GeoIP.dat.gz
    rm -f /usr/share/GeoIP/GeoIP.dat
    mv -f GeoIP.dat /usr/share/GeoIP/GeoIP.dat
else
    echo "The GeoIP library could not be downloaded and updated"
fi

Disclaimer

There isn’t a large amount of error checking in the above script and you could still potentially end up without a GeoIP.dat file at the end of the process. I would recommend either running this manually every now and again and making sure everything worked OK (which is what I do myself), or modifying the tests to make them a little more robust.

If there is a better way…

If there is a better way to update the GeoIP.dat file on Debian please let me know and I will update this post. I couldn’t see any way to update it using apt-get or similar.