. lib/path_tmp_timestamped
BIN_NAME="$(basename $0)"
+CMD_CLAIM=claim
CMD_HELP=help
CMD_KEYS=keys
CMD_ORGPULL=orgpull
+location_from_servername() { printf 'ssh://plom@%s/./borg' "$1"; }
+path_repo_location() { printf '%s' "${PATH_BORG_CONF_SECURITY}/${1}/location"; }
+
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 repos in ID, key filename, and alleged location"
- echo " ${CMD_ORGPULL} pull most recent org directory available in repos"
+ echo " ${CMD_CLAIM} PATH register file of PATH as key to repo at \"$(location_from_servername SERVERNAME)\", with SERVERNAME the filename portion of PATH"
+ echo " ${CMD_HELP} print this help and exit"
+ echo " ${CMD_KEYS} list known repos in ID, key filename, and alleged location"
+ echo " ${CMD_ORGPULL} pull most recent org directory available in repos"
}
# exits
# commands beyond print_usage
PATH_BORG_CONF_SECURITY="${PATH_BORG_CONF}/security"
-_path_repo_location() { printf '%s' "${PATH_BORG_CONF_SECURITY}/${1}/location"; }
+PATH_BORG_CONF_KEYS="${PATH_BORG_CONF}/keys"
+
+cmd_claim() {
+ _ensure_no_overwrite_at() {
+ if [ -f "$1" ]; then
+ error_exit "refusing to overwrite pre-existing file at $1"
+ fi
+ }
+ _PATH="$1"
+ _FILENAME=$(basename ${_PATH})
+ _TARGET_PATH_KEY="${PATH_BORG_CONF_KEYS}/${_FILENAME}"
+ if [ ! -f "${_PATH}" ]; then
+ error_exit "no file at ${_PATH}"
+ fi
+ _ensure_no_overwrite_at "${_TARGET_PATH_KEY}"
+ _REPO_ID="$(head -1 ${_PATH} | 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}"
+ fi
+ _TARGET_PATH_LOCATION="$(path_repo_location ${_REPO_ID})"
+ _ensure_no_overwrite_at "${_TARGET_PATH_LOCATION}"
+ mkdir -p "${PATH_BORG_CONF_KEYS}" "${PATH_BORG_CONF_SECURITY}"
+ echo "Copying ${_PATH} to ${_TARGET_PATH_KEY} …"
+ cp "${_PATH}" "${_TARGET_PATH_KEY}"
+ echo "Writing ${_TARGET_PATH_LOCATION} …"
+ echo "$(location_from_servername ${_FILENAME})" > "${_TARGET_PATH_LOCATION}"
+ chmod a-rwx "${_TARGET_PATH_KEY}" "${_TARGET_PATH_LOCATION}"
+ chmod u+rw "${_TARGET_PATH_KEY}" "${_TARGET_PATH_LOCATION}"
+}
cmd_keys() {
- _PATH_BORG_CONF_KEYS="${PATH_BORG_CONF}/keys"
_exit_ok() {
echo "(none, since directory ${1})"
exit 0
}
- echo "Known keys, as per ${_PATH_BORG_CONF_KEYS}:"
- if [ ! -d "${_PATH_BORG_CONF_KEYS}" ]; then
+ echo "Known keys, as per ${PATH_BORG_CONF_KEYS}:"
+ if [ ! -d "${PATH_BORG_CONF_KEYS}" ]; then
_exit_ok "non-existant"
fi
- _KEYFILES="$(ls -1 ${_PATH_BORG_CONF_KEYS})"
+ _KEYFILES="$(ls -1 ${PATH_BORG_CONF_KEYS})"
if [ -z "${_KEYFILES}" ]; then
_exit_ok "empty"
fi
echo "${_KEYFILES}" | while read _FILENAME; do
- _KEY_ID=$(head -1 "${_PATH_BORG_CONF_KEYS}/${_FILENAME}" | cut -d' ' -f2)
- _PATH_LOC="$(_path_repo_location ${_KEY_ID})"
+ _KEY_ID=$(head -1 "${PATH_BORG_CONF_KEYS}/${_FILENAME}" | cut -d' ' -f2)
+ _PATH_LOC="$(path_repo_location ${_KEY_ID})"
printf "${_KEY_ID} ${_FILENAME} "
if [ -f "${_PATH_LOC}" ]; then
printf '%s\n' "$(cat ${_PATH_LOC})"
mkfifo "${_PATH_PIPE}"
ls -1 "${_PATH_BORG_CONF_SECURITY}/" > "${_PATH_PIPE}" &
while read _FILENAME; do
- _REPO="$(cat $(_path_repo_location ${_FILENAME}))"
+ _REPO="$(cat $(path_repo_location ${_FILENAME}))"
_NAME_SERVER="$(echo ${_REPO} | cut -d'/' -f3)"
if ping -c1 -W2 "${_NAME_SERVER}" > /dev/null 2>&1; then
break
# parse args to execution
check_args_beyond() {
- _CMD="$1"
- shift 1
+ _N_MAX_ARGS="$1"
+ _CMD="$2"
+ shift "${_N_MAX_ARGS}"
if [ "$#" -gt 0 ]; then
_MSG="unexpected arguments beyond command '${_CMD}': $@"
error_exit "${_MSG}"
if [ "$#" -lt 1 ]; then
error_exit_with_usage "missing command."
+elif [ "$1" = "${CMD_CLAIM}" ]; then
+ check_args_beyond 2 "$@"
+ cmnd_claim "$1"
elif [ "$1" = "${CMD_HELP}" ]; then
- check_args_beyond "$@"
+ check_args_beyond 1 "$@"
print_usage
elif [ "$1" = "${CMD_KEYS}" ]; then
- check_args_beyond "$@"
+ check_args_beyond 1 "$@"
cmd_keys
elif [ "$1" = "${CMD_ORGPULL}" ]; then
- check_args_beyond "$@"
+ check_args_beyond 1 "$@"
cmd_orgpull
else
error_exit_with_usage "unexpected command: ${1}"