Last active
August 24, 2019 18:19
-
-
Save juangacovas/043c81434db42366efa5dac2fa0e332b to your computer and use it in GitHub Desktop.
Automatic download of MaxMindDB GeoLite2 (for lighttpd, 2019)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Automatic MaxMindDB GeoLite2 download (city version) | |
# * that downloads ONLY when new database is available (which is cool and nice with MaxMind servers) * | |
# * by Juanga Covas 2019 | |
# logs to download.log and download-nothing.log | |
# recommended cron: 0 6 * * * cd /root/maxminddb; sh download.sh > download-last-cron.log | |
final_path="/var/cache/lighttpd" | |
file_tar_gz="GeoLite2-City.tar.gz" | |
file="GeoLite2-City.mmdb" | |
uri="https://geolite.maxmind.com/download/geoip/database/$file_tar_gz" | |
# use -z option of curl to check if the file seems newer | |
if test -e "${final_path}/${file}.tar.gz.new" | |
then zflag="-z '${final_path}/${file}.tar.gz.new'" | |
else zflag= | |
fi | |
echo Checking for maxminddb updates ... | |
#echo $zflag ... | |
_command="curl --fail --silent -o "${final_path}/${file}.tar.gz" $zflag "$uri"" | |
echo $_command | |
if [ -z zflag ] ;then | |
eval $_command | |
else | |
eval $_command && echo "Nothing to download" | |
fi | |
if [ -f ${final_path}/${file}.tar.gz ] ;then | |
echo "Extracting ..." | |
mv -f ${final_path}/${file}.tar.gz{,.new} | |
# directory name inside the tar.gz is always different so we look for wildcard dir. and only the .mmdb file | |
tar zxvf ${final_path}/${file}.tar.gz.new */$file --overwrite -C ./ --strip-components=1 | |
echo "Copying mmdb file to ${final_path}..."; | |
cp -fv ./$file ${final_path}/${file} | |
chown lighttpd:lighttpd ${final_path}/${file} | |
# checking that exists: /var/cache/lighttpd/GeoLite2-City.mmdb | |
ls -la ${final_path}/${file} | |
# restart lighttpd | |
service lighttpd restart | |
# checking the datetime of the just downloaded maxminddb: | |
echo $(date) - Downloaded new $file timestamped $(ls -ls $file | awk '{print $7,$8,$9}') >> download.log | |
else | |
echo $(date) - Nothing to download >> download-nothing.log | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment