Managing SSH chroots to backup a lot of machines
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.

132 lines
3.3 KiB

  1. #!/bin/sh
  2. #
  3. # bkctld is a shell script to create and manage a backup server which will
  4. # handle the backup of many servers (clients).
  5. #
  6. # Authors: Victor Laborie <vlaborie@evolix.fr>
  7. # Jérémy Lecour <jlecour@evolix.fr>
  8. # Benoît Série <bserie@evolix.fr>
  9. # Gregory Colpart <reg@evolix.fr>
  10. # Romain Dessort <rdessort@evolix.fr>
  11. # Tristan Pilat <tpilat@evolix.fr>
  12. # Licence: AGPLv3
  13. #
  14. set -u
  15. if [ "$(id -u)" -ne 0 ]; then
  16. echo "You need to be root to run ${0} !" >&2
  17. exit 1
  18. fi
  19. basedir=$(dirname "$0")
  20. if [ "${basedir}" = "/usr/local/sbin" ] && [ -d "/usr/local/lib/bkctld" ]; then
  21. LIBDIR='/usr/local/lib/bkctld'
  22. elif [ "${basedir}" = "/usr/sbin" ] && [ -d "/usr/lib/bkctld" ]; then
  23. LIBDIR='/usr/lib/bkctld'
  24. elif [ -d './lib' ]; then
  25. LIBDIR='lib'
  26. else
  27. echo "Failed to find a suitable lib directory for bkctld." >&2
  28. exit 1
  29. fi
  30. # shellcheck source=lib/includes
  31. . "${LIBDIR}/includes"
  32. # Parse options
  33. # based on https://gist.github.com/deshion/10d3cb5f88a21671e17a
  34. if [ "$#" = "0" ]; then
  35. show_help
  36. exit 1
  37. fi
  38. while :; do
  39. case $1 in
  40. -h|-\?|--help)
  41. show_help
  42. exit 0
  43. ;;
  44. -V|--version)
  45. show_version
  46. exit 0
  47. ;;
  48. -f|--force)
  49. FORCE=1
  50. ;;
  51. *)
  52. # Default case: If no more options then break out of the loop.
  53. break
  54. ;;
  55. esac
  56. shift
  57. done
  58. subcommand="${1:-}"
  59. case "${subcommand}" in
  60. "inc" | "rm" | "check-jails" | "check-setup" | "stats" | "list")
  61. "${LIBDIR}/bkctld-${subcommand}"
  62. ;;
  63. "check")
  64. # backward compatibility
  65. "${LIBDIR}/bkctld-check-jails"
  66. ;;
  67. "check-incs")
  68. option="${2:-}"
  69. if [ "${option}" = "all" ] || [ -z "${option}" ]; then
  70. "${LIBDIR}/bkctld-check-incs"
  71. elif [ "${option}" = "last" ]; then
  72. "${LIBDIR}/bkctld-check-last-incs"
  73. else
  74. show_help
  75. exit 1
  76. fi
  77. ;;
  78. "init" | "is-on")
  79. jail_name="${2:-}"
  80. if [ -z "${jail_name}" ]; then
  81. show_help
  82. exit 1
  83. else
  84. "${LIBDIR}/bkctld-${subcommand}" "${jail_name}"
  85. fi
  86. ;;
  87. "key" | "port" | "ip")
  88. jail_name="${2:-}"
  89. option="${3:-}"
  90. if [ "${jail_name}" = "all" ] || [ -z "${jail_name}" ]; then
  91. show_help
  92. exit 1
  93. else
  94. "${LIBDIR}/bkctld-${subcommand}" "${jail_name}" "${option}"
  95. fi
  96. ;;
  97. "start" | "stop" | "reload" | "restart" | "sync" | "update" | "remove" | "firewall" | "upgrade-config")
  98. jail_name="${2:-}"
  99. if [ "${jail_name}" = "all" ]; then
  100. for jail in $("${LIBDIR}/bkctld-list"); do
  101. "${LIBDIR}/bkctld-${subcommand}" "${jail}"
  102. done
  103. elif [ -z "${jail_name}" ]; then
  104. show_help
  105. exit 1
  106. else
  107. "${LIBDIR}/bkctld-${subcommand}" "${jail_name}"
  108. fi
  109. ;;
  110. "status")
  111. jail_name="${2:-}"
  112. if [ "${jail_name}" = "all" ] || [ -z "${jail_name}" ]; then
  113. for jail in $("${LIBDIR}/bkctld-list"); do
  114. "${LIBDIR}/bkctld-${subcommand}" "${jail}"
  115. done
  116. else
  117. "${LIBDIR}/bkctld-${subcommand}" "${jail_name}"
  118. fi
  119. ;;
  120. *)
  121. show_help
  122. exit 1
  123. ;;
  124. esac