update-location-database: Only update database once a week.

Message ID 20200728200246.3834-1-stefan.schantl@ipfire.org
State Superseded
Headers
Series update-location-database: Only update database once a week. |

Commit Message

Stefan Schantl July 28, 2020, 8:02 p.m. UTC
  Ensure to download and update the database only once a week, even the
script will be called by cron each hour.

Also detect of the database has been changed and only in this case
re-export the database.

Fixes #12462.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 src/scripts/update-location-database | 32 +++++++++++++++++++++++-----
 1 file changed, 27 insertions(+), 5 deletions(-)
  

Patch

diff --git a/src/scripts/update-location-database b/src/scripts/update-location-database
index d830286ce..b3eb27c92 100644
--- a/src/scripts/update-location-database
+++ b/src/scripts/update-location-database
@@ -21,6 +21,19 @@ 
 
 eval $(/usr/local/bin/readhash /var/ipfire/proxy/settings)
 
+DB_FILE="/var/lib/location/database.db"
+UPDATE_INTERVAL="weekly"
+
+# Tiny function to get the current timestamp of the database file.
+function get_db_timestamp {
+	local timestamp=`stat -c "%Y" $DB_FILE`
+
+	echo $timestamp
+}
+
+# Get database timestamp.
+database_timestamp=$(get_db_timestamp)
+
 # Proxy settings.
 # Check if a proxy should be used.
 if [[ $UPSTREAM_PROXY ]]; then
@@ -39,11 +52,20 @@  if [[ $UPSTREAM_PROXY ]]; then
 fi
 
 # Get the latest location database from server.
-if /usr/bin/location update; then
-	# Call location and export all countries in xt_geoip compatible format.
-	if /usr/bin/location export --directory=/usr/share/xt_geoip --family=ipv4 --format=xt_geoip; then
+if /usr/bin/location update --cron=$UPDATE_INTERVAL; then
+	# Grab timestamp of the database again.
+	new_database_timestamp=$(get_db_timestamp)
 
-		# Call initscript to reload the firewall.
-		/etc/init.d/firewall reload
+	# Check if the timestamps are different.
+	# In this case it needs to be exported again.
+	if [ ! $database_timestamp eq $new_database_timestamp ]; then
+		# Call location and export all countries in xt_geoip compatible format.
+		if /usr/bin/location export --directory=/usr/share/xt_geoip --family=ipv4 --format=xt_geoip; then
+
+			# Call initscript to reload the firewall.
+			/etc/init.d/firewall reload
+		fi
 	fi
 fi
+
+