echoip-server-setup/geolite2-db-update.sh

60 lines
1.8 KiB
Bash
Raw Permalink Normal View History

2025-01-19 11:25:00 +00:00
#!/bin/bash
# Define a default path for the main data folder
DEFAULT_MAIN_DATA_DIR="/path/to/default/main/data/folder"
# Define your MaxMind account access for GeoLite2 databases
AUTH="YOUR_ACCOUNT_ID:YOUR_LICENSE_KEY"
# Script
# Use the provided argument or fall back to the default
MAIN_DATA_DIR="${1:-$DEFAULT_MAIN_DATA_DIR}"
# Check if the specified main data folder exists
if [ ! -d "$MAIN_DATA_DIR" ]; then
echo "Error: The specified main data folder does not exist: $MAIN_DATA_DIR"
exit 1
fi
2025-01-19 11:25:00 +00:00
# Define constants
BASE_URL="https://download.maxmind.com/geoip/databases"
OUTPUT_DIR="$MAIN_DATA_DIR/geolite2/db" # Geolite folder
LOG_DIR="$MAIN_DATA_DIR/log" # Log folder
LOG_FILE="$LOG_DIR/download.log" # Log file path
2025-01-19 11:25:00 +00:00
CITY_FILE="GeoLite2-City"
ASN_FILE="GeoLite2-ASN"
COUNTRY_FILE="GeoLite2-Country"
# Create necessary directory structures if they don't exist
2025-01-19 11:25:00 +00:00
mkdir -p "$OUTPUT_DIR/city"
mkdir -p "$OUTPUT_DIR/asn"
mkdir -p "$OUTPUT_DIR/country"
mkdir -p "$LOG_DIR"
2025-01-19 11:25:00 +00:00
# Download, extract, and move the files
process_file() {
local file_name=$1
local target_dir=$2
echo "Processing $file_name..." | tee -a "$LOG_FILE"
2025-01-19 11:25:00 +00:00
# Download the .tar.gz file to the output directory
curl -o "$OUTPUT_DIR/$file_name.tar.gz" -J -L -u "$AUTH" "$BASE_URL/$file_name/download?suffix=tar.gz" 2>> "$LOG_FILE"
2025-01-19 11:25:00 +00:00
# Extract the .tar.gz file into the target directory
tar -xzf "$OUTPUT_DIR/$file_name.tar.gz" --wildcards --strip-components=1 -C "$target_dir" "*/$file_name.mmdb" 2>> "$LOG_FILE"
2025-01-19 11:25:00 +00:00
# Clean up the downloaded tar.gz file
rm -f "$OUTPUT_DIR/$file_name.tar.gz"
2025-01-19 11:25:00 +00:00
echo "$file_name processed and placed in $target_dir" | tee -a "$LOG_FILE"
2025-01-19 11:25:00 +00:00
}
# Process each database
process_file "$CITY_FILE" "$OUTPUT_DIR/city"
process_file "$ASN_FILE" "$OUTPUT_DIR/asn"
process_file "$COUNTRY_FILE" "$OUTPUT_DIR/country"
echo "All files processed successfully." | tee -a "$LOG_FILE"