1#!/usr/local/bin/bash
2#
3# Simple shell script to update db-ip.com database files
4#
5# Project    : ipv6calc/DBIP
6# File       : DBIP-update.sh
7# Version    : $Id: 51cd31806067116931ddbe33fccf69e4985de1bf $
8# Copyright  : 2014-2021 by Peter Bieringer <pb (at) bieringer.de>
9# License    : GNU GPL version 2
10
11DBIP_DAT_DIR_DEFAULT="@DBIP2_DB@"
12[ -z "$DBIP_DAT_DIR" -a -n "$IPV6CALC_DB_DBIP2_DIR" ] && DBIP_DAT_DIR="$IPV6CALC_DB_DBIP2_DIR"
13[ -z "$DBIP_DAT_DIR" ] && DBIP_DAT_DIR="$DBIP_DAT_DIR_DEFAULT"
14
15DBIP_DAT_URL_BASE="http://download.db-ip.com/free/"
16DBIP_DAT_FILES="dbip-country-lite-%Y-%m.mmdb.gz dbip-city-lite-%Y-%m.mmdb.gz"
17
18
19help() {
20	cat <<END
21Usage: $(basename "$0") [-v] [-s] [-D <dir>] [-U <url>] [-T <type>]
22	-s		skip download
23	-v		verbose
24	-D <dir>	database directory (optional)
25	-U <url>	use URL to download full database
26	-T <type>	set type of via URL downloaded file
27                          MMDB: country|isp|location|*location-isp*
28			  CSV : isp|location|*full*
29
30	database directory: $DBIP_DAT_DIR (default: $DBIP_DAT_DIR_DEFAULT)
31
32 it honors externally defined environment value:
33	prio 1: DBIP_DAT_DIR
34	prio 2: IPV6CALC_DB_DBIP2_DIR
35
36 this script will download data from
37 DB-IP.com (default: free versions) the MaxMindDB (mmdb) files DB files for ipv6calc
38 "DBIP" database support
39
40 DBIP_DAT_URL_BASE=$DBIP_DAT_URL_BASE
41 DBIP_DAT_FILES=$DBIP_DAT_FILES
42
43 in case -U <url> (take URL from subscription e-mail) is provided, it downloads the database
44  use -T <type> in case it's not "location-isp"
45END
46}
47
48skip_download=false
49verbose=false
50while getopts "vT:D:U:sh\?" opt; do
51	case $opt in
52	    v)
53		verbose=true
54		;;
55	    s)
56		skip_download=true
57		;;
58	    D)
59		DBIP_DAT_DIR="$OPTARG"
60		;;
61	    U)
62		DBIP_URL="$OPTARG"
63		;;
64	    T)
65		DBIP_TYPE="$OPTARG"
66		;;
67	    *)
68		help
69		exit 1
70		;;
71	esac
72done
73
74if [ -n "$DBIP_URL" ]; then
75	case $DBIP_URL in
76	    *.csv)
77		urldbtype=${DBIP_TYPE:-full}
78		DBIP_DAT_FILES="dbip-$urldbtype-%Y-%m.csv.gz"
79		;;
80	    *.mmdb)
81		urldbtype=${DBIP_TYPE:-location-isp}
82		DBIP_DAT_FILES="dbip-$urldbtype-%Y-%m.mmdb.gz"
83		;;
84	    *)
85		echo "ERROR : given URL has unsupported filename suffix"
86		exit 1
87		;;
88	esac
89fi
90
91if [ ! -t 0 ] || $verbose; then
92	options_generate="-q"
93	options_wget="-q"
94fi
95
96if [ ! -d "$DBIP_DAT_DIR" ]; then
97	echo "ERROR : missing directory: $DBIP_DAT_DIR"
98	exit 1
99fi
100
101if [ ! -w "$DBIP_DAT_DIR" ]; then
102	echo "ERROR : missing write permissions on directory: $DBIP_DAT_DIR"
103	exit 1
104fi
105
106# Download files
107download_result=true
108if ! $skip_download; then
109	for file in $DBIP_DAT_FILES; do
110		# convert tokens
111		year=$(date +%Y)
112		month=$(date +%m)
113
114		file=${file//%Y/$year}
115		file=${file//%m/$month}
116
117		file_dest="$DBIP_DAT_DIR/`basename "$file"`"
118
119		if [ -z "$DBIP_URL" ]; then
120			echo "INFO  : try to download file: $file ($file_dest)"
121			wget $options_wget -O "$file_dest" "$DBIP_DAT_URL_BASE$file"
122			if [ $? -ne 0 ]; then
123				echo "ERROR : download of file not successful: $file ($file_dest)"
124				download_result=false
125				continue
126			fi
127		else
128			echo "INFO  : try to download file: $DBIP_URL ($file_dest)"
129			wget $options_wget -O "$file_dest" "$DBIP_URL"
130			if [ $? -ne 0 ]; then
131				echo "ERROR : download of file not successful: $DBIP_URL ($file_dest)"
132				download_result=false
133				continue
134			fi
135		fi
136		echo "INFO  : download of file successful: $DBIP_URL ($file_dest)"
137	done
138fi
139
140# create db/decompress mmdb files from downloaded files
141error=0
142if $download_result; then
143	for file in $DBIP_DAT_FILES; do
144		# convert tokens
145		year=$(date +%Y)
146		month=$(date +%m)
147
148		file_input=${file//%Y/$year}
149		file_input=${file_input//%m/$month}
150
151		file_input="$DBIP_DAT_DIR/`basename "$file_input"`"
152
153		case $file in
154		    *.mmdb.gz)
155			if [ -e "$file_input" ]; then
156				echo "INFO  : decompress: $file_input)"
157				gunzip "$file_input"
158				if [ $? -ne 0 ] ;then
159					echo "ERROR : decompress failed: $file_input"
160					error=1
161					continue
162				fi
163			fi
164
165			file_input=${file_input/.gz}
166			if [ ! -e "$file_input" ]; then
167				echo "ERROR : decompressed file missing: $file_input)"
168				error=1
169				continue
170			fi
171
172			file_softlink="${file/-%Y}"
173			file_softlink="${file_softlink/-%m}"
174			file_softlink="${file_softlink/.gz}"
175			file_softlink="$DBIP_DAT_DIR/`basename "${file_softlink}"`"
176			echo "NOTICE: mmdb file requires only softlink: $file_softlink -> `basename "$file_input"`"
177			if [ -e "$file_softlink" ]; then
178				if [ ! -L "$file_softlink" ]; then
179					echo "ERROR : file exists but is not a softlink: $file_softlink"
180					continue
181				fi
182				echo "NOTICE: remove existing softlink: $file_softlink"
183				rm -f "$file_softlink"
184			fi
185			ln -sf "`basename "$file_input"`" "$file_softlink"
186			;;
187		    *)
188			echo "WARN  : unsupported file: $file (SKIP)"
189			;;
190		esac
191	done
192fi
193exit $error
194