Project

General

Profile

GeoIP 업데이트

Added by 종현 이 almost 4 years ago

#!/bin/bash

REPLACEDATA="30"
CITYDATA="N"

lockfile=/var/lock/$(basename $0)
if [ -f $lockfile ];then
P=$(cat $lockfile)
if [ -n "$(ps --no-headers -f $P)" ];then
exit 1
fi
fi
echo $$ > $lockfile
trap 'rm -f "$lockfile"' EXIT

GEOIPDIR="/usr/share/GeoIP"
DATALINK="/usr/share/xt_geoip /var/lib/GeoIP"
if ! -d $GEOIPDIR ;then
mkdir -p $GEOIPDIR
fi
for a in $DATALINK
do
if ! -d $a ;then if $(readlink $a) != $GEOIPDIR ;then
rm -rf $a;ln -s $GEOIPDIR $a
fi;fi
done

cd $GEOIPDIR
if [ ! -e $GEOIPDIR/geolite2legacy/geolite2legacy.py ];then cd $GEOIPDIR
git clone https://github.com/sherpya/geolite2legacy.git
fi

if [ -d $GEOIPDIR/geolite2legacy ];then cd $GEOIPDIR/geolite2legacy
COUNTRY="GeoLite2-Country-CSV.zip"
CITY="GeoLite2-City-CSV.zip"
if -z $(find $GEOIPDIR/geolite2legacy -maxdepth 1 \( -name $COUNTRY -o -name $CITY \) -mtime +$REPLACEDATA) ;then
rm -f $COUNTRY $CITY
wget "https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country-CSV.zip" > /dev/null 2>&1
if $CITYDATA "Y" ;then
wget "https://geolite.maxmind.com/download/geoip/database/GeoLite2-City-CSV.zip" > /dev/null 2>&1
fi
fi
if [ -e $GEOIPDIR/geolite2legacy/$COUNTRY ];then
python geolite2legacy.py --input-file $COUNTRY --fips-file geoname2fips.csv --output-file GeoIP.dat
python geolite2legacy.py --input-file $COUNTRY -6 --fips-file geoname2fips.csv --output-file GeoIPv6.dat
if $CITYDATA "Y" ;then
python geolite2legacy.py --input-file $CITY --fips-file geoname2fips.csv --output-file GeoLiteCity.dat
python geolite2legacy.py --input-file $CITY -6 --fips-file geoname2fips.csv --output-file GeoLiteCityv6.dat
fi
fi
if [ -e GeoIP.dat ];then
find $GEOIPDIR -maxdepth 1 -name "*.dat" -mtime +$REPLACEDATA -exec rm -f {} \;
if $CITYDATA == "Y" ;then
mv -f Geo{IP,IPv6,LiteCity,LiteCityv6}.dat $GEOIPDIR/ > /dev/null 2>&1
else
mv -f Geo{IP,IPv6}.dat $GEOIPDIR/ > /dev/null 2>&1
fi;fi;fi

unset GEOIPDIR DATALINK CITY COUNTRY CITYDATA REPLACEDATA
exit 0