From d145fc8683137b721bf9a992e90a13a5bca7d539 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Fri, 17 Mar 2023 10:46:33 +0100
Subject: [PATCH] replace hostname -f with $FQDN

---
 jobhelper.sh               | 34 ++++++++++++++++++++++++++++++++--
 plugins/transfer/restic.sh |  3 +--
 transfer.sh                |  4 ++--
 3 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/jobhelper.sh b/jobhelper.sh
index a05467c..2d9188e 100755
--- a/jobhelper.sh
+++ b/jobhelper.sh
@@ -13,7 +13,7 @@
 # 2017-02-16  ah,ds  v1.2  added storage helper function
 # 2018-02-13  ah,ds  v1.3  detect samba shares based on a flag
 # 2022-10-07  ah     v1.4  unescape regex with space to prevent "grep: warning: stray \ before white space"
-# 2023-03-16  ah     v1.5  ignore required user on MS windows; source jobs/env if it exists
+# 2023-03-17  ah     v1.5  ignore required user on MS windows; source jobs/env if it exists; use varaiable FQDN
 # ================================================================================
 
 
@@ -48,6 +48,8 @@ function j_init(){
     . "${DIR_JOBS}/env"
   fi
 
+  _getFqdn
+
   if [ ! -d "${DIR_LOGS}" ]; then
     mkdir -p "${DIR_LOGS}" && echo "INFO: dir created ${DIR_LOGS}"
   fi
@@ -150,7 +152,7 @@ function j_getFullTarget(){
   sTmpSafeName=$(j_getSafename "$1")
   sTmpHostname=$2
   if [ -z "$sTmpHostname" ]; then
-    sTmpHostname=$(hostname -f)
+    sTmpHostname=$FQDN
   fi
   if [ -z "${STORAGE_BASEDIR}" ]; then
     STORAGE_BASEDIR=$(_j_getvar "${STORAGEFILE}" "storage")
@@ -386,6 +388,34 @@ function _isMswindows(){
   uname | grep -iE "(CYGWIN_NT|MINGW|MSYS_NT|Windows_NT|WindowsNT)" >/dev/null
 }
 
+# get fqdn and put it into FQDN; called in j_init
+# - with gnu core utils: hostname -f
+# - on ms windows: grep from output of ipconfig and %COMPUTERNAME%
+function _getFqdn(){
+  if [ -z "$FQDN" ]; then
+    if ! FQDN=$( hostname -f 2>/dev/null ); then
+      echo "INFO: hostname -f is not available"
+      if _isMswindows; then
+
+        if [ -n "$COMPUTERNAME" ]; then
+          local _domain
+          _domain=$( ipconfig -all | grep "Primary Dns" | cut -f 2 -d ':' | tr -d ' ' )
+          FQDN="${COMPUTERNAME}.${_domain}"
+        fi
+      fi
+    fi
+    if [ -z "$FQDN" ]; then
+      echo "ERROR: unable to detect fqdn. Aborting."
+      exit 1
+    fi
+  fi
+  if ! echo "$FQDN" | grep "\..*" >/dev/null; then
+    echo "ERROR: fqdn $FQDN does not contain a domain name. Aborting."
+    exit 1
+  fi
+  # echo "INFO: FQDN is [$FQDN]"
+}
+
 # ------------------------------------------------------------
 # check if it was startet with a given user
 # This is skipped if MS windows was detected with "mingw".
diff --git a/plugins/transfer/restic.sh b/plugins/transfer/restic.sh
index 73035ac..190a5c5 100644
--- a/plugins/transfer/restic.sh
+++ b/plugins/transfer/restic.sh
@@ -38,8 +38,7 @@
         local _target=$( j_getFullTarget )
         echo ${_target} | grep "https.*@" >/dev/null
         if [ $? -eq 0 ]; then
-            local _host=$( hostname -f )
-            _target=$( echo $_target | sed "s#${_host}/##" )
+            _target=$( echo $_target | sed "s#${FQDN}/##" )
         fi
 
         export RESTIC_REPOSITORY=$_target
diff --git a/transfer.sh b/transfer.sh
index c34e502..8443e42 100755
--- a/transfer.sh
+++ b/transfer.sh
@@ -323,7 +323,7 @@ function setAction(){
   else
     iExit=1
     until [ $iExit -eq 0 ]; do
-      registerBackupSlot "$(hostname -f)"
+      registerBackupSlot "$FQDN"
       iExit=$?
       if [ $iExit -ne 0 ]; then
           statusBackupSlot
@@ -482,7 +482,7 @@ function setAction(){
   if [ -z "$STORAGE_REGISTER" ]; then
     echo "SKIP"
   else
-    unregisterBackupSlot "$(hostname -f)" $rc
+    unregisterBackupSlot "$FQDN" $rc
   fi
 
   h2 "$( date )  Backup finished"
-- 
GitLab