Skip to content

Instantly share code, notes, and snippets.

@teckl
Created May 12, 2024 08:17
Show Gist options
  • Save teckl/ae8725a7aab5e5fccb488defe552ff84 to your computer and use it in GitHub Desktop.
Save teckl/ae8725a7aab5e5fccb488defe552ff84 to your computer and use it in GitHub Desktop.
Script to periodically download and update maxmind geolite2 files
#!/bin/bash
DOWNLOAD_DIR="/tmp"
EXTRACT_DIR="/var/xxxx/yyyy"
ACCOUNT_ID=NNNNNNN
LICENSE_KEY=xxxxxx_yyyyyyyyyyyyyyyyyyyyyyyyyyyyy_zzz
TEMP_FILE="tmp_downloaded_geolite2.tar.gz"
if ! curl -o "${DOWNLOAD_DIR}/${TEMP_FILE}" -J -L -u $ACCOUNT_ID:$LICENSE_KEY 'https://download.maxmind.com/geoip/databases/GeoLite2-Country/download?suffix=tar.gz'; then
echo "Error: File download failed."
exit 1
fi
MMDB_PATH=$(tar -tzf "${DOWNLOAD_DIR}/${TEMP_FILE}" | grep --color=never GeoLite2-Country.mmdb)
if ! tar xzf "${DOWNLOAD_DIR}/${TEMP_FILE}" -C "$EXTRACT_DIR" "$MMDB_PATH" --strip-components 1; then
echo "Error: Failed to extract GeoLite2-Country.mmdb. "
exit 1
fi
echo "GeoLite2-Country.mmdb has been extracted to $EXTRACT_DIR/GeoLite2-Country.mmdb"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment