#!/bin/sh
set -e
cd $(dirname "$0")
-. lib/constants_borg
+. lib/constants_borg # PATH_BORG_CONF
. lib/get_passphrase
. lib/path_tmp_timestamped
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() {
}
# exits
-
_PREFIX_ABORTING='Aborting due to '
error_exit() {
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() {
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})"
_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
}
HELP_DESC_orgpull='pull most recent org directory available in repos'
-cmd_orgpull() {
+CMDFNC_orgpull() {
_check_args_beyond 0 "$@"
_NAME_ARCHIVE=orgdir
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."
}
# parse args to execution
-
if [ "$#" -lt 1 ]; then
error_exit_with_usage "missing command."
fi
error_exit_with_usage "unknown command: ${1}"
fi
shift 1
-"cmd_${CMD}"
+"CMDFNC_${CMD}"
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
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"
. 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'
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