Skip to content
Snippets Groups Projects
Commit b21c421a authored by Jan Mach's avatar Jan Mach
Browse files

Improved utility script fetch-geoipdb.sh for fetching geolocation databases.

Added comments, added ability to receive and process command line arguments for better usability, few additional minor tweaks. (Redmine issue: #3443)
parent 3c59f37e
No related branches found
No related tags found
No related merge requests found
...@@ -8,5 +8,4 @@ ...@@ -8,5 +8,4 @@
# #
# Run every Wednesday at 6am # 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
#!/bin/bash #!/bin/bash
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Utility script for fetching geolocation databases.
#
# Copyright (C) since 2011 CESNET, z.s.p.o # Copyright (C) since 2011 CESNET, z.s.p.o
# Use of this source is governed by the MIT license, see LICENSE file. # 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 mkdir -p $DBDIR
#
# Limit downloads to once in 12 hours. # 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 then
echo "All database files are recent enough" success "All database files in '$DBDIR' are recent enough at `date` (last update at `date -r $DBDIR/$STAMPF`)"
exit 0
fi fi
#
# Fetch latest versions of GeoLite2 database files. # 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-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-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" /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. # 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" /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. # Remove obsolete versions of database files.
#
/bin/rm -f "$DBDIR/GeoLite2-ASN.mmdb" /bin/rm -f "$DBDIR/GeoLite2-ASN.mmdb"
/bin/rm -f "$DBDIR/GeoLite2-City.mmdb" /bin/rm -f "$DBDIR/GeoLite2-City.mmdb"
/bin/rm -f "$DBDIR/GeoLite2-Country.mmdb" /bin/rm -f "$DBDIR/GeoLite2-Country.mmdb"
cd $DBDIR cd $DBDIR
#
# Extract, install and cleanup. # Extract, install and cleanup.
#
/bin/tar -xzvf GeoLite2-ASN*.tar.gz /bin/tar -xzvf GeoLite2-ASN*.tar.gz
/bin/tar -xzvf GeoLite2-City*.tar.gz /bin/tar -xzvf GeoLite2-City*.tar.gz
/bin/tar -xzvf GeoLite2-Country*.tar.gz /bin/tar -xzvf GeoLite2-Country*.tar.gz
...@@ -59,6 +144,9 @@ cd $DBDIR ...@@ -59,6 +144,9 @@ cd $DBDIR
/bin/rm -f GeoLite2-City.tar.gz /bin/rm -f GeoLite2-City.tar.gz
/bin/rm -f GeoLite2-Country.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`"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment