Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ####################################################
- # borg_add_client.sh - Version: 0.1
- # By Gerrit 'Nold' Pannek - 03/2016
- # GPLv3 Licensed - https://gnu.org/licenses/gpl.txt
- #
- # This script helps to add a new ssh client on
- # a borg backup server.
- #
- # Configuration:
- # Change BORG_DIR & BORG_USER variables in this
- # script:
- # - BORG_DIR is the base-directory where all
- # borg-repostories are located.
- # - BORG_USER is the local user which all borg
- # clients use via ssh
- #
- # Executing:
- # When executing this script, you need to pass two
- # parameters to it:
- # - HOSTNAME: Hostname of the client & will become
- # the repository name
- # - SSH_KEY: SSH-Public-Key of the borg client
- #
- # Example:
- # # borg_add_client.sh myclient.lan "ssh-rsa AAAAB3NzaC1yc2E... nold@myclient.lan"
- ####################################################
- function usage {
- echo "Add Client to Borg-Backup"
- echo "Usage: $0 <HOSTNAME> <SSH-KEY>"
- echo
- echo $1
- exit 1
- }
- ### CONFIG:
- BORG_DIR=/backup
- BORG_USER=borg
- ### END CONFIG
- # Params
- HOSTNAME=$1
- SSH_KEY=$2
- # Check if everything is setup correctly
- [ ! -d "${BORG_DIR}" -o -z "${BORG_USER}" ] && usage "ERROR: You must configure BORG_DIR and BORG_USER correctly."
- [ -z "${HOSTNAME}" -o -z "${SSH_KEY}" ] && usage
- USER_HOME=$(awk -F: "/^${BORG_USER}:/ {print \$6}" /etc/passwd)
- [ ! -d "${USER_HOME}" ] && usage "ERROR: User ${BORG_USER} home doesn't exist: ${USER_HOME}"
- [ ! -e "${USER_HOME}/.ssh/authorized_keys" ] && usage "ERROR: User ${BORG_USER} authorized_keys doesn't exist: ${USER_HOME}/.ssh/authorized_keys"
- # Check if TARGET_DIR exists, otherwise create it
- TARGET_DIR="${BORG_DIR}/${HOSTNAME}"
- [ ! -e "$TARGET_DIR" ] && mkdir -p "$TARGET_DIR" 2>/dev/null && chown -R ${BORG_USER}: "$TARGET_DIR"
- # Check if SSH_KEY is already in use
- # grep -q "${SSH_KEY}" ${USER_HOME}/.ssh/authorized_keys && usage "ERROR: Key already in use!"
- # Check if TARGET_DIR is already in use
- # grep -q "${TARGET_DIR}" ${USER_HOME}/.ssh/authorized_keys && usage "ERROR: TARGET_DIR \"${TARGET_DIR}\" already in use!"
- # Add Key and Params to authorized_keys
- echo "command=\"cd ${TARGET_DIR}; borg serve --restrict-to-path ${TARGET_DIR}\",no-port-forwarding,no-X11-forwarding,no-pty,no-agent-forwarding,no-user-rc ${SSH_KEY}" >> ${USER_HOME}/.ssh/authorized_keys
- echo "Borg repository \"${TARGET_DIR}\" successfully created & SSH-Key Authorized"
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement