Skip to content

Instantly share code, notes, and snippets.

@poqdavid
Last active July 22, 2021 11:38
Show Gist options
  • Save poqdavid/7885188f76d57ad313ba2f89cce5335d to your computer and use it in GitHub Desktop.
Save poqdavid/7885188f76d57ad313ba2f89cce5335d to your computer and use it in GitHub Desktop.
A simple bash script to update database from db-ip.com
#!/bin/bash
DATAFILE=/home/ubuntu/scripts/dbipdata/lastupdate
DATABASEDIR=/home/ubuntu/geoip
DATABASENAME=citylite.mmdb
DATABASEFULLPATH="/home/ubuntu/geoip/citylite.mmdb"
CURRENTDATE=`date +%Y-%m`
LASTUPDATE=``
GZIPFILE="/home/ubuntu/geoip/dbip-city-lite-${CURRENTDATE}.mmdb.gz"
UNZIPEDFILE="/home/ubuntu/geoip/dbip-city-lite-${CURRENTDATE}.mmdb"
if [ -f "$DATAFILE" ];
then
echo $(date '+%H:%M:%S')": Info: File $DATAFILE exists!"
else
echo "0000-00" > ${DATAFILE}
fi;
read -r LASTUPDATE < ${DATAFILE}
CURRENTURL="https://download.db-ip.com/free/dbip-city-lite-${CURRENTDATE}.mmdb.gz"
echo $(date '+%H:%M:%S')": >${CURRENTDATE}< >${LASTUPDATE}<"
echo $(date '+%H:%M:%S')": Current URL ${CURRENTURL}"
if [ "${LASTUPDATE}" = "${CURRENTDATE}" ]; then
echo $(date '+%H:%M:%S')": DATABASE is Update..."
else
echo $(date '+%H:%M:%S')": DATABASE isn't Update..."
echo $(date '+%H:%M:%S')": Checking Update Source..."
status="$(curl -Is ${CURRENTURL} | head -1)"
echo $(date '+%H:%M:%S')": Status: ${status}"
if [[ "$status" == *"200"* ]]; then
echo $(date '+%H:%M:%S')": Downloading DATABASE Update..."
wget -P ${DATABASEDIR} ${CURRENTURL}
echo $(date '+%H:%M:%S')": Downloaded DATABASE Update."
if [ -f "$GZIPFILE" ];
then
echo $(date '+%H:%M:%S')": Info: File $GZIPFILE exists!"
echo $(date '+%H:%M:%S')": Decompressing ${GZIPFILE}..."
gzip -d ${GZIPFILE}
echo $(date '+%H:%M:%S')": Decompresed ${GZIPFILE}."
if [ -f "$UNZIPEDFILE" ];
then
echo $(date '+%H:%M:%S')": Info: File $UNZIPEDFILE exists!"
echo $(date '+%H:%M:%S')": Replacing Update with current DATABASE..."
mv -vf "${UNZIPEDFILE}" "${DATABASEFULLPATH}"
chown ubuntu:ubuntu ${DATABASEFULLPATH}
echo $(date '+%H:%M:%S')": Replaced Update with current DATABASE..."
echo "$CURRENTDATE" > ${DATAFILE}
echo $(date '+%H:%M:%S')": Rebooting server..."
reboot
else
echo $(date '+%H:%M:%S')": ${UNZIPEDFILE} doesn't exists!"
fi;
else
echo $(date '+%H:%M:%S')": ${GZIPFILE} doesn't exists!"
fi;
else
echo $(date '+%H:%M:%S')": UPDATE NOT AVILABLE..."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment