From 47479a34058f3df4197bc0fc959a9e4edd05118f Mon Sep 17 00:00:00 2001
From: David Beniamine <david.beniamine@tetras-libre.fr>
Date: Fri, 7 May 2021 17:47:52 +0200
Subject: [PATCH] Allow to choose confifile

---
 backup.sh       |  4 ++++
 clear_backup.sh |  4 ++++
 inc.sh          |  5 ++++-
 restore_file.sh | 37 ++++++++++++++++++++++++++++---------
 4 files changed, 40 insertions(+), 10 deletions(-)

diff --git a/backup.sh b/backup.sh
index 3c0ca15..2054bf5 100755
--- a/backup.sh
+++ b/backup.sh
@@ -1,6 +1,10 @@
 #!/bin/bash
 
 DIR=$(realpath $(dirname $0))
+if [ ! -z "$1" ]; then
+   conf_file=$1
+   shift
+fi
 source $DIR/inc.sh
 
 exit_on_fail(){
diff --git a/clear_backup.sh b/clear_backup.sh
index 8cb9b41..1204b54 100755
--- a/clear_backup.sh
+++ b/clear_backup.sh
@@ -1,6 +1,10 @@
 #!/bin/bash
 
 DIR=$(realpath $(dirname $0))
+if [ ! -z "$1" ]; then
+   conf_file=$1
+   shift
+fi
 source $DIR/inc.sh
 
 export PASSPHRASE="$encpass"
diff --git a/inc.sh b/inc.sh
index c1d568a..d0ff899 100755
--- a/inc.sh
+++ b/inc.sh
@@ -1,7 +1,10 @@
 #!/bin/bash
 
 DIR=$(realpath $(dirname $0))
-source $DIR/.env
+if [ -z "$conf_file" ]; then
+    conf_file="$DIR/.env"
+fi
+source $conf_file
 
 # Exit the script and sendmail
 # $1 : exit code
diff --git a/restore_file.sh b/restore_file.sh
index d635327..8b7e9aa 100755
--- a/restore_file.sh
+++ b/restore_file.sh
@@ -1,23 +1,42 @@
 #!/bin/bash
 DIR=$(realpath $(dirname $0))
-source $DIR/inc.sh
-
-if [ -z "$2" ]; then
-    echo "Usage $0 <restore_path> <destination_path> [timeago]"
+usage(){
+    echo "Usage $0 [options] <restore_path> <destination_path>"
     echo "restore_path      :   path to restore without leading slash"
     echo "destination_path  :   path to download the file(s), duplicity will not override existing files"
-    echo "timeago           :   optionnal : the age of the version we want to restore, for instance 6D for 6 days ago"
+    echo "options"
+    echo "    -t timeago    :   the age of the version we want to restore, for instance 6D for 6 days ago"
+    echo "    -c file       :   config_file to use"
+}
+
+while getopts "t:c:h" arg; do
+    case ${arg} in
+        h)
+            usage
+            exit 0
+            ;;
+        t)
+            delay="-t $OPTARG"
+            ;;
+        c)
+            conf_file=$OPTARG
+            ;;
+    esac
+done
+restore_path=${@:$OPTIND:1}
+destination_path=${@:$OPTIND+1:1}
+
+if [ -z "$destination_path" ]; then
+    usage
     exit 1
 fi
 
-if [ ! -z "$3" ]; then
-    delay="-t $3"
-fi
+source $DIR/inc.sh
 
 export PASSPHRASE="$encpass"
 export FTP_PASSWORD="$ftppass"
 
-/usr/bin/duplicity --file-to-restore $delay "$1" ftp://$user@$host/$remotedir/ "$2"
+/usr/bin/duplicity --file-to-restore $delay "$restore_path" ftp://$user@$host/$remotedir/ "$destination_path"
 
 unset PASSPHRASE
 unset FTP_PASSWORD
-- 
GitLab