From 633c971a4433f171acecf38176d849d5c2beac93 Mon Sep 17 00:00:00 2001
From: DL6ER <dl6er@dl6er.de>
Date: Sat, 23 Nov 2024 09:41:59 +0100
Subject: [PATCH] Store failed gravity restoration status in message table

Signed-off-by: DL6ER <dl6er@dl6er.de>
---
 advanced/Templates/gravity.db.sql | 5 +++++
 gravity.sh                        | 9 ++++++---
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/advanced/Templates/gravity.db.sql b/advanced/Templates/gravity.db.sql
index 5436fb87..3e8603e9 100644
--- a/advanced/Templates/gravity.db.sql
+++ b/advanced/Templates/gravity.db.sql
@@ -67,6 +67,11 @@ CREATE TABLE info
 );
 
 INSERT INTO "info" VALUES('version','19');
+/* This is a flag to indicate if gravity was restored from a backup
+    false = not restored,
+    failed = restoration failed due to no backup
+    other string = restoration successful with the string being the backup file used */
+INSERT INTO "info" VALUES('gravity_restored','false');
 
 CREATE TABLE domain_audit
 (
diff --git a/gravity.sh b/gravity.sh
index b616b2e4..4bf8e9c6 100755
--- a/gravity.sh
+++ b/gravity.sh
@@ -353,12 +353,13 @@ gravity_CheckDNSResolutionAvailable() {
 #   0 - If the backup is successfully restored.
 #   1 - If no backup is available or if the restoration fails.
 try_restore_backup () {
-  local num
+  local num filename
   num=$1
+  filename="${gravityBCKfile}.${num}"
   # Check if a backup exists
-  if [ -f "${gravityBCKfile}.${num}" ]; then
+  if [ -f "${filename}" ]; then
     echo -e "  ${INFO} Attempting to restore previous database from backup no. ${num}"
-    cp "${gravityBCKfile}.${num}" "${gravityDBfile}"
+    cp "${filename}" "${gravityDBfile}"
 
     # If the backup was successfully copied, prepare a new gravity database from
     # it
@@ -372,6 +373,7 @@ try_restore_backup () {
         gravity_Cleanup "error"
       fi
 
+      pihole-FTL sqlite3 "${gravityTEMPfile}" "INSERT OR REPLACE INTO info (property,value) values ('gravity_restored','${filename}');"
       echo -e "  ${TICK} Successfully restored from backup (${gravityBCKfile}.${num})"
       return 0
     else
@@ -434,6 +436,7 @@ gravity_DownloadBlocklists() {
     # If none of the attempts worked, return 1
     if [[ "${i}" -eq 10 ]]; then
       return 1
+      pihole-FTL sqlite3 "${gravityTEMPfile}" "INSERT OR REPLACE INTO info (property,value) values ('gravity_restored','failed');"
     fi
 
     echo -e "  ${TICK} ${str}"