Using the GeoIP database of Maxmind with some related Apache/Nginx modules to assign and allocate your visitors based on their IP address. With the GeoIP database, you are able to deliver user based content as the website language or ban and block visitors, bots and crawler from specific countries. The possibilities are various!
In this article, We only scratch the surface for keeping our GeoIP database up to date. Aren’t you familiar with the GeoIP set up and need deeper information about the installation, configuration or optimization, follow these links:
Could the database be installed successfully, We can now begin to keep the GeoIP database up to date? To achieve this, We need a shell script to download, extract and replace the GeoIP database file on the server and a cron job with succeeding cycles does the rest.
Let’s start with the shell script
// geoip_update.sh #!/bin/sh export PATH = "/bin:/sbin:/usr/bin:/usr/sbin" DOWNLOAD_SRC = http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry DOWNLOAD_FILE = GeoIP.dat.gz DST_DIR = ~/GeoIP DST_FILE = GeoIP.dat TO_MAILADRESS = "YOUR@EMAIL.COM" SUBJECT_ERROR_MAIL = "[ERROR] GeoIP Update on $HOSTNAME" SUBJECT_SUCCESS_MAIL = "[SUCCESS] GeoIP Update on $HOSTNAME" WGET_OUTPUT = $(2>&1 wget --progress=dot:mega "$DOWNLOAD_SRC/$DOWNLOAD_FILE" -O "$DST_DIR/$DOWNLOAD_FILE") if [ $? -ne 0 ] then echo $0: "$WGET_OUTPUT" | mail -s "$SUBJECT_ERROR_MAIL" "$TO_MAILADRESS" exit 1 fi if echo "$WGET_OUTPUT" | fgrep 'saved' &> /dev/null then UNZIP_OUTPUT = $(2>&1 gunzip -v -f "$DST_DIR/$DOWNLOAD_FILE") chmod 2770 "$DST_DIR/$DST_FILE" echo $0: "$UNZIP_OUTPUT" | mail -s "$SUBJECT_SUCCESS_MAIL" "$TO_MAILADRESS" else echo $0: "$UNZIP_OUTPUT" | mail -s "$SUBJECT_ERROR_MAIL" "$TO_MAILADRESS" exit 1 fi
The cron job
// /etc/crontab 0 3 * * * ~/geoip_update.sh > /dev/null
The cron job cycle triggers each day at 3 am the
geoip_update.sh script, and We assume that the script is located in the root folder of the crontab user (root, www-data or someone else).
Your GeoIP database file should now be kept up to date automatically.