Skip to content

Example: Automatically updating Maxmind GeoIP database

Alexey Zilber edited this page Jan 24, 2020 · 1 revision

Example

We have a Maxmind Region, City database that we want to load into Redis every month.

  1. Create a geoip2redis directory and redis directory:
mkdir -p ~/geoip/work/maxmind

Remember to make a user specific for this process, that has access to redis.

Example download and load bash script:

#!/bin/bash

cd ~/geoip

if [ -e GeoIP2-City-CSV-MM3.zip ]; then
  if [ -e GeoIP2-City-CSV-MM3.zip.old ]; then
    rm -f GeoIP2-City-CSV-MM3.zip.old
  fi
mv GeoIP2-City-CSV-MM3.zip GeoIP2-City-CSV-MM3.zip.old
fi


wget -O GeoIP2-City-CSV-MM3.zip 'https://download.maxmind.com/app/geoip_download?edition_id=GeoIP2-City-CSV&license_key=<insert Maxmind License Key>&suffix=zip'


unzip -o -j -d work/maxmind GeoIP2-City-CSV-MM3.zip */GeoIP2-City-Blocks-IPv4.csv */GeoIP2-City-Locations-en.csv

./maxmind-ip2location work/maxmind/GeoIP2-City-Blocks-IPv4.csv work/maxmind/GeoIP2-City-Locations-en.csv work/maxmind/mm-merged.csv

./geoip2redis -f ip2location -i 3 -d MM3 -r 127.0.0.1 -p 6379 work/maxmind/mm-merged.csv

rm -f work/maxmind/mm-merged.csv

touch .lastran-mm

echo "Redis updated with Maxmind data." |mail -s "Updated GEOIP MM3 data" nobody@noreply.com

Add a cron job to run it every month.

Clone this wiki locally