|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | 3 | # ================================================================= |
4 | | -# Restic Backup Script v0.40 - 2025.11.18 |
| 4 | +# Restic Backup Script v0.41 - 2025.11.24 |
5 | 5 | # ================================================================= |
6 | 6 |
|
7 | 7 | export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
8 | 8 | set -euo pipefail |
9 | 9 | umask 077 |
10 | 10 |
|
11 | 11 | # --- Script Constants --- |
12 | | -SCRIPT_VERSION="0.40" |
| 12 | +SCRIPT_VERSION="0.41" |
13 | 13 | SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd) |
14 | 14 | PROG_NAME=$(basename "$0"); readonly PROG_NAME |
15 | 15 | CONFIG_FILE="${SCRIPT_DIR}/restic-backup.conf" |
@@ -76,7 +76,7 @@ import_restic_key() { |
76 | 76 | fi |
77 | 77 | done |
78 | 78 |
|
79 | | - # 4. Check Debian/Ubuntu system keyring (Fallback for apt-installed systems) |
| 79 | + # 4. Check System Keyring (Distro specific) |
80 | 80 | debian_keyring="/usr/share/keyrings/restic-archive-keyring.gpg" |
81 | 81 | if [[ -f "$debian_keyring" ]]; then |
82 | 82 | echo "Checking system keyring..." |
@@ -111,9 +111,9 @@ check_and_install_restic() { |
111 | 111 | echo -e "${C_BOLD}--- Checking Restic Version ---${C_RESET}" |
112 | 112 | if ! command -v less &>/dev/null || ! command -v bzip2 &>/dev/null || ! command -v curl &>/dev/null || ! command -v gpg &>/dev/null || ! command -v jq &>/dev/null; then |
113 | 113 | echo |
114 | | - echo -e "${C_RED}ERROR: 'less', 'bzip2', 'curl', 'gpg', and 'jq' are required for secure auto-installation.${C_RESET}" >&2 |
| 114 | + echo -e "${C_RED}ERROR: Missing dependencies.${C_RESET}" >&2 |
115 | 115 | echo |
116 | | - echo -e "${C_YELLOW}On Debian based systems install with: sudo apt-get install less bzip2 curl gnupg jq${C_RESET}" >&2 |
| 116 | + echo -e "${C_YELLOW}Install with: ${MISSING_PKG_HINT}${C_RESET}" >&2 |
117 | 117 | echo |
118 | 118 | exit 1 |
119 | 119 | fi |
@@ -194,7 +194,11 @@ check_and_install_restic() { |
194 | 194 | echo "Decompressing and installing to /usr/local/bin/restic..." |
195 | 195 | if bunzip2 -c "$temp_binary" > /usr/local/bin/restic.tmp; then |
196 | 196 | chmod +x /usr/local/bin/restic.tmp |
197 | | - mv /usr/local/bin/restic.tmp /usr/local/bin/restic |
| 197 | + mv /usr/local/bin/restic.tmp /usr/local/bin/restic |
| 198 | + if [[ "$IS_SELINUX_DISTRO" == "true" ]] && command -v restorecon &>/dev/null; then |
| 199 | + echo "Applying SELinux context to binary..." |
| 200 | + restorecon -v /usr/local/bin/restic || true |
| 201 | + fi |
198 | 202 | echo -e "${C_GREEN}✅ Restic version $latest_version installed successfully.${C_RESET}" |
199 | 203 | else |
200 | 204 | echo -e "${C_RED}Installation failed.${C_RESET}" >&2 |
@@ -348,6 +352,48 @@ display_help() { |
348 | 352 | echo |
349 | 353 | } |
350 | 354 |
|
| 355 | +detect_distro() { |
| 356 | + if [ -f /etc/os-release ]; then |
| 357 | + # shellcheck source=/dev/null |
| 358 | + . /etc/os-release |
| 359 | + OS_NAME=$ID |
| 360 | + else |
| 361 | + OS_NAME=$(uname -s) |
| 362 | + fi |
| 363 | + case "$OS_NAME" in |
| 364 | + fedora|rhel|centos|almalinux|rocky|amzn) |
| 365 | + # RHEL / Fedora based |
| 366 | + MISSING_PKG_HINT="sudo dnf install restic curl gnupg bzip2 less jq util-linux" |
| 367 | + IS_SELINUX_DISTRO=true |
| 368 | + ;; |
| 369 | + debian|ubuntu|pop|mint|kali|raspbian|elementary) |
| 370 | + # Debian / Ubuntu based |
| 371 | + MISSING_PKG_HINT="sudo apt-get install restic curl gnupg bzip2 less jq" |
| 372 | + IS_SELINUX_DISTRO=false |
| 373 | + ;; |
| 374 | + arch|manjaro|endeavouros|garuda) |
| 375 | + # Arch based ( # i don't use arch btw :) |
| 376 | + MISSING_PKG_HINT="sudo pacman -S restic curl gnupg bzip2 less jq" |
| 377 | + IS_SELINUX_DISTRO=false |
| 378 | + ;; |
| 379 | + opensuse*|sles) |
| 380 | + # OpenSUSE |
| 381 | + MISSING_PKG_HINT="sudo zypper install restic curl gpg2 bzip2 less jq" |
| 382 | + IS_SELINUX_DISTRO=false |
| 383 | + ;; |
| 384 | + alpine) |
| 385 | + # Alpine |
| 386 | + MISSING_PKG_HINT="sudo apk add restic curl gnupg bzip2 less jq util-linux # (Ensure community repo is enabled)" |
| 387 | + IS_SELINUX_DISTRO=false |
| 388 | + ;; |
| 389 | + *) |
| 390 | + # Fallback for unknown systems |
| 391 | + MISSING_PKG_HINT="Please install manually: restic curl gnupg bzip2 less jq util-linux" |
| 392 | + IS_SELINUX_DISTRO=false |
| 393 | + ;; |
| 394 | + esac |
| 395 | +} |
| 396 | + |
351 | 397 | log_message() { |
352 | 398 | local message="$1" |
353 | 399 | local timestamp |
@@ -791,13 +837,7 @@ run_preflight_checks() { |
791 | 837 | local required_cmds=(restic curl flock jq less gpg bzip2) |
792 | 838 | for cmd in "${required_cmds[@]}"; do |
793 | 839 | if ! command -v "$cmd" &>/dev/null; then |
794 | | - local install_hint="On Debian-based systems, try: sudo apt install $cmd" |
795 | | - case "$cmd" in |
796 | | - gpg) install_hint="On Debian-based systems, try: sudo apt install gnupg";; |
797 | | - bzip2) install_hint="On Debian-based systems, try: sudo apt install bzip2";; |
798 | | - less) install_hint="On Debian-based systems, try: sudo apt install less";; |
799 | | - esac |
800 | | - handle_failure "Required command '$cmd' not found. $install_hint" "10" |
| 840 | + handle_failure "Required command '$cmd' not found. Try: $MISSING_PKG_HINT" "10" |
801 | 841 | fi |
802 | 842 | done |
803 | 843 | if [[ "$verbosity" == "verbose" ]]; then echo -e "[${C_GREEN} OK ${C_RESET}]"; fi |
|
1722 | 1762 | LOCK_FD=200 |
1723 | 1763 |
|
1724 | 1764 | # 4. After lock, it's safe to run updates. |
| 1765 | +detect_distro |
1725 | 1766 | check_for_script_update |
1726 | 1767 | check_and_install_restic |
1727 | 1768 |
|
|
0 commit comments