Shell Script To Update MaxMind GeoIP Database

by on March 29, 2009 · 4 comments

First, you need to install MaxMind C Api including geoipupdate utility.

Second, configure /usr/local/etc/GeoIP.conf file with your LicenseKey, UserId and ProductIds. A sample config file:

UserId 424242
LicenseKey zLzIYFYENTZOyAyNdSdWbEoR42
ProductIds 106

Put the following script at - /etc/cron.monthly/updategeoip.sh (cron job)

  1. #!/bin/bash
  2. # Shell Script to Update GeoIP Country database in each WebServer Jail defined by $TDIRS
  3. # -------------------------------------------------------------------------
  4. # Copyright (c) 2007 Vivek Gite <vivek@nixcraft.com>
  5. # This script is licensed under GNU GPL version 2.0 or above
  6. # -------------------------------------------------------------------------
  7. # This script is part of nixCraft shell script collection (NSSC)
  8. # Visit http://bash.cyberciti.biz/ for more information.
  9. # -------------------------------------------------------------------------
  10. GEOUPDATE=/usr/local/bin/geoipupdate
  11. GEODB=/usr/local/share/GeoIP/GeoIP.dat
  12. # ------------------
  13. # TRUE = Update in Jail
  14. # FALSE = Update only at $GEODB
  15. CHROOTED=FALSE
  16. # ------------------
  17. # DIR:user:group format, only used if CHROOTED == TRUE
  18. TDIRS="/webroot/apache/usr/local/geoip|www:www /webroot/nginx/usr/local/geoip|nginx:nginx"
  19. CHOWN=/bin/chown
  20. CP=/bin/cp
  21.  
  22. [ -f $GEOUPDATE ] && $GEOUPDATE
  23.  
  24. # Update if chrooted Apache jail...
  25. if [ "$CHROOTED" == "TRUE" ]
  26. then
  27. for i in $TDIRS
  28. do
  29. d=$(echo "$i" | cut -d'|' -f1)
  30. p=$(echo "$i" | cut -d'|' -f2)
  31. $CP -f $GEODB $d
  32. $CHOWN $p $d/$(basename $GEODB)
  33. done
  34. fi


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • Juan Giordana

    There’s a typo on the first nginx (nginix).

  • Mould Maker

    So, how to get the License Key & Product Id ?

  • Vivek Gite

    Thanks for the heads up!

  • Vivek Gite

    You can order it from the maxmind site only.

Previous Script:

Next Script: