This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
mission:log:2012:10:25:geoip-database-magic-safety-update-script [2012-10-25 20:51] – created chrono | mission:log:2012:10:25:geoip-database-magic-safety-update-script [2013-06-30 12:41] (current) – [update-geoip.sh] chrono | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== GeoIP Database Magic-Safety-Updater ====== | ||
+ | |||
+ | In the past several months it sometimes just so happened, that the geoipupdate tool, available on many GNU/Linux systems to update MaxMind' | ||
+ | |||
+ | It performed very well for the last several weeks, never raised an alert and is published now, as it might be useful for someone else out there, confronted with the same problem. You could spend your time elsewhere, instead of re-inventing something that is already here and which doesn' | ||
+ | |||
+ | ===== ===== | ||
+ | |||
+ | ===== update-geoip.sh ===== | ||
+ | |||
+ | **Dependencies** | ||
+ | |||
+ | - BASH | ||
+ | - logger | ||
+ | - geoip-bin/ | ||
+ | |||
+ | <WRAP round tip> | ||
+ | With the update-$whatever naming convention, which can be found on most distributions today, naming it // | ||
+ | </ | ||
+ | |||
+ | <sxh bash> | ||
+ | #!/bin/bash | ||
+ | # | ||
+ | ########################################################################## | ||
+ | # | ||
+ | # This program is free software: you can redistribute it and/or modify | ||
+ | # it under the terms of the GNU General Public License as published by | ||
+ | # the Free Software Foundation, either version 3 of the License, or | ||
+ | # (at your option) any later version. | ||
+ | # | ||
+ | # This program is distributed in the hope that it will be useful, | ||
+ | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
+ | # | ||
+ | # GNU General Public License for more details. | ||
+ | # | ||
+ | # You should have received a copy of the GNU General Public License | ||
+ | # along with this program. If not, see < | ||
+ | # | ||
+ | ########################################################################## | ||
+ | |||
+ | ########################################################################## | ||
+ | # GeoIP Database Magic-Safety Updater | ||
+ | ########################################################################## | ||
+ | # | ||
+ | # The purpose of this script is to keep GeoIP Databases automagically | ||
+ | # | ||
+ | # | ||
+ | # will roll back to a last known good version in case the new DBs | ||
+ | # fail to geolocate a predefined Test-IP/ | ||
+ | # | ||
+ | ########################################################################## | ||
+ | # | ||
+ | ########################################################################## | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | |||
+ | G_HOME="/ | ||
+ | G_BIN="/ | ||
+ | G_UPD="/ | ||
+ | G_LOG="/ | ||
+ | G_IP=" | ||
+ | G_CHK=" | ||
+ | DEBUG=0 | ||
+ | |||
+ | ### functions ############################################################ | ||
+ | |||
+ | function geo_check() | ||
+ | { | ||
+ | local RES=$(${G_BIN} ${G_IP}|sed -n 2p|awk ' | ||
+ | echo ${RES} | ||
+ | } | ||
+ | |||
+ | function log() | ||
+ | { | ||
+ | if [ ${DEBUG} == 1 ]; then | ||
+ | echo " | ||
+ | else | ||
+ | ${G_LOG} " | ||
+ | fi | ||
+ | } | ||
+ | |||
+ | ### prep rollback safety ################################################# | ||
+ | |||
+ | if [ ! -d " | ||
+ | mkdir -p ${G_HOME}/ | ||
+ | fi | ||
+ | |||
+ | cd ${G_HOME} | ||
+ | cp ${G_HOME}/ | ||
+ | |||
+ | ### try update ########################################################### | ||
+ | |||
+ | if [ " | ||
+ | DEBUG=1 | ||
+ | ${G_UPD} | ||
+ | else | ||
+ | ${G_UPD} > /dev/null 2>&1 | ||
+ | fi | ||
+ | |||
+ | ### check update ######################################################### | ||
+ | |||
+ | CHECK=$(geo_check) | ||
+ | |||
+ | if [ " | ||
+ | |||
+ | ### check successfull - move last known working .dat to archive ######## | ||
+ | |||
+ | log "GeoIP DB is valid - Moving last version into archive" | ||
+ | mv ${G_HOME}/ | ||
+ | |||
+ | else | ||
+ | |||
+ | ### check unsuccessfull - rolling back ################################# | ||
+ | |||
+ | log "GeoIP DB check FAILED - Rolling back last known working" | ||
+ | mv ${G_HOME}/ | ||
+ | |||
+ | ### check rollback ##################################################### | ||
+ | |||
+ | CHECK=$(geo_check) | ||
+ | |||
+ | if [ " | ||
+ | |||
+ | ### rollback successfull ############################################# | ||
+ | |||
+ | log "GeoIP DB Rollback is valid" | ||
+ | |||
+ | else | ||
+ | |||
+ | ### rollback data is corrupt too - move archived version ############# | ||
+ | |||
+ | log "GeoIP DB Rollback check FAILED - Moving back archive" | ||
+ | mv ${G_HOME}/ | ||
+ | |||
+ | ### check archive #################################################### | ||
+ | |||
+ | CHECK=$(geo_check) | ||
+ | |||
+ | if [ " | ||
+ | |||
+ | ### Archive recovery successfull ################################### | ||
+ | |||
+ | log " | ||
+ | |||
+ | else | ||
+ | |||
+ | ### ALERT - we have no GeoIP capability on this machine ############ | ||
+ | |||
+ | log "Holy DevOp... I have failed you and had to kill myself!" | ||
+ | |||
+ | # Do some alerting here if you really depend on working geoip-lookup | ||
+ | |||
+ | exit 1 | ||
+ | |||
+ | fi | ||
+ | fi | ||
+ | fi | ||
+ | |||
+ | exit 0 | ||
+ | #EOF | ||
+ | </ | ||
+ | |||
+ | <WRAP round download> | ||
+ | **Download the script:**\\ | ||
+ | [[https:// | ||
+ | </ | ||
+ | |||
+ | {{tag> | ||
+ | |||
+ | {{keywords> | ||
+ | |||
+ | |||
+ | ~~DISCUSSION~~ | ||