home · contact · privacy
Improve borgplom bin.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 2 Apr 2025 10:35:17 +0000 (12:35 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 2 Apr 2025 10:35:17 +0000 (12:35 +0200)
testing/home/desktop/.local/bin/borgplom

index a32eec18869322134dfe1ef1bb3cae040497e0d4..2e1089ba0ed5f8b6da3e6bf2f0775a41b741ae00 100755 (executable)
@@ -5,26 +5,71 @@ cd $(dirname "$0")
 . lib/get_passphrase
 . lib/path_tmp_timestamped
 
-PATH_CONF_SECURITY="${PATH_BORG_CONF}/security"
-NAME_REPO=borg
-NAME_ARCHIVE=orgdir
-
-if [ "$1" = "orgpull" ]; then
-    PATH_PIPE="$(path_tmp_timestamped 'pipe')"
-    mkfifo "${PATH_PIPE}"
-    ls -1 "${PATH_CONF_SECURITY}/" > "${PATH_PIPE}" &
-    while read FILENAME; do
-        PATH_LOC="${PATH_CONF_SECURITY}/${FILENAME}/location"
-        NAME_SERVER="$(cat ${PATH_LOC} | cut -d'/' -f3)"
-        if ping -c1 -W2 "${NAME_SERVER}" > /dev/null 2>&1; then
+BIN_NAME="$(basename $0)"
+CMD_HELP=help
+CMD_KEYS=keys
+CMD_ORGPULL=orgpull
+PREFIX_ABORTING='Aborting due to '
+
+print_usage() {
+    echo "Usage: ${BIN_NAME} COMMAND"
+    echo "Wrapper around certain borgbackup usages.\n"
+    echo "Available commands:\n"
+    echo "  ${CMD_HELP}      print this help and exit"
+    echo "  ${CMD_KEYS}      list known repo keys by ID and filename"
+    echo "  ${CMD_ORGPULL}   pull most recent org directory available in repos"
+}
+
+error_exit() {
+    echo "${PREFIX_ABORTING}$1"
+    exit 1
+}
+
+error_exit_with_usage() {
+    echo "${PREFIX_ABORTING}$1\n"
+    print_usage
+    exit 1
+}
+
+check_args_beyond() {
+    shift 2
+    if [ "$#" -gt 0 ]; then
+        echo "WARNING: unexpected arguments beyond command, ignoring: $@\n"
+    fi
+}
+
+cmd_keys() {
+    _PATH_CONF_KEYS="${PATH_BORG_CONF}/keys"
+    echo "Known keys (from ${PATH_BORG_CONF}):"
+    ls -1 "${_PATH_CONF_KEYS}" | while read _FILENAME; do
+        _KEY_ID=$(head -1 "${_PATH_CONF_KEYS}/${_FILENAME}" | cut -d' ' -f2)
+       echo "${_KEY_ID} ${_FILENAME}"
+    done
+}
+
+cmd_orgpull() {
+    _PATH_CONF_SECURITY="${PATH_BORG_CONF}/security"
+    _NAME_REPO=borg
+    _NAME_ARCHIVE=orgdir
+
+    # determine server
+    _PATH_PIPE="$(path_tmp_timestamped 'pipe')"
+    mkfifo "${_PATH_PIPE}"
+    ls -1 "${_PATH_CONF_SECURITY}/" > "${_PATH_PIPE}" &
+    while read _FILENAME; do
+        _PATH_LOC="${_PATH_CONF_SECURITY}/${_FILENAME}/location"
+        _NAME_SERVER="$(cat ${_PATH_LOC} | cut -d'/' -f3)"
+        if ping -c1 -W2 "${_NAME_SERVER}" > /dev/null 2>&1; then
             break
         else
-            echo "Cannot reach ${NAME_SERVER}, skipping."
+            echo "Cannot reach ${_NAME_SERVER}, skipping."
         fi
-    done < "${PATH_PIPE}"
-    rm "${PATH_PIPE}"
-    REPO="${NAME_SERVER}:${NAME_REPO}"
-    echo "Checking out ${REPO} …"
+    done < "${_PATH_PIPE}"
+    rm "${_PATH_PIPE}"
+
+    # determine passphrase and archive
+    _REPO="${_NAME_SERVER}:${_NAME_REPO}"
+    echo "Checking out ${_REPO} …"
     while true; do
         if [ -z "${BORG_PASSPHRASE}" ]; then
             printf 'Passhrase:'
@@ -32,19 +77,35 @@ if [ "$1" = "orgpull" ]; then
             echo ''
         fi
         set +e
-        ARCHIVES=$(borg list "${REPO}")  # separate step so we may fail early on bad passphrase
-        RESULT=$?
+        _ARCHIVES=$(borg list "${_REPO}")  # separate step so we may fail early on bad passphrase
+        _RESULT=$?
         set -e
-        if [ "${RESULT}" = "0" ]; then
+        if [ "${_RESULT}" = "0" ]; then
             break
-        elif [ "${RESULT}" != "2" ]; then
-            echo "Aborting due to unexpected ${NAME_BORGAPP} error."
-            exit 1
+        elif [ "${_RESULT}" != "2" ]; then
+            error_exit "unexpected ${NAME_BORGAPP} error."
         fi
         export BORG_PASSPHRASE=
     done
-    ARCHIVE=$(echo "${ARCHIVES}" | grep "${NAME_ARCHIVE}" | tail -1 | cut -f1 -d' ')
-    echo "Pulling archive: ${ARCHIVE}"
+    _ARCHIVE=$(echo "${_ARCHIVES}" | grep "${_NAME_ARCHIVE}" | tail -1 | cut -f1 -d' ')
+
+    # pull archive
+    echo "Pulling archive: ${_ARCHIVE}"
     cd /
-    borg extract --verbose "${REPO}::${ARCHIVE}"
+    borg extract --verbose "${_REPO}::${_ARCHIVE}"
+}
+
+if [ "$#" -lt 1 ]; then
+    error_exit_with_usage "missing command."
+elif [ "$1" = "${CMD_HELP}" ]; then
+    check_args_beyond
+    print_usage
+elif [ "$1" = "${CMD_KEYS}" ]; then
+    check_args_beyond
+    cmd_keys
+elif [ "$1" = "${CMD_ORGPULL}" ]; then
+    check_args_beyond
+    cmd_orgpull
+else
+    error_exit_with_usage "unexpected command: ${1}"
 fi