-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_database.sh
More file actions
executable file
·108 lines (94 loc) · 2.75 KB
/
setup_database.sh
File metadata and controls
executable file
·108 lines (94 loc) · 2.75 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
print_msg() {
local color="$1"
local msg="$2"
case "$color" in
"red")
echo -e "\033[31m$msg\033[0m"
;;
"green")
echo -e "\033[32m$msg\033[0m"
;;
"yellow")
echo -e "\033[33m$msg\033[0m"
;;
"blue")
echo -e "\033[34m$msg\033[0m"
;;
*)
echo "$msg"
;;
esac
}
if [[ "$EUID" -ne 0 ]]; then
print_msg "red" "Please run as root using sudo"
exit 1
fi
usage() {
echo "Usage: $0 -a <api_key> -d <database_name> -t <data_directory> -e <extraction_directory>"
exit 1
}
while getopts "a:d:t:e:" opt; do
case $opt in
a) api_key="$OPTARG" ;;
d) database_name="$OPTARG" ;;
t) data_directory="$OPTARG" ;;
e) extraction_directory="$OPTARG" ;;
*) usage ;;
esac
done
if [[ -z "$api_key" || -z "$database_name" || -z "$data_directory" || -z "$extraction_directory" ]]; then
usage
fi
if [[ -d "$extraction_directory" ]]; then
print_msg "yellow" "Extraction directory exists, removing..."
sudo rm -rf "$extraction_directory"
fi
print_msg "blue" "Creating extraction directory..."
sudo mkdir -p "$extraction_directory"
cd "$extraction_directory"
database_url="https://database.ipgeolocation.io/download/${database_name}?apiKey=${api_key}"
zip_path="${extraction_directory}/db.zip"
print_msg "blue" "Downloading database..."
if sudo curl -o "$zip_path" "$database_url"; then
print_msg "green" "Database downloaded successfully."
else
print_msg "red" "Failed to download database."
exit 1
fi
print_msg "blue" "Unzipping database..."
if sudo unzip -o "$zip_path" -d "$extraction_directory"; then
print_msg "green" "Database unzipped successfully."
else
print_msg "red" "Failed to unzip database."
exit 1
fi
if [[ -d "$data_directory" ]]; then
print_msg "yellow" "Data directory exists, removing old MMDB files..."
sudo rm -f "$data_directory"/*.mmdb
else
print_msg "blue" "Creating data directory..."
sudo mkdir -p "$data_directory"
fi
print_msg "blue" "Moving MMDB files to data directory..."
if sudo mv "$extraction_directory"/*.mmdb "$data_directory"; then
print_msg "green" "MMDB files moved successfully."
else
print_msg "red" "Failed to move MMDB files."
exit 1
fi
print_msg "blue" "Moving cloud provider files to data directory..."
if sudo mv "$extraction_directory"/db-cloud-provider.csv.gz "$data_directory"; then
print_msg "green" "MMDB files moved successfully."
else
print_msg "red" "Failed to move MMDB files."
exit 1
fi
print_msg "blue" "Removing extraction directory..."
if sudo rm -rf "$extraction_directory"; then
print_msg "green" "Extraction directory removed successfully."
else
print_msg "red" "Failed to remove extraction directory."
exit 1
fi
print_msg "green" "Database setup completed successfully."