File: GeoIP-update.sh.in

package info (click to toggle)
ipv6calc 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,400 kB
  • sloc: ansic: 73,764; sh: 5,750; perl: 4,030; xml: 1,194; makefile: 888
file content (95 lines) | stat: -rwxr-xr-x 2,448 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
#
# Simple shell script to update GeoIP database files
#
# Project    : ipv6calc/GeoIP
# File       : GeoIP-update.sh
# Version    : $Id: 06c282f199bee054aa8b8522d74a59f72e8c14a7 $
# Copyright  : 2012-2013 by Peter Bieringer <pb (at) bieringer.de>
# License    : GNU GPL version 2

GEOIP_DAT_DIR_DEFAULT="@GEOIP_DB@"
[ -z "$GEOIP_DAT_DIR" ] && GEOIP_DAT_DIR="$GEOIP_DAT_DIR_DEFAULT"

GEOIP_DAT_URL_BASE="http://geolite.maxmind.com/download/geoip/database/"
GEOIP_DAT_FILES="GeoLiteCountry/GeoIP.dat.gz GeoIPv6.dat.gz GeoLiteCity.dat.gz GeoLiteCityv6-beta/GeoLiteCityv6.dat.gz asnum/GeoIPASNum.dat.gz asnum/GeoIPASNumv6.dat.gz"


help() {
	cat <<END
Usage: $(basename "$0") [-D <dir>]
	-D <dir>	database directory (optional)

	database directory: $GEOIP_DAT_DIR (default: $GEOIP_DAT_DIR_DEFAULT)

	it honors externally defined environment value: GEOIP_DAT_DIR

 this script will download data from geolite.maxmind.com (GeoIP)
 into given/set database directory

 GEOIP_DAT_URL_BASE=$GEOIP_DAT_URL_BASE
 GEOIP_DAT_FILES=$GEOIP_DAT_FILES
END
}

while getopts "D:h\?" opt; do
	case $opt in
	    D)
		GEOIP_DAT_DIR=$OPTARG
		;;
	    *)
		help
		exit 1
		;;
	esac
done


if [ ! -d "$GEOIP_DAT_DIR" ]; then
	echo "ERROR : missing directory: $GEOIP_DAT_DIR"
	exit 1
fi

if [ ! -w "$GEOIP_DAT_DIR" ]; then
	echo "ERROR : missing write permissions on directory: $GEOIP_DAT_DIR"
	exit 1
fi

# Download and unpack files
for file in $GEOIP_DAT_FILES; do
	file_basename="`basename "$file"`"
	file_basename_decomp="`basename "$file" .gz`"
	file_dest="$GEOIP_DAT_DIR/$file_basename"

	echo "INFO  : try to download file: $file ($file_dest)"
	wget -q -O "$file_dest" "$GEOIP_DAT_URL_BASE$file"
	if [ $? -ne 0 ]; then
		echo "ERROR : download of file not successful: $file ($file_dest)"
		continue
	fi
	echo "INFO  : download of file successful: $file ($file_dest)"

	gunzip -f "$file_dest"
	if [ $? -ne 0 ]; then
		echo "ERROR : unzip of file not successful: $file_dest"
		continue
	fi
	echo "INFO  : unzip of file successful: $file_dest"

	# check for softlinks
	case "$file_basename_decomp" in
	    GeoLiteCity.dat)
		softlinkdst="GeoIPCity.dat" 
		;;
	    GeoLiteCityv6.dat)
		softlinkdst="GeoIPCityv6.dat" 
		;;
	    *)
		softlinkdst="" 
        esac

	if [ -n "$softlinkdst" -a ! -e "$GEOIP_DAT_DIR/$softlinkdst" ]; then
		echo "NOTICE: softlink missing, create: $softlinkdst"
		ln -s "$file_basename_decomp" "$GEOIP_DAT_DIR/$softlinkdst"
	fi
done