How to update GeoIp data automatically
Hello Guys;
Here is another short but interesting application. As you may know there is being online for a long time a way to collect information based on the user IP address, there was many other intents to do this, even google implemented it on their google maps API but it never worked the way it should and they finally dropped this implementation, I hope they bring it back some time in the future.
But must of the websites use a service provided by MaxMind (maxmind.com) that for a long time is being giving these services, they have two options, one completely free where they provide a database that is 90% precise, called GeoLite and another paid called GeoIp, prices are subject to change and you should use the version you consider more appropriate to your needs, in this case we will focus on the Lite version but this same code with little modification can be used on the full version as well.
Make sure you have php geoip functions installed in the server, in case you don’t have it run the following codes.
$ yum install geoip-devel
$ pecl install geoip
Geoip.sh code
GEOIP_MIRROR="http://geolite.maxmind.com/download/geoip/database" GEOIPDIR=/var/lib/GeoIP TMPDIR= EMAIL_TO="EMAIL_TO" EMAIL_SUBJECT="[WARNING] GeoIP update error" EMAIL_HEAD="Dear Master," 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 Servant Slave." EMAIL_META="VM: server IP - DNS: server.dns - DC: server_datacenter" DATABASES="GeoLiteCity GeoLiteCityv6-beta/GeoLiteCityv6 GeoLiteCountry/GeoIP GeoIPv6 asnum/GeoIPASNum asnum/GeoIPASNumv6" EMAIL_BUGGY="" 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}/${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 mv "${GEOIPDIR}/GeoLiteCity.dat" "${GEOIPDIR}/GeoIPCity.dat" mv "${GEOIPDIR}/GeoLiteCityv6.dat" "${GEOIPDIR}/GeoIPCityv6.dat" fi
Make sure you change the EMAIL_TO and EMAIL_META variables
All you need to do is open your terminal, and type
$ cd /root $ nano geoip.sh
Paste the abode code and save it. Then, make it readable/writable/executable by root user only:
$ chmod 700 geoip.sh
Now, you should be able to launch the GeoIP database update process:
$ ./geoip.sh
If it is all right and you didn’t received any error insert it on the crontab so it happen automatically once a month.
$ crontab -e
Paste the following line at the very end of the file:
# Automatic GeoIP database update 30 3 1 * * /root/geoip.sh >>/dev/null
This will run this code every first day of the month at 3:30 AM
Save the file and exit your text editor. You should now see a message telling you the new crontab has been installed.
You are now done! Your GeoIP database will now stay fresh & reliable, and your happy server will do all the job for you!
Categories
Archives
- July 2017 (1)
- May 2015 (1)
- November 2013 (2)
- August 2013 (1)
- September 2012 (3)
- June 2012 (1)
- May 2012 (4)
Tags Cloud
Android APC APC Cache bash Blackberry CentOS clamscan co cpanel databases HTML 5 install memcache iPhone linux load balanced servers memcache Memcached memcahed sessions MySQL PHP php sessions Server Cache shell ssh sticky notes available text formats sticky notes commands Sticky Notes format viruses webOS website antivirus website infections