Skip to content

Instantly share code, notes, and snippets.

@dcm
Created September 19, 2021 08:59
Show Gist options
  • Save dcm/c4c64300b16583a04033e8aa2297054b to your computer and use it in GitHub Desktop.
Save dcm/c4c64300b16583a04033e8aa2297054b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
YR=$(date +%Y)
MTH=$(date +%m)
DSTDIR="${1:-${HOME}/.geoip}"
OLDDIR="${2:-${DSTDIR}/.old}"
BURL="https://download.db-ip.com/free"
mkdir -p "${DSTDIR}" "${OLDDIR}"
cd "${DSTDIR}"
for TP in country city asn; do
_YR=$YR
_MTH=$MTH
while [ 1 ]; do
URL="${URL:-${BURL}/dbip-${TP}-lite-${_YR}-${_MTH}.mmdb.gz}"
ZF="$(basename "${URL}")"
F="${ZF/%.gz/}"
if [ -f "${F}" ]; then
echo "Already have '${F}'"
break
fi
LP=0
set +e
wget -qO "${ZF}" "${URL}"
if [ $? -ne 0 ]; then
echo -n "No '${TP}' database file found for ${_MTH}/${_YR}"
if [[ "$_MTH" == "01" ]]; then
_YR=$(( _YR - 1 ))
_MTH=12
else
_MTH=$(( _MTH - 1 ))
fi
if [ ${#_MTH} -lt 2 ]; then
_MTH="0$_MTH"
fi
echo ", retrying for ${_MTH}/${_YR}..."
LP=1
fi
set -e
if [ $LP -eq 1 ]; then continue; fi
gunzip "${ZF}"
ls -l "${F}"
break
done
unset URL
mv "${F}" ".${F}"
if ls -1 . | grep -qE "dbip-${TP}-lite-.+-.+.mmdb" &>/dev/null; then
for _OF in ./dbip-${TP}-lite-*-*.mmdb; do
echo "Archiving '${_OF}' to '${OLDDIR}'"
mv -f "${_OF}" "${OLDDIR}/"
done
fi
mv ".${F}" "${F}"
done
echo "Done."
exit 0
@dcm
Copy link
Author

dcm commented Sep 19, 2021

This downloads the latest DB-IP "lite" databases to ~/.geoip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment