-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdate-geolite-db.sh
More file actions
66 lines (51 loc) · 1.54 KB
/
update-geolite-db.sh
File metadata and controls
66 lines (51 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
# Exit immediately if any command fails
set -e
# Set variables
TEMP_DIR="/tmp/downloads_geoip2_$(date +%s)"
DESTINATION_DIR="./data/nginx/geoip2"
# Define URLs directly in the script
URLS=(
"https://git.io/GeoLite2-ASN.mmdb"
"https://git.io/GeoLite2-City.mmdb"
"https://git.io/GeoLite2-Country.mmdb"
)
# Create temporary directory
mkdir -p "$TEMP_DIR"
echo "Created temporary directory: $TEMP_DIR"
# Function to clean up
cleanup() {
echo "Cleaning up temporary directory..."
rm -rf "$TEMP_DIR"
# If $1 is provided, it's an error and we should exit with error code
if [ -n "$1" ]; then
exit 1
else
exit 0
fi
}
# Trap for handling unexpected script termination
trap 'cleanup "Interrupted"' INT TERM
echo "Starting downloads..."
# Download all URLs
for url in "${URLS[@]}"; do
filename=$(basename "$url")
echo "Downloading $url to $TEMP_DIR/$filename"
# We don't use set -e for the wget command to handle failures ourselves
wget -q --show-progress -O "$TEMP_DIR/$filename" "$url" || {
echo "Error: Failed to download $url"
cleanup "Download failed"
}
done
echo "All downloads completed successfully."
# Check if destination directory exists, create if it doesn't
if [ ! -d "$DESTINATION_DIR" ]; then
echo "Creating destination directory: $DESTINATION_DIR"
mkdir -p "$DESTINATION_DIR"
fi
# Move files to destination
echo "Moving files to $DESTINATION_DIR"
mv "$TEMP_DIR"/* "$DESTINATION_DIR/"
# Clean up temporary directory
echo "Operation completed successfully. Files are now in $DESTINATION_DIR"
cleanup