[ Home ] [ GeoIP ] [ Tools ] [ Tips ]

Updating the GeoIP database

Essentially the LEAF packages are just gzipped tar files. I have created the following script that checks for updates of the GeoIP database and automatically creates a new LEAF package from it. The script uses curl and csv2bin, both unavailable on a LEAF box. You will need a second Linux box therefore to run the script.
#!/bin/bash

#  
#  GeoIP update script


#GEO_CSV="http://www.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip"
GEO_CSV="http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip"
CSV2BIN="/usr/local/sbin/csv2bin"
LRP_ROOT="/home/leaf/current"

function update_db 
{
  currentdir=`pwd`
  cd /var/geoip
  curl -s "${GEO_CSV}" --output /var/geoip/GeoIPCountryCSV.zip
  unzip -o /var/geoip/GeoIPCountryCSV.zip -d /var/geoip > /dev/nul
  ${CSV2BIN} /var/geoip/GeoIPCountryWhois.csv
  cd ${currentdir}
  rm /var/geoip/GeoIPCountryCSV.zip
  
  if [[ ! -d /var/lib/lrpkg ]]; then
    mkdir /var/lib/lrpkg
    chmod 755 /var/lib/lrpkg
    chown root.root /var/lib/lrpkg
  else
    rm /var/lib/lrpkg/*
  fi
  
  cut -d " " -f 3-6 /var/geoip/geoipdb.last | sed 's/ /-/' | sed 's/ /-/' > /var/lib/lrpkg/geoipdb.version
  today=`date +%d-%b-%Y`
  echo "GeoIP database" > /var/lib/lrpkg/geoipdb.help
  echo "Homepage: http://www.maxmind.com/" >> /var/lib/lrpkg/geoipdb.help
  echo "LEAF package by [email protected], ${today}" >> /var/lib/lrpkg/geoipdb.help

  cd /
  tar -c var/geoip/geoipdb.bin var/geoip/geoipdb.idx var/lib/lrpkg/ | gzip -9 > ${LRP_ROOT}/geoipdb.lrp
  chmod 644 ${LRP_ROOT}/geoipdb.lrp

}	

if [[ ! -d /var/geoip ]]; then
  mkdir /var/geoip
  chmod 755 /var/geoip
  chown root.root /var/geoip
  curl -Is "${GEO_CSV}" | grep "Last-Modified:" > /var/geoip/geoipdb.last 
  update_db
fi

if [[ ! -f /var/geoip/geoipdb.last ]]; then
  curl -Is "${GEO_CSV}" | grep "Last-Modified:" > /var/geoip/geoipdb.last
  update_db
fi

curl -Is "${GEO_CSV}" | grep "Last-Modified:" > /tmp/geoipdb.last 

if ! diff /tmp/geoipdb.last /var/geoip/geoipdb.last; then
  update_db
  mv /tmp/geoipdb.last /var/geoip/geoipdb.last
else
  rm /tmp/geoipdb.last
fi


Last modified: August 7, 2009