From d9bae1afaa87fcea545696c58a1553e60b68ff23 Mon Sep 17 00:00:00 2001
From: Xueshan Feng <xueshan-feng@stanford.edu>
Date: Tue, 21 Sep 2021 21:22:53 -0700
Subject: [PATCH] Update with more precise information why restore is happened.

---
 start.sh | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/start.sh b/start.sh
index c70d5db..b688914 100755
--- a/start.sh
+++ b/start.sh
@@ -46,15 +46,19 @@ function mdb_load() {
     echo "Latest mdb file in backup: $bakData"
     # Restore data and config if LDAP_FORCE_RESTORE is true or newer backup exists or size is smaller then usual.
     echo "Comparing timestamps of $bakData and ${LDAP_MDB_DIR}/data.mdb"
-    if [[ ${LDAP_FORCE_RESTORE} = "true" ]] || [[ ${bakData} -nt ${LDAP_MDB_DIR}/data.mdb ]] \
-        || du -sh ${LDAP_MDB_DIR}/data.mdb | grep -q 'M';
-    then
-        if [[ ${LDAP_FORCE_RESTORE} = "true" ]]; then
-          echo "Force restore from ${bakData}"
-        else
-          du -sh ${LDAP_MDB_DIR}/data.mdb
-          echo -e "\nLoading most recent data and config..."
-        fi
+    RESTORE="false"
+    if [[ ${LDAP_FORCE_RESTORE} = "true" ]]; then
+      echo "Force restore flag is on."
+      RESTORE="true"
+    elif [[ -f ${LDAP_MDB_DIR}/data.mdb ]] && [[ ${bakData} -nt ${LDAP_MDB_DIR}/data.mdb ]]; then
+      echo "${LDAP_MDB_DIR}/data.mdb is newer."
+      RESTORE="true"
+    elif [[ -f ${LDAP_MDB_DIR}/data.mdb ]] && du -sh ${LDAP_MDB_DIR}/data.mdb | grep -q 'M'; then
+      echo "$(du -sh ${LDAP_MDB_DIR}/data.mdb) size is too small."
+      RESTORE="true"
+    fi
+    if [[ ${RESTORE} = "true" ]]; then
+        echo -e "\nLoading most recent data and config from ${bakData}..."
         find ${LDAP_MDB_DIR} -type f -name '*.mdb' -delete
         stat --format "Restoring snapshot '%n' modified on %y" ${bakData}
         cp -v ${bakData} ${LDAP_MDB_DIR}/data.mdb
-- 
GitLab