Advertisement
Guest User

borg_add_client.sh

a guest
Oct 23rd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.43 KB | None | 0 0
  1. #!/bin/bash
  2. ####################################################
  3. # borg_add_client.sh - Version: 0.1
  4. # By Gerrit 'Nold' Pannek - 03/2016
  5. # GPLv3 Licensed - https://gnu.org/licenses/gpl.txt
  6. #
  7. # This script helps to add a new ssh client on
  8. # a borg backup server.
  9. #
  10. # Configuration:
  11. #  Change BORG_DIR & BORG_USER variables in this
  12. #  script:
  13. #  - BORG_DIR is the base-directory where all
  14. #    borg-repostories are located.
  15. #  - BORG_USER is the local user which all borg
  16. #    clients use via ssh
  17. #
  18. # Executing:
  19. #  When executing this script, you need to pass two
  20. #  parameters to it:
  21. #  - HOSTNAME: Hostname of the client & will become
  22. #    the repository name
  23. #  - SSH_KEY: SSH-Public-Key of the borg client
  24. #
  25. # Example:
  26. #  # borg_add_client.sh myclient.lan "ssh-rsa AAAAB3NzaC1yc2E... nold@myclient.lan"
  27. ####################################################
  28.  
  29. function usage {
  30.     echo "Add Client to Borg-Backup"
  31.     echo "Usage: $0 <HOSTNAME> <SSH-KEY>"
  32.     echo
  33.     echo $1
  34.     exit 1
  35. }
  36.  
  37. ### CONFIG:
  38. BORG_DIR=/backup
  39. BORG_USER=borg
  40. ### END CONFIG
  41.  
  42. # Params
  43. HOSTNAME=$1
  44. SSH_KEY=$2
  45.  
  46. # Check if everything is setup correctly
  47. [ ! -d "${BORG_DIR}" -o -z "${BORG_USER}" ] && usage "ERROR: You must configure BORG_DIR and BORG_USER correctly."
  48. [ -z "${HOSTNAME}" -o -z "${SSH_KEY}" ] && usage
  49.  
  50. USER_HOME=$(awk -F: "/^${BORG_USER}:/ {print \$6}" /etc/passwd)
  51. [ ! -d "${USER_HOME}" ] && usage "ERROR: User ${BORG_USER} home doesn't exist: ${USER_HOME}"
  52. [ ! -e "${USER_HOME}/.ssh/authorized_keys" ] && usage "ERROR: User ${BORG_USER} authorized_keys doesn't exist: ${USER_HOME}/.ssh/authorized_keys"
  53.  
  54. # Check if TARGET_DIR exists, otherwise create it
  55. TARGET_DIR="${BORG_DIR}/${HOSTNAME}"
  56. [ ! -e "$TARGET_DIR" ] && mkdir -p "$TARGET_DIR" 2>/dev/null && chown -R ${BORG_USER}: "$TARGET_DIR"
  57.  
  58. # Check if SSH_KEY is already in use
  59. # grep -q "${SSH_KEY}" ${USER_HOME}/.ssh/authorized_keys && usage "ERROR: Key already in use!"
  60.  
  61. # Check if TARGET_DIR is already in use
  62. # grep -q "${TARGET_DIR}" ${USER_HOME}/.ssh/authorized_keys && usage "ERROR: TARGET_DIR \"${TARGET_DIR}\" already in use!"
  63.  
  64. # Add Key and Params to authorized_keys
  65. 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
  66.  
  67. echo "Borg repository \"${TARGET_DIR}\" successfully created & SSH-Key Authorized"
  68. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement