Files
ns8-backup-monitor/deploy/install.sh
T

221 lines
7.2 KiB
Bash
Raw Normal View History

#!/bin/bash
# =============================================================================
# ns8-backup-monitor - Installer / Uninstaller
# =============================================================================
# Usage:
# install.sh - install
# install.sh --uninstall - remove everything installed by this script
#
# Requires: root, python3, git
# Tested on: AlmaLinux 8/9, Rocky Linux 8/9 (NS8 supported distros)
# =============================================================================
set -euo pipefail
# --- constants ---------------------------------------------------------------
SERVICE_NAME="ns8-backup-monitor"
INSTALL_DIR="/opt/ns8-backup-monitor"
CONFIG_DIR="/etc/ns8-backup-monitor"
CONFIG_FILE="${CONFIG_DIR}/config.yml"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
REPO_URL="https://repo.lelekaos.com/admin/ns8-backup-monitor.git"
PYTHON=$(command -v python3 || true)
# --- colours -----------------------------------------------------------------
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'
BLUE='\033[0;34m'; BOLD='\033[1m'; RESET='\033[0m'
info() { echo -e "${BLUE}[INFO]${RESET} $*"; }
ok() { echo -e "${GREEN}[OK]${RESET} $*"; }
warn() { echo -e "${YELLOW}[WARN]${RESET} $*"; }
error() { echo -e "${RED}[ERROR]${RESET} $*" >&2; exit 1; }
# =============================================================================
# UNINSTALL
# =============================================================================
do_uninstall() {
echo -e "${BOLD}=== ns8-backup-monitor UNINSTALL ===${RESET}"
warn "This will stop and remove the service, code and config."
read -rp "Continue? [y/N] " confirm
[[ "$confirm" =~ ^[Yy]$ ]] || { info "Aborted."; exit 0; }
# Stop & disable service
if systemctl is-active --quiet "$SERVICE_NAME" 2>/dev/null; then
info "Stopping service..."
systemctl stop "$SERVICE_NAME"
fi
if systemctl is-enabled --quiet "$SERVICE_NAME" 2>/dev/null; then
info "Disabling service..."
systemctl disable "$SERVICE_NAME"
fi
# Remove service file
if [[ -f "$SERVICE_FILE" ]]; then
info "Removing systemd unit..."
rm -f "$SERVICE_FILE"
systemctl daemon-reload
ok "Systemd unit removed."
fi
# Remove install dir
if [[ -d "$INSTALL_DIR" ]]; then
info "Removing ${INSTALL_DIR}..."
rm -rf "$INSTALL_DIR"
ok "Install directory removed."
fi
# Ask about config
if [[ -d "$CONFIG_DIR" ]]; then
read -rp "Remove config directory ${CONFIG_DIR}? [y/N] " rmcfg
if [[ "$rmcfg" =~ ^[Yy]$ ]]; then
rm -rf "$CONFIG_DIR"
ok "Config directory removed."
else
warn "Config kept at ${CONFIG_DIR}."
fi
fi
echo -e "${GREEN}${BOLD}Uninstall complete.${RESET}"
}
# =============================================================================
# INSTALL
# =============================================================================
do_install() {
echo -e "${BOLD}=== ns8-backup-monitor INSTALLER ===${RESET}\n"
# --- pre-flight checks ---------------------------------------------------
[[ $EUID -eq 0 ]] || error "Run as root (or with sudo)."
[[ -n "$PYTHON" ]] || error "python3 not found."
command -v git &>/dev/null || error "git not found."
command -v ns8-sendmail &>/dev/null || \
warn "ns8-sendmail not found in PATH - make sure this runs on an NS8 node."
# --- interactive mail config ---------------------------------------------
echo -e "${BOLD}Mail configuration${RESET}"
echo -e "Email delivery uses ${BLUE}ns8-sendmail${RESET} (NS8 configured relay)."
echo
# mail from
local default_from="ns8-backup-monitor@$(hostname -f 2>/dev/null || echo localhost)"
read -rp "Sender address (From) [${default_from}]: " MAIL_FROM
MAIL_FROM="${MAIL_FROM:-$default_from}"
# mail to - loop until at least one address is entered
local MAIL_TO_LIST=()
echo "Recipient addresses (To) - enter one per line, empty line to finish:"
while true; do
read -rp " Recipient: " addr
[[ -z "$addr" ]] && break
MAIL_TO_LIST+=("$addr")
done
[[ ${#MAIL_TO_LIST[@]} -gt 0 ]] || error "At least one recipient is required."
# subject prefix
read -rp "Subject prefix [[NS8 Backup]]: " SUBJECT_PREFIX
SUBJECT_PREFIX="${SUBJECT_PREFIX:-[NS8 Backup]}"
echo
info "From: $MAIL_FROM"
info "To: ${MAIL_TO_LIST[*]}"
info "Prefix: $SUBJECT_PREFIX"
echo
read -rp "Confirm and proceed with install? [Y/n] " go
[[ "$go" =~ ^[Nn]$ ]] && { info "Aborted."; exit 0; }
echo
# --- clone / update source -----------------------------------------------
if [[ -d "${INSTALL_DIR}/.git" ]]; then
info "Updating existing clone in ${INSTALL_DIR}..."
git -C "$INSTALL_DIR" pull --ff-only
else
info "Cloning into ${INSTALL_DIR}..."
git clone "$REPO_URL" "$INSTALL_DIR"
fi
ok "Source ready."
# --- config directory ----------------------------------------------------
mkdir -p "$CONFIG_DIR"
chmod 750 "$CONFIG_DIR"
if [[ -f "$CONFIG_FILE" ]]; then
warn "Config file ${CONFIG_FILE} already exists - keeping it."
warn "Edit it manually if you need to change mail settings."
else
info "Writing ${CONFIG_FILE}..."
# Build YAML 'to' list
local to_yaml=""
for addr in "${MAIL_TO_LIST[@]}"; do
to_yaml+=" - \"${addr}\"\n"
done
cat > "$CONFIG_FILE" <<EOF
# ns8-backup-monitor configuration
# Generated by install.sh on $(date -u '+%Y-%m-%d %H:%M UTC')
# Email delivery is handled by ns8-sendmail (NS8 configured relay).
mail:
from: "${MAIL_FROM}"
to:
$(printf '%b' "$to_yaml")
subject_prefix: "${SUBJECT_PREFIX}"
receiver:
host: "127.0.0.1"
port: 9099
correlator:
wait_seconds: 30
recent_window: 3600
redis:
socket: "/var/lib/nethserver/cluster/state/redis.sock"
repo_check:
timeout: 60
restic_flags: ""
logging:
level: INFO
file: "/var/log/ns8-backup-monitor.log"
EOF
chmod 640 "$CONFIG_FILE"
ok "Config written."
fi
# --- systemd unit --------------------------------------------------------
info "Installing systemd unit..."
cp "${INSTALL_DIR}/deploy/ns8-backup-monitor.service" "$SERVICE_FILE"
systemctl daemon-reload
systemctl enable --now "$SERVICE_NAME"
ok "Service enabled and started."
# --- done ----------------------------------------------------------------
echo
echo -e "${GREEN}${BOLD}Installation complete.${RESET}"
echo -e " Config: ${CONFIG_FILE}"
echo -e " Logs: journalctl -u ${SERVICE_NAME} -f"
echo -e " Status: systemctl status ${SERVICE_NAME}"
echo
echo -e "To uninstall: ${BOLD}bash ${INSTALL_DIR}/deploy/install.sh --uninstall${RESET}"
}
# =============================================================================
# ENTRYPOINT
# =============================================================================
case "${1:-}" in
--uninstall|-u|uninstall)
do_uninstall
;;
--help|-h)
echo "Usage: $0 [--uninstall]"
exit 0
;;
"")
do_install
;;
*)
error "Unknown argument: $1. Use --uninstall to remove."
;;
esac