Share via:

Automatically Update Maxmind's GeoIP Databases

This tutorial contains a .sh script used to automatically update maxmind's GeoIP databases.

47 views

Edited: 2019-12-05 12:12

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

Comments