diff --git a/conf/cron_fetch-geoipdb-sh b/conf/cron_fetch-geoipdb-sh index 72156a9119945b52c9d9683fb9e2ef831bc22d94..80421f0e2236184687f85641f9479b68b2b9e9e3 100644 --- a/conf/cron_fetch-geoipdb-sh +++ b/conf/cron_fetch-geoipdb-sh @@ -8,5 +8,4 @@ # # Run every Wednesday at 6am -0 6 * * 3 root /etc/mentat/scripts/fetch-geoipdb.sh - +0 6 * * 3 root /etc/mentat/scripts/fetch-geoipdb.sh --quiet diff --git a/scripts/fetch-geoipdb.sh b/scripts/fetch-geoipdb.sh index cd04212ae77196ace146692b8fe9d22e84f4e5e9..8aebfa58875c4043f6fba24ce71da824d9752549 100755 --- a/scripts/fetch-geoipdb.sh +++ b/scripts/fetch-geoipdb.sh @@ -1,48 +1,133 @@ #!/bin/bash #------------------------------------------------------------------------------- +# Utility script for fetching geolocation databases. +# # Copyright (C) since 2011 CESNET, z.s.p.o # Use of this source is governed by the MIT license, see LICENSE file. #------------------------------------------------------------------------------- -if [ "A$1A" = "AA" ] -then - DBDIR="/var/opt/opensourcedbs" - echo "Fetching OSDBs into default folder: '/var/opt/opensourcedbs'" +# +# Helper functions for displaying information of various severity to the user. +# +function error { + /bin/echo -e "${RED}[ERROR] $1${NC}" 1>&2 + exit 1 +} + +function info { + if [ $FLAG_QUIET != 'YES' ]; then + /bin/echo "$1" + fi +} + +function success { + if [ $FLAG_QUIET != 'YES' ]; then + /bin/echo -e "${GREEN}[SUCCESS] $1${NC}" + fi + exit 0 +} + +function usage { + /bin/echo "" + /bin/echo "Utility script for fetching geolocation databases." + /bin/echo "" + /bin/echo "Available options:" + /bin/echo " -h | --help display this help message and exit" + /bin/echo " -q | --quiet run in quiet mode, display only errors" + /bin/echo "" + /bin/echo " -t=dir" + /bin/echo " --target=dir name of the target directory (defaults to '/var/opt/opensourcedbs')" + /bin/echo "" +} -else +#------------------------------------------------------------------------------- - DBDIR="$1" - echo "Fetching OSDBs into folder: '$1'" -fi +# +# Color code definitions for colored terminal output. +# Source: https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux +# +RED='\033[0;31m' +GREEN='\033[0;32m' +ORANGE='\033[0;33m' +NC='\033[0m' + +FLAG_DEVEL=NO +FLAG_QUIET=NO +DBDIR="/var/opt/opensourcedbs" +STAMPF=".download.ts" + +#------------------------------------------------------------------------------- + + +# +# Command line argument parsing. +# Source: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash +# +for i in "$@" +do +case $i in + -h|--help) + usage + exit 0 + ;; + -q|--quiet) + FLAG_QUIET=YES + shift # past argument=value + ;; + -t=*|--target=*) + DBDIR="${i#*=}" + shift # past argument=value + ;; + *) + usage + error "Invalid usage, unknown command line option '$i'" + ;; +esac +done + + +#------------------------------------------------------------------------------- + + +info "Fetching database files into folder: '$DBDIR'" mkdir -p $DBDIR +# # Limit downloads to once in 12 hours. -if [ -f "$DBDIR/GeoLite2-ASN.mmdb" ] && [ "$(( $(date +'%s') - $(date -r $DBDIR/.download.ts +'%s') ))" -lt "43200" ] +# +if [ -f "$DBDIR/GeoLite2-ASN.mmdb" ] && [ "$(( $(date +'%s') - $(date -r $DBDIR/$STAMPF +'%s') ))" -lt "43200" ] then - echo "All database files are recent enough" - exit 0 + success "All database files in '$DBDIR' are recent enough at `date` (last update at `date -r $DBDIR/$STAMPF`)" fi +# # Fetch latest versions of GeoLite2 database files. +# /usr/bin/wget -q http://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN.tar.gz -O "$DBDIR/GeoLite2-ASN.tar.gz" /usr/bin/wget -q http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz -O "$DBDIR/GeoLite2-City.tar.gz" /usr/bin/wget -q http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz -O "$DBDIR/GeoLite2-Country.tar.gz" +# # Fetch latest version of ISO 3166-2 country code database. +# /usr/bin/wget -q https://raw.githubusercontent.com/datasets/country-codes/master/data/country-codes.csv -O "$DBDIR/country-codes.csv" +# # Remove obsolete versions of database files. +# /bin/rm -f "$DBDIR/GeoLite2-ASN.mmdb" /bin/rm -f "$DBDIR/GeoLite2-City.mmdb" /bin/rm -f "$DBDIR/GeoLite2-Country.mmdb" cd $DBDIR +# # Extract, install and cleanup. +# /bin/tar -xzvf GeoLite2-ASN*.tar.gz /bin/tar -xzvf GeoLite2-City*.tar.gz /bin/tar -xzvf GeoLite2-Country*.tar.gz @@ -59,6 +144,9 @@ cd $DBDIR /bin/rm -f GeoLite2-City.tar.gz /bin/rm -f GeoLite2-Country.tar.gz -/bin/chown -R root:root $DBDIR +# +# Mark the time of the download into separate timestamp file. +# +/usr/bin/touch "$DBDIR/$STAMPF" -/usr/bin/touch "$DBDIR/.download.ts" +success "All database files downloaded and installed into '$DBDIR' at `date`"