You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.5 KiB

  1. #!/bin/bash
  2. . /etc/shlib
  3. include common
  4. include parse
  5. include cmdline
  6. include config
  7. [[ "${BASH_SOURCE[0]}" != "${0}" ]] && SOURCED=true
  8. version=0.1
  9. desc='Install backup'
  10. help=""
  11. [ "$SOURCED" ] && return 0
  12. ##
  13. ## Command line processing
  14. ##
  15. cmdline.spec.gnu
  16. cmdline.spec.reporting
  17. cmdline.spec.gnu install
  18. cmdline.spec.gnu backup
  19. cmdline.spec::cmd:install:run() {
  20. :
  21. }
  22. cmdline.spec:install:cmd:backup:run() {
  23. : :posarg: BACKUP_SERVER 'Target backup server'
  24. : :optval: --service-name,-s "YAML service name in compose
  25. file to check for existence of key.
  26. Defaults to 'rsync-backup'"
  27. : :optval: --compose-file,-f "Compose file location. Defaults to
  28. the value of '\$DEFAULT_COMPOSE_FILE'"
  29. local service_name compose_file
  30. [ -e "/etc/compose/local.conf" ] && source /etc/compose/local.conf
  31. compose_file=${opt_compose_file:-$DEFAULT_COMPOSE_FILE}
  32. service_name=${opt_service_name:-rsync-backup}
  33. if ! [ -e "$compose_file" ]; then
  34. err "Compose file not found in '$compose_file'."
  35. return 1
  36. fi
  37. ## XXXvlab: far from perfect as it mimics and depends internal
  38. ## logic of current default way to get a domain in compose-core
  39. host=$(hostname)
  40. if ! egrep "^$host:" "$compose_file" >/dev/null &&
  41. ! egrep "^\s+domain:\s+$host\s*$" "$compose_file" >/dev/null; then
  42. err "Can't find domain '$host' in compose file '$compose_file'."
  43. return 1
  44. fi
  45. ip=$(getent ahosts "$host" | egrep "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s+" | head -n 1 | cut -f 1 -d " ") || return 1
  46. my_ip=$(curl -s myip.kal.fr)
  47. if [ "$ip" != "$my_ip" ]; then
  48. err "IP of '$host' ($ip) doesn't match mine ($my_ip)."
  49. return 1
  50. fi
  51. if [ -e "/root/.ssh/rsync_rsa" ]; then
  52. if ! [ -e "/root/.ssh/rsync_rsa.pub" ]; then
  53. err "Didn't find public key in '/root/.ssh/rsync_rsa.pub'. (Private key is present !)."
  54. return 1
  55. fi
  56. else
  57. Wrap -d "Creating rsync key pair" -- \
  58. ssh-keygen -t rsa -N \"\" -f /root/.ssh/rsync_rsa -C "rsync@$host"
  59. fi
  60. if egrep "^$service_name:" "$compose_file" >/dev/null; then
  61. err "There's already a backup service named '$service_name'"
  62. return 1
  63. fi
  64. cat <<EOF >> "$compose_file"
  65. $service_name:
  66. options:
  67. ident: $host
  68. target: $BACKUP_SERVER
  69. private-key: |
  70. $(cat /root/.ssh/rsync_rsa | sed -r 's/^/ /g')
  71. EOF
  72. }
  73. cmdline::parse "$@"