#!/bin/sh
GEOIP_MIRROR="http://geolite.maxmind.com/download/geoip/database"
GEOIPDIR=/usr/share/GeoIP
#GEOIPDIR=/tmp/geoip
TMPDIR=
EMAIL_TO="tdperry@ride-texas.org"
EMAIL_SUBJECT="[WARNING] GeoIP update error"
EMAIL_HEAD="Oh Worthy One,"
EMAIL_CONTENT="There was an error updating the following GeoIP databases:"
EMAIL_FIX="Don't forget to launch a new update manually. No new automatic update will be done until next month!"
EMAIL_FOOT="Your Slave, from Palestine, TX (USA)."
EMAIL_META="VM: server_name - DNS: server_domain.tld - DC: server_datacenter"
EMAIL_BUGGY=""
DATABASES="GeoLiteCity GeoLiteCountry/GeoIP"
if [ -d "${GEOIPDIR}" ]; then
cd $GEOIPDIR
if [ -n "${DATABASES}" ]; then
TMPDIR=$(mktemp -d geoipupdate.XXXXXXXXXX)
echo "Updating GeoIP databases..."
for db in $DATABASES; do
fname=$(basename $db)
wget --no-verbose -t 3 -T 60 "${GEOIP_MIRROR}/${db}.dat.gz" -O "${TMPDIR}/${fname}.dat.gz"
if [ "$?" = "0" ]; then
gunzip -fdc "${TMPDIR}/${fname}.dat.gz" > "${TMPDIR}/${fname}.dat"
mv "${TMPDIR}/GeoLiteCity.dat" "${GEOIPDIR}/GeoIPCity.dat"
mv "${TMPDIR}/${fname}.dat" "${GEOIPDIR}/${fname}.dat"
chmod 0644 "${GEOIPDIR}/${fname}.dat"
else
if [ "${EMAIL_BUGGY}" = "" ]; then
EMAIL_BUGGY=$db
else
EMAIL_BUGGY=${EMAIL_BUGGY}", $db"
fi
fi
done
[ -d "${TMPDIR}" ] && rm -rf $TMPDIR
if [ "${EMAIL_BUGGY}" != "" ]; then
echo "${EMAIL_HEAD}\n\n${EMAIL_CONTENT} ${EMAIL_BUGGY}\n\n${EMAIL_FIX}\n\n\n${EMAIL_FOOT}\n\n\n${EMAIL_META}" | /usr/bin/mail -s "${EMAIL_SUBJECT}" "${EMAIL_TO}"
fi
fi
fi