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

41 lines
1.2 KiB
Bash

#!/bin/bash
# Define constants
BASE_URL="https://download.maxmind.com/geoip/databases"
AUTH="YOUR_ACCOUNT_ID:YOUR_LICENSE_KEY"
OUTPUT_DIR="$(dirname "$0")/geolite2/db"
CITY_FILE="GeoLite2-City"
ASN_FILE="GeoLite2-ASN"
COUNTRY_FILE="GeoLite2-Country"
# Create directory structure if it doesn't exist
mkdir -p "$OUTPUT_DIR/city"
mkdir -p "$OUTPUT_DIR/asn"
mkdir -p "$OUTPUT_DIR/country"
# Download, extract, and move the files
process_file() {
local file_name=$1
local target_dir=$2
echo "Processing $file_name..."
# 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"
# 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"
# Clean up the downloaded tar.gz file
rm -f "$OUTPUT_DIR/$file_name.tar.gz"
echo "$file_name processed and placed in $target_dir"
}
# 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."