Files
linux-backuper/packaging/install.sh
T
2026-10-09 20:22:51 +02:00

134 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# linux-backuper generic tarball installer
set -euo pipefail
PREFIX="${PREFIX:-/usr}"
BINDIR="${PREFIX}/bin"
LIBDIR="${PREFIX}/lib/linux-backuper"
CONFDIR="/etc/linux-backuper"
BACKUPDIR="/var/backups/linux-backuper"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_ROOT="${SCRIPT_DIR}/.."
if [[ ! -d "${SOURCE_ROOT}/linux_backuper" ]]; then
# In case run from inside extracted tarball where install.sh is at root
if [[ -d "${SCRIPT_DIR}/linux_backuper" ]]; then
SOURCE_ROOT="${SCRIPT_DIR}"
fi
fi
usage() {
cat <<EOF
Usage: $0 [OPTION]
Options:
--install Install linux-backuper to system (default)
--uninstall Remove linux-backuper from system
-h, --help Show this help message
EOF
}
check_python() {
if ! command -v python3 >/dev/null 2>&1; then
echo "[ERROR] python3 is required but not installed." >&2
exit 1
fi
PYTHON_VER="$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')"
PYTHON_MAJOR="$(echo "$PYTHON_VER" | cut -d. -f1)"
PYTHON_MINOR="$(echo "$PYTHON_VER" | cut -d. -f2)"
if [[ "$PYTHON_MAJOR" -lt 3 ]] || [[ "$PYTHON_MAJOR" -eq 3 && "$PYTHON_MINOR" -lt 6 ]]; then
echo "[ERROR] Python >= 3.6 required (found $PYTHON_VER)." >&2
exit 1
fi
}
do_install() {
if [[ $EUID -ne 0 ]]; then
echo "[ERROR] Installation requires root privileges. Please run with sudo." >&2
exit 1
fi
check_python
echo "[INFO] Installing linux-backuper..."
mkdir -p "${BINDIR}" "${LIBDIR}" "${CONFDIR}" "${BACKUPDIR}"
chmod 0700 "${BACKUPDIR}"
# Install library files
cp -r "${SOURCE_ROOT}/linux_backuper" "${LIBDIR}/"
find "${LIBDIR}" -type d -exec chmod 0755 {} +
find "${LIBDIR}" -type f -exec chmod 0644 {} +
# Install launcher
cp "${SOURCE_ROOT}/bin/linux-backuper" "${BINDIR}/linux-backuper"
chmod 0755 "${BINDIR}/linux-backuper"
# Install man page
if [[ -f "${SOURCE_ROOT}/man/linux-backuper.8" ]]; then
mkdir -p "${PREFIX}/share/man/man8"
cp "${SOURCE_ROOT}/man/linux-backuper.8" "${PREFIX}/share/man/man8/linux-backuper.8"
chmod 0644 "${PREFIX}/share/man/man8/linux-backuper.8"
fi
# Install completions
if [[ -f "${SOURCE_ROOT}/completions/bash/linux-backuper" ]]; then
mkdir -p "${PREFIX}/share/bash-completion/completions"
cp "${SOURCE_ROOT}/completions/bash/linux-backuper" "${PREFIX}/share/bash-completion/completions/linux-backuper"
chmod 0644 "${PREFIX}/share/bash-completion/completions/linux-backuper"
fi
if [[ -f "${SOURCE_ROOT}/completions/zsh/_linux-backuper" ]]; then
mkdir -p "${PREFIX}/share/zsh/site-functions"
cp "${SOURCE_ROOT}/completions/zsh/_linux-backuper" "${PREFIX}/share/zsh/site-functions/_linux-backuper"
chmod 0644 "${PREFIX}/share/zsh/site-functions/_linux-backuper"
fi
echo "[SUCCESS] linux-backuper installed successfully to ${BINDIR}/linux-backuper"
echo "[INFO] Testing installed binary:"
"${BINDIR}/linux-backuper" --version || true
}
do_uninstall() {
if [[ $EUID -ne 0 ]]; then
echo "[ERROR] Uninstallation requires root privileges. Please run with sudo." >&2
exit 1
fi
echo "[INFO] Uninstalling linux-backuper..."
rm -f "${BINDIR}/linux-backuper"
rm -rf "${LIBDIR}"
rm -f "${PREFIX}/share/man/man8/linux-backuper.8"
rm -f "${PREFIX}/share/bash-completion/completions/linux-backuper"
rm -f "${PREFIX}/share/zsh/site-functions/_linux-backuper"
echo "[SUCCESS] linux-backuper removed from system."
echo "[NOTE] Backup storage at ${BACKUPDIR} and configuration at ${CONFDIR} were preserved."
}
ACTION="install"
while [[ $# -gt 0 ]]; do
case "$1" in
--install)
ACTION="install"
shift
;;
--uninstall)
ACTION="uninstall"
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "[ERROR] Unknown option: $1" >&2
usage
exit 1
;;
esac
done
if [[ "$ACTION" == "install" ]]; then
do_install
elif [[ "$ACTION" == "uninstall" ]]; then
do_uninstall
fi