#!/bin/sh # # bkctld is a shell script to create and manage a backup server which will # handle the backup of many servers (clients). # # Authors: Victor Laborie # Jérémy Lecour # Benoît Série # Gregory Colpart # Romain Dessort # Tristan Pilat # Licence: AGPLv3 # set -u if [ "$(id -u)" -ne 0 ]; then echo "You need to be root to run ${0} !" >&2 exit 1 fi basedir=$(dirname "$0") if [ "${basedir}" = "/usr/local/sbin" ] && [ -d "/usr/local/lib/bkctld" ]; then LIBDIR='/usr/local/lib/bkctld' elif [ "${basedir}" = "/usr/sbin" ] && [ -d "/usr/lib/bkctld" ]; then LIBDIR='/usr/lib/bkctld' elif [ -d './lib' ]; then LIBDIR='lib' else echo "Failed to find a suitable lib directory for bkctld." >&2 exit 1 fi # shellcheck source=lib/includes . "${LIBDIR}/includes" # Parse options # based on https://gist.github.com/deshion/10d3cb5f88a21671e17a if [ "$#" = "0" ]; then show_help exit 1 fi while :; do case $1 in -h|-\?|--help) show_help exit 0 ;; -V|--version) show_version exit 0 ;; -f|--force) FORCE=1 ;; *) # Default case: If no more options then break out of the loop. break ;; esac shift done subcommand="${1:-}" case "${subcommand}" in "inc" | "rm" | "check-jails" | "check-setup" | "stats" | "list") "${LIBDIR}/bkctld-${subcommand}" ;; "check") # backward compatibility "${LIBDIR}/bkctld-check-jails" ;; "check-incs") option="${2:-}" if [ "${option}" = "all" ] || [ -z "${option}" ]; then "${LIBDIR}/bkctld-check-incs" elif [ "${option}" = "last" ]; then "${LIBDIR}/bkctld-check-last-incs" else show_help exit 1 fi ;; "init" | "is-on") jail_name="${2:-}" if [ -z "${jail_name}" ]; then show_help exit 1 else "${LIBDIR}/bkctld-${subcommand}" "${jail_name}" fi ;; "key" | "port" | "ip") jail_name="${2:-}" option="${3:-}" if [ "${jail_name}" = "all" ] || [ -z "${jail_name}" ]; then show_help exit 1 else "${LIBDIR}/bkctld-${subcommand}" "${jail_name}" "${option}" fi ;; "start" | "stop" | "reload" | "restart" | "sync" | "update" | "remove" | "firewall" | "upgrade-config" | "archive") jail_name="${2:-}" if [ "${jail_name}" = "all" ]; then for jail in $("${LIBDIR}/bkctld-list"); do "${LIBDIR}/bkctld-${subcommand}" "${jail}" done elif [ -z "${jail_name}" ]; then show_help exit 1 else "${LIBDIR}/bkctld-${subcommand}" "${jail_name}" fi ;; "status") jail_name="${2:-}" if [ "${jail_name}" = "all" ] || [ -z "${jail_name}" ]; then for jail in $("${LIBDIR}/bkctld-list"); do "${LIBDIR}/bkctld-${subcommand}" "${jail}" done else "${LIBDIR}/bkctld-${subcommand}" "${jail_name}" fi ;; "inc-lock" | "inc-unlock") if [ "${subcommand}" = "inc-lock" ]; then lock_status=on elif [ "${subcommand}" = "inc-unlock" ]; then lock_status=off else show_help exit 1 fi pattern="${2:-}" if [ "${pattern}" = "all" ]; then target_path_pattern=$(incs_path "*/*") elif [ -n "${pattern}" ]; then # Check if pattern is a jail name if jails_with_incs_list | grep --silent --fixed-strings --line-regexp "${pattern}"; then debug "${pattern} is a jail, change pattern to ${pattern}/*" # then change attern to all subdirectories target_path_pattern=$(incs_path "${pattern}/*") else # or use it as is target_path_pattern=$(incs_path "${pattern}") fi else show_help exit 1 fi for target_path in ${target_path_pattern}; do "${LIBDIR}/bkctld-inc-lock" "${lock_status}" "${target_path}" done echo "finish" ;; "rename") jail_name="${2:-}" new_jail_name="${3:-}" if [ -z "${jail_name}" ] || [ -z "${new_jail_name}" ]; then show_help exit 1 else "${LIBDIR}/bkctld-${subcommand}" "${jail_name}" "${new_jail_name}" fi ;; *) show_help exit 1 ;; esac