Initial commit
This commit is contained in:
		
						commit
						f99973c247
					
				
							
								
								
									
										11
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,11 @@ | |||||||
|  | build/ | ||||||
|  | stash/ | ||||||
|  | .archive/ | ||||||
|  | .vagrant/ | ||||||
|  | .env | ||||||
|  | .keys | ||||||
|  | *_[0-9] | ||||||
|  | *_[0-9][0-9] | ||||||
|  | *_????-??-?? | ||||||
|  | *.zip | ||||||
|  | 
 | ||||||
							
								
								
									
										96
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										96
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,96 @@ | |||||||
|  | # GeoIP Database Updater Script | ||||||
|  | 
 | ||||||
|  | This script, automates the process of checking for new releases of GeoIP | ||||||
|  | databases on GitHub, downloading them if they're not already present, and | ||||||
|  | updating the local database files for use. | ||||||
|  | 
 | ||||||
|  | ## Prerequisites | ||||||
|  | 
 | ||||||
|  | Ensure `wget` and `curl` are installed on your system. | ||||||
|  | 
 | ||||||
|  | ## Setup Instructions | ||||||
|  | 
 | ||||||
|  | ### 1. Create the Destination Directory | ||||||
|  | 
 | ||||||
|  | ```bash | ||||||
|  | sudo mkdir -p /usr/local/share/GeoIP | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | ### 2. Create a Group for GeoIP Users | ||||||
|  | 
 | ||||||
|  | Create a group to manage access to the GeoIP data. | ||||||
|  | 
 | ||||||
|  | ```bash | ||||||
|  | sudo groupadd geoipusers | ||||||
|  | ``` | ||||||
|  | **Note: Log out and back in for the group change to take effect.** | ||||||
|  | 
 | ||||||
|  | ### 3. Set Permissions and Ownership | ||||||
|  | 
 | ||||||
|  | Change the group ownership of the `/usr/local/share/GeoIP` directory to | ||||||
|  | `geoipusers`, and set the appropriate permissions. | ||||||
|  | 
 | ||||||
|  | ```bash | ||||||
|  | sudo chown :geoipusers /usr/local/share/GeoIP | ||||||
|  | sudo chmod 775 /usr/local/share/GeoIP | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | ### 4. Add Your User to the GeoIP Users Group | ||||||
|  | 
 | ||||||
|  | Add your user account to the `geoipusers` group to allow script execution and | ||||||
|  | access to the GeoIP directory. | ||||||
|  | 
 | ||||||
|  | ```bash | ||||||
|  | sudo usermod -a -G geoipusers $USER | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | *Note: You may need to log out and log back in for the group changes to take effect.* | ||||||
|  | 
 | ||||||
|  | ### 5. Install the Script | ||||||
|  | 
 | ||||||
|  | Copy the script to a globally accessible location and ensure it is executable: | ||||||
|  | 
 | ||||||
|  | ```bash | ||||||
|  | sudo cp geoip_updater.sh /usr/local/bin/ | ||||||
|  | sudo chown :geoipusers /usr/local/bin/geoip_updater.sh | ||||||
|  | sudo chmod +x /usr/local/bin/geoip_updater.sh | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | ### 6. Schedule the Script in Crontab | ||||||
|  | 
 | ||||||
|  | Edit your crontab to run the script automatically: | ||||||
|  | 
 | ||||||
|  | ```bash | ||||||
|  | crontab -e | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | Add the following line to schedule the script to run daily at 2 AM: | ||||||
|  | 
 | ||||||
|  | ```bash | ||||||
|  | 0 2 * * * /usr/local/bin/geoip_updater.sh >> /var/log/geoip_update.log 2>&1 | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | ### 7. Prepare the Log File | ||||||
|  | 
 | ||||||
|  | Make sure the script can write to the log file: | ||||||
|  | 
 | ||||||
|  | ```bash | ||||||
|  | sudo touch /var/log/geoip_update.log | ||||||
|  | sudo chown :geoipusers /var/log/geoip_update.log | ||||||
|  | sudo chmod 664 /var/log/geoip_update.log | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | ## Running the Script | ||||||
|  | 
 | ||||||
|  | The script will run as scheduled in the crontab. You can also execute it | ||||||
|  | manually at any time: | ||||||
|  | 
 | ||||||
|  | ```bash | ||||||
|  | /usr/local/bin/geoip_updater.sh | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | ## Troubleshooting | ||||||
|  | 
 | ||||||
|  | - Ensure the script is executable and the `/var/log/geoip_update.log` file is writable. | ||||||
|  | - Verify the crontab entry if the script does not run as expected. | ||||||
|  | - Check the log file `/var/log/geoip_update.log` for errors if the script fails. | ||||||
							
								
								
									
										93
									
								
								geoip_updater.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										93
									
								
								geoip_updater.sh
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,93 @@ | |||||||
|  | #! /bin/bash | ||||||
|  | 
 | ||||||
|  | # Script Name: geoip_updater.sh | ||||||
|  | # Description: This script checks for new GeoLite2 City database releases from | ||||||
|  | #              the specified GitHub repository, downloads the latest release | ||||||
|  | #              if it's not already present, and updates the local database | ||||||
|  | #              file. It ensures the destination directory is writable and | ||||||
|  | #              handles download and extraction of the database. | ||||||
|  | # Usage: Execute without arguments. Designed to be run as a cron job or | ||||||
|  | #        manually as needed. | ||||||
|  | 
 | ||||||
|  | # Exit immediately if a command exits with a non-zero status. | ||||||
|  | set -e | ||||||
|  | 
 | ||||||
|  | # Define variables | ||||||
|  | DATE="2024-02-02" | ||||||
|  | # DATE=$(date -I) | ||||||
|  | DEST_DIR="/usr/local/share/GeoIP" | ||||||
|  | FN="GeoLite2-City-${DATE//-/}.tar.gz" | ||||||
|  | URL="https://github.com/merkez/maxmind-databases/releases/download/${DATE}/${FN}" | ||||||
|  | MMDB="GeoLite2-City.mmdb" | ||||||
|  | DEST="${DEST_DIR}/GeoLite2-City-${DATE//-/}.mmdb" | ||||||
|  | 
 | ||||||
|  | # Check if the target file is already downloaded | ||||||
|  | check_if_file_is_already_downloaded() { | ||||||
|  |   if [ -e "$DEST" ]; then | ||||||
|  |     echo "File is already on the local filesystem" | ||||||
|  |     ensure_symlink_exists | ||||||
|  |     exit 0 | ||||||
|  |   fi | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # Verify destination directory is writable | ||||||
|  | check_if_dest_dir_is_writable_by_user() { | ||||||
|  |   if [ ! -w "$DEST_DIR" ]; then | ||||||
|  |     echo "Destination directory is not writable: ${DEST_DIR}" >&2 | ||||||
|  |     exit 1 | ||||||
|  |   fi | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # Check if the URL exists | ||||||
|  | check_url_exists() { | ||||||
|  |   status_code=$(curl -o /dev/null -I -s -w "%{http_code}" "$URL") | ||||||
|  |    | ||||||
|  |   if [[ "$status_code" -eq 404 ]]; then | ||||||
|  |     echo "No new release for today!" | ||||||
|  |     exit 0 | ||||||
|  |   elif [[ "$status_code" -ge 400 && "$status_code" -lt 500 ]]; then | ||||||
|  |     echo "Client-side error detected: $status_code" >&2 | ||||||
|  |     exit 1 | ||||||
|  |   elif [[ "$status_code" -ge 500 && "$status_code" -lt 600 ]]; then | ||||||
|  |     echo "Server-side error detected: $status_code" >&2 | ||||||
|  |     exit 1 | ||||||
|  |   fi | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # Download the GeoIP package | ||||||
|  | download_geoip_package() { | ||||||
|  |   echo "Downloading ${URL}" | ||||||
|  |   wget -qO "/tmp/${FN}" "$URL" | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # Extract the MMDB file from the downloaded package | ||||||
|  | extract_mmdb() { | ||||||
|  |   MMDB_PATH=$(tar -tzf "/tmp/${FN}" | grep "${MMDB}" || true) | ||||||
|  |   if [ -n "$MMDB_PATH" ]; then | ||||||
|  |     tar -xzf "/tmp/${FN}" "${MMDB_PATH}" && mv "$MMDB_PATH" $DEST | ||||||
|  |   else | ||||||
|  |     echo "Failed to find ${MMDB} in the tarball." >&2 | ||||||
|  |     exit 1 | ||||||
|  |   fi | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # Clean up the temporary files | ||||||
|  | clean_up() { | ||||||
|  |   rm -rf "/tmp/${FN}" "/tmp/$(dirname "${MMDB_PATH}")" | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # Ensure the symlink to the latest database exists | ||||||
|  | ensure_symlink_exists() { | ||||||
|  |   ln -s -f "$DEST" "${DEST_DIR}/${MMDB}" | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # Main execution flow | ||||||
|  | echo "Checking for the GeoLite2 City database release for ${DATE}" | ||||||
|  | check_if_file_is_already_downloaded | ||||||
|  | check_if_dest_dir_is_writable_by_user | ||||||
|  | check_url_exists | ||||||
|  | download_geoip_package | ||||||
|  | extract_mmdb | ||||||
|  | ensure_symlink_exists | ||||||
|  | clean_up | ||||||
|  | echo "GeoLite2 City database update completed." | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user