home · contact · privacy
Fix.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 3 Apr 2025 00:48:39 +0000 (02:48 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 3 Apr 2025 00:48:39 +0000 (02:48 +0200)
testing/home/desktop/.local/bin/borgplom
testing/home/desktop/.local/bin/make_secrets
testing/scripts/_setup_secrets_user.sh
testing/scripts/lib/constants_borg
testing/scripts/lib/constants_secrets

index 4957bb5f9103b3097cba4d24568c53f643768dde..582a90a2b2234a0fc16c7007ad481ae74eea9f56 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/sh
 set -e
 cd $(dirname "$0")
-. lib/constants_borg
+. lib/constants_borg  # PATH_BORG_CONF
 . lib/get_passphrase
 . lib/path_tmp_timestamped
 
@@ -9,7 +9,8 @@ BIN_NAME="$(basename $0)"
 
 PATH_BORG_CONF_SECURITY="${PATH_BORG_CONF}/security"
 PATH_BORG_CONF_KEYS="${PATH_BORG_CONF}/keys"
-location_from_servername() { printf 'ssh://plom@%s/./borg' "$1"; }
+servername_to_location() { printf 'ssh://plom@%s/./borg' "$1"; }
+location_to_servername() { echo "$1" | cut -d'/' -f3 | cut -d'@' -f2; }
 path_repo_location() { printf '%s' "${PATH_BORG_CONF_SECURITY}/${1}/location"; }
 
 _print_usage() {
@@ -40,7 +41,6 @@ _print_usage() {
 }
 
 # exits
-
 _PREFIX_ABORTING='Aborting due to '
 
 error_exit() {
@@ -67,9 +67,42 @@ _check_args_beyond() {
     fi
 }
 
+_id_from_file() {
+    _ID=$(head -1 "$1" | cut -d'/' -f2)
+    if [ ! -z "$(echo ${_ID} | sed 's/[a-f0-9]//g')" ]; then
+        error_exit "inability to parse valid repo ID from alleged key file at $1"
+    fi
+    printf "${_ID}"
+}
+
+HELP_ARGS_backup_keys=' PATH'
+HELP_DESC_backup_keys="copy known keys to PATH/, with their repos' server names as filenames"
+CMDFNC_backup_keys() {
+    _check_args_beyond 1 "$@"
+    _PATH_TARGET_DIR="$1"
+    if [ -e "${_PATH_TARGET_DIR}"] && [ ! -d "${_PATH_TARGET_DIR}" ]; then
+        error_exit "non-directory at ${_PATH_TARGET_DIR}"
+    fi
+    mkdir -p "${_PATH_TARGET_DIR}"
+    cd "${PATH_BORG_CONF_KEYS}"
+    ls -1 | while read _FILENAME; do
+        _KEY_ID=$(_id_from_file "${_FILENAME}")
+        _PATH_LOC="$(path_repo_location ${_KEY_ID})"
+        if [ ! -f "${_PATH_LOC}" ]; then
+            echo "Ignoring ${_FILENAME} due to lack of matching location file."
+            continue
+        fi
+       _NAME_SERVER=$(location_to_servername "$(cat ${_PATH_LOC})")
+        _PATH_TARGET="${_PATH_TARGET_DIR}/${_NAME_SERVER}"
+        echo "Copying ${_FILENAME} to ${_PATH_TARGET} …"
+        cp "${_FILENAME}" "${_PATH_TARGET}"
+    done
+    cd -
+}
+
 HELP_ARGS_claim=' PATH'
-HELP_DESC_claim="register file of PATH as key to repo at \"$(location_from_servername SERVERNAME)\", with SERVERNAME the filename portion of PATH"
-cmd_claim() {
+HELP_DESC_claim="register file of PATH as key to repo at \"$(servername_to_location SERVERNAME)\", with SERVERNAME the filename portion of PATH"
+CMDFNC_claim() {
     _check_args_beyond 1 "$@"
     _PATH_SOURCE="$1"
     _ensure_no_overwrite_at() {
@@ -84,29 +117,26 @@ cmd_claim() {
         error_exit "no file at ${_PATH_SOURCE}"
     fi
     _ensure_no_overwrite_at "${_PATH_TARGET_KEY}"
-    _REPO_ID="$(head -1 ${_PATH_SOURCE} | cut -d' ' -f2)"
-    if [ ! -z "$(echo ${_REPO_ID} | sed 's/[a-f0-9]//g')" ]; then
-        error_exit "inability to parse valid repo ID from alleged key file at ${_PATH_SOURCE}"
-    fi
+    _REPO_ID="$(_id_from_file ${_PATH_SOURCE})"
     _PATH_TARGET_LOCATION="$(path_repo_location ${_REPO_ID})"
     _ensure_no_overwrite_at "${_PATH_TARGET_LOCATION}"
     mkdir -p "${PATH_BORG_CONF_KEYS}" "$(dirname ${_PATH_TARGET_LOCATION})"
     echo "Copying ${_PATH_SOURCE} to ${_PATH_TARGET_KEY} …"
     cp "${_PATH_SOURCE}" "${_PATH_TARGET_KEY}"
     echo "Writing ${_PATH_TARGET_LOCATION} …"
-    printf '%s' "$(location_from_servername ${_FILENAME})" > "${_PATH_TARGET_LOCATION}"
+    printf '%s' "$(servername_to_location ${_FILENAME})" > "${_PATH_TARGET_LOCATION}"
     chmod a-rwx "${_PATH_TARGET_KEY}" "${_PATH_TARGET_LOCATION}"
     chmod u+rw "${_PATH_TARGET_KEY}" "${_PATH_TARGET_LOCATION}"
 }
 
 HELP_DESC_help='print this help and exit'
-cmd_help() {
+CMDFNC_help() {
     _check_args_beyond 0 "$@"
     _print_usage
 }
 
 HELP_DESC_keys='list known repos in ID, key filename, and alleged location'
-cmd_keys() {
+CMDFNC_keys() {
     _check_args_beyond 0 "$@"
     _exit_ok() {
        echo "(none, since directory ${1})"
@@ -122,7 +152,7 @@ cmd_keys() {
        _exit_ok "empty"
     fi
     echo "${_KEYFILES}" | while read _FILENAME; do
-        _KEY_ID=$(head -1 "${PATH_BORG_CONF_KEYS}/${_FILENAME}" | cut -d' ' -f2)
+        _KEY_ID=$(_id_from_file "${PATH_BORG_CONF_KEYS}/${_FILENAME}")
        _PATH_LOC="$(path_repo_location ${_KEY_ID})"
         printf "${_KEY_ID} ${_FILENAME} "
         if [ -f "${_PATH_LOC}" ]; then
@@ -134,7 +164,7 @@ cmd_keys() {
 }
 
 HELP_DESC_orgpull='pull most recent org directory available in repos'
-cmd_orgpull() {
+CMDFNC_orgpull() {
     _check_args_beyond 0 "$@"
     _NAME_ARCHIVE=orgdir
 
@@ -143,10 +173,10 @@ cmd_orgpull() {
     mkfifo "${_PATH_PIPE}"
     ls -1 "${PATH_BORG_CONF_SECURITY}/" > "${_PATH_PIPE}" &
     while read _FILENAME; do
-        _TESTED_REPO="$(cat $(path_repo_location ${_FILENAME}))"
-        _NAME_SERVER="$(echo ${_TESTED_REPO} | cut -d'/' -f3 | cut -d'@' -f2)"
+        _LOCATION="$(cat $(path_repo_location ${_FILENAME}))"
+       _NAME_SERVER=$(location_to_servername "${_LOCATION}")
         if ping -c1 -W2 "${_NAME_SERVER}" > /dev/null 2>&1; then
-            _REPO="${_TESTED_REPO}"
+            _REPO="${_LOCATION}"
             break
         else
             echo "Cannot reach ${_NAME_SERVER}, skipping."
@@ -185,7 +215,6 @@ cmd_orgpull() {
 }
 
 # parse args to execution
-
 if [ "$#" -lt 1 ]; then
     error_exit_with_usage "missing command."
 fi
@@ -199,4 +228,4 @@ if [ -z "${CMD}" ]; then
     error_exit_with_usage "unknown command: ${1}"
 fi
 shift 1
-"cmd_${CMD}"
+"CMDFNC_${CMD}"
index 2737a0d4c75bc4ba50f9eb654c860188f1ce6b53..344214e90d37af0c2ce1fb93d8a9a051113069a6 100755 (executable)
@@ -3,8 +3,8 @@ set -e
 
 cd $(dirname "$0")
 . lib/abort_if_exists
-. lib/constants_borg  # PATH_BORG_CONF
-. lib/constants_secrets  # PATH_REL_SECRETS, PATH_SECRETS
+. lib/constants_borg  # NAME_BORGAPP, PATH_BORG_CONF
+. lib/constants_secrets  # PATH_REL_SECRETS, PATH_SECRETS, PATH_SECRETS_BORGKEYS
 . lib/constants_ssh  # PATH_USER_SSH 
 . lib/constants_user  # USERNAME
 . lib/mount_secrets  # mount_secrets, copy_and_unmount_secrets
@@ -12,7 +12,7 @@ cd $(dirname "$0")
 abort_if_exists "${PATH_SECRETS}"
 echo "Collecting new ${PATH_REL_SECRETS}."
 mkdir "${PATH_SECRETS}"
-cp -a "${PATH_BORG_CONF}" "${PATH_SECRETS}/"
+"${NAME_BORGAPP}" backup_keys "${PATH_SECRETS_BORGKEYS}"
 cp -a "${PATH_USER_SSH}" "${PATH_SECRETS_SSH}"
 echo "secrets file, last update: $(whoami)/$(hostname) at $(date)" > "${PATH_SECRETS}/info"
 
index c80c652acfec42d574f5433b571f9c33788cb7d5..5192e7de77acc2473df8c7fc7724c666f75282a7 100644 (file)
@@ -5,23 +5,21 @@ cd $(dirname "$0")
 . lib/abort_if_exists
 . lib/abort_if_not_user
 . lib/abort_if_offline
-. lib/constants_borg  # PATH_BORG_CONF, PATH_REL_BORG_CONF
-. lib/constants_secrets  # PATH_SECRETS
-. lib/constants_ssh  # PATH_USER_SSH 
+. lib/constants_secrets  # PATH_SECRETS, PATH_SECRETS_BORGKEYS
+. lib/constants_ssh  # PATH_USER_SSH
 . lib/constants_user  # USERNAME
+. lib/constants_borg  # NAME_BORGAPP
 . lib/mount_secrets  # mount_secrets, copy_and_unmount_secrets
 
 PATH_REPOS="${HOME}/repos"
 REPOS_SITE_DOMAIN=plomlompom.com
 REMOTE_PATH_REPOS=/var/repos
-NAME_BORGAPP=borgplom
 
 abort_if_offline
 abort_if_not_user "${USERNAME}"
 abort_if_exists "${PATH_SECRETS}"
 abort_if_exists "${PATH_USER_SSH}"
 abort_if_exists "${PATH_REPOS}"
-abort_if_exists "${PATH_BORG_CONF}"
 
 mount_secrets "$1" # sets PASSPHRASE
 copy_and_unmount_secrets 'in'
@@ -55,9 +53,11 @@ done
 cd -
 
 echo "\nSetting up borg and pull in ~/org"
-PATH_PARENT_BORG_CONF="$(dirname ${PATH_BORG_CONF})"
-mkdir -p "${PATH_BORG_CONF}"
-cp -a "${PATH_SECRETS}/${PATH_REL_BORG_CONF}" "${PATH_PARENT_BORG_CONF}/"
+cd "${PATH_SECRETS_BORGKEYS}"
+ls -1 | while read _FILENAME; do
+    "${NAME_BORGAPP}" claim "${_FILENAE}"
+done
+cd -
 while true; do
     set +e
     "${NAME_BORGAPP}" orgpull
index e3457505cc317a66a6377809ee2ce9a24925ff48..befcb4a65b389f1615ed6f522f2d0aab23242810 100644 (file)
@@ -1,2 +1,2 @@
-PATH_REL_BORG_CONF=borg
-PATH_BORG_CONF="${HOME}/.config/${PATH_REL_BORG_CONF}"
+NAME_BORGAPP=borgplom
+PATH_BORG_CONF="${HOME}/.config/borg"
index 8e11fe71bf5fbccaff41cdb99b4071f7656b6b72..ddd6064c809ffdce01d259788edebade2d60b59a 100644 (file)
@@ -3,3 +3,4 @@ PATH_MEDIA=/media
 PATH_REL_SECRETS=.secrets
 PATH_SECRETS="${PATH_USER_HOME}/${PATH_REL_SECRETS}"
 PATH_SECRETS_SSH="${PATH_SECRETS}/ssh"
+PATH_SECRETS_BORGKEYS="${PATH_SECRETS}/borgkeys"