sojorn/go-backend/scripts/setup-geoip.sh

52 lines
1.7 KiB
Bash

#!/bin/bash
# Setup script for GeoIP database
# This downloads the GeoLite2-Country database from MaxMind
set -e
GEOIP_DIR="/opt/sojorn/geoip"
DATABASE_FILE="$GEOIP_DIR/GeoLite2-Country.mmdb"
echo "Setting up GeoIP database for geographic filtering..."
# Create directory if it doesn't exist
sudo mkdir -p "$GEOIP_DIR"
sudo chown patrick:patrick "$GEOIP_DIR"
# Download the GeoLite2-Country database
echo "Downloading GeoLite2-Country database..."
cd "$GEOIP_DIR"
# Download the free GeoLite2-Country database
echo "Downloading from MaxMind..."
wget -O GeoLite2-Country.tar.gz "https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-Country.mmdb.gz" 2>/dev/null || {
echo "Primary download failed, trying alternative source..."
# Alternative source - GitHub mirror
wget -O GeoLite2-Country.mmdb.gz "https://raw.githubusercontent.com/P3TERX/GeoLite.mmdb/download/GeoLite2-Country.mmdb.gz" || {
echo "All downloads failed. Please manually download GeoLite2-Country.mmdb"
exit 1
}
}
# Extract if we got a gz file
if [ -f "GeoLite2-Country.mmdb.gz" ]; then
echo "Extracting database..."
gunzip GeoLite2-Country.mmdb.gz
echo "Database extracted successfully!"
elif [ -f "GeoLite2-Country.tar.gz" ]; then
echo "Extracting tar.gz file..."
tar xzf GeoLite2-Country.tar.gz --strip-components=1
rm GeoLite2-Country.tar.gz
echo "Database extracted successfully!"
fi
# Verify the database file exists
if [ -f "$DATABASE_FILE" ]; then
echo "✓ GeoIP database installed successfully at: $DATABASE_FILE"
echo "File size: $(du -h "$DATABASE_FILE" | cut -f1)"
else
echo "✗ Failed to install GeoIP database"
exit 1
fi