Keeping GeoIP database updated
May 4th, 2012
No comments
Here’s a simple script to install on a server to keep the GeoIP db updated.
Source code for file (e.g.) get_latest_GeoIP.sh
#!/bin/bash # getting the latest GeoLiteCity database from maxmind host=$(hostname -s) stamp=`date +"%m%Y"` # getting current month & year for GeoLiteCity filename if [ -f "/usr/share/GeoIP/GeoLiteCity_"$stamp".dat" ]; then last=`date -r /usr/share/GeoIP/GeoLiteCity_"$stamp".dat +"%m%Y"` else last=0000 fi if [ ! -f "/usr/share/GeoIP/GeoLiteCity_"$stamp".dat" ] || [ "$stamp" != "$last" ]; then # if current file doesn't exist or current file wasn't modified in the current month wget -O "/usr/share/GeoIP/GeoLiteCity.dat.gz" http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz if [ ! -s "/usr/share/GeoIP/GeoLiteCity.dat.gz" ]; then rm /usr/share/GeoIP/GeoLiteCity.dat.gz exit 1 else rm /usr/share/GeoIP/GeoLiteCity.dat gunzip /usr/share/GeoIP/GeoLiteCity.dat.gz if [ -f "/usr/share/GeoIP/GeoLiteCity_"$stamp".dat" ]; then rm /usr/share/GeoIP/GeoLiteCity_"$stamp".dat fi mv /usr/share/GeoIP/GeoLiteCity.dat "/usr/share/GeoIP/GeoLiteCity_"$stamp".dat" ln -s "/usr/share/GeoIP/GeoLiteCity_"$stamp".dat" /usr/share/GeoIP/GeoLiteCity.dat echo "Report: GeoIP Database "$stamp" has been updated on "$host". - Enjoy!" fi fi exit
… now just install a cronjob, that checks withn the first days of the month, whether there is a fresh GeoIP db to fetch.
22 3 1-10 * * ~/get_latest_GeoIP.sh > /dev/null 2>&1