Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 54 additions & 13 deletions restic-backup.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/usr/bin/env bash

# =================================================================
# Restic Backup Script v0.40 - 2025.11.18
# Restic Backup Script v0.41 - 2025.11.24
# =================================================================

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
set -euo pipefail
umask 077

# --- Script Constants ---
SCRIPT_VERSION="0.40"
SCRIPT_VERSION="0.41"
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
PROG_NAME=$(basename "$0"); readonly PROG_NAME
CONFIG_FILE="${SCRIPT_DIR}/restic-backup.conf"
Expand Down Expand Up @@ -76,7 +76,7 @@ import_restic_key() {
fi
done

# 4. Check Debian/Ubuntu system keyring (Fallback for apt-installed systems)
# 4. Check System Keyring (Distro specific)
debian_keyring="/usr/share/keyrings/restic-archive-keyring.gpg"
if [[ -f "$debian_keyring" ]]; then
echo "Checking system keyring..."
Expand Down Expand Up @@ -111,9 +111,9 @@ check_and_install_restic() {
echo -e "${C_BOLD}--- Checking Restic Version ---${C_RESET}"
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
echo
echo -e "${C_RED}ERROR: 'less', 'bzip2', 'curl', 'gpg', and 'jq' are required for secure auto-installation.${C_RESET}" >&2
echo -e "${C_RED}ERROR: Missing dependencies.${C_RESET}" >&2
echo
echo -e "${C_YELLOW}On Debian based systems install with: sudo apt-get install less bzip2 curl gnupg jq${C_RESET}" >&2
echo -e "${C_YELLOW}Install with: ${MISSING_PKG_HINT}${C_RESET}" >&2
echo
exit 1
fi
Expand Down Expand Up @@ -194,7 +194,11 @@ check_and_install_restic() {
echo "Decompressing and installing to /usr/local/bin/restic..."
if bunzip2 -c "$temp_binary" > /usr/local/bin/restic.tmp; then
chmod +x /usr/local/bin/restic.tmp
mv /usr/local/bin/restic.tmp /usr/local/bin/restic
mv /usr/local/bin/restic.tmp /usr/local/bin/restic
if [[ "$IS_SELINUX_DISTRO" == "true" ]] && command -v restorecon &>/dev/null; then
echo "Applying SELinux context to binary..."
restorecon -v /usr/local/bin/restic || true
fi
echo -e "${C_GREEN}✅ Restic version $latest_version installed successfully.${C_RESET}"
else
echo -e "${C_RED}Installation failed.${C_RESET}" >&2
Expand Down Expand Up @@ -348,6 +352,48 @@ display_help() {
echo
}

detect_distro() {
if [ -f /etc/os-release ]; then
# shellcheck source=/dev/null
. /etc/os-release
OS_NAME=$ID
else
OS_NAME=$(uname -s)
fi
case "$OS_NAME" in
fedora|rhel|centos|almalinux|rocky|amzn)
# RHEL / Fedora based
MISSING_PKG_HINT="sudo dnf install restic curl gnupg bzip2 less jq util-linux"
IS_SELINUX_DISTRO=true
;;
debian|ubuntu|pop|mint|kali|raspbian|elementary)
# Debian / Ubuntu based
MISSING_PKG_HINT="sudo apt-get install restic curl gnupg bzip2 less jq"
IS_SELINUX_DISTRO=false
;;
arch|manjaro|endeavouros|garuda)
# Arch based ( # i don't use arch btw :)
MISSING_PKG_HINT="sudo pacman -S restic curl gnupg bzip2 less jq"
IS_SELINUX_DISTRO=false
;;
opensuse*|sles)
# OpenSUSE
MISSING_PKG_HINT="sudo zypper install restic curl gpg2 bzip2 less jq"
IS_SELINUX_DISTRO=false
;;
alpine)
# Alpine
MISSING_PKG_HINT="sudo apk add restic curl gnupg bzip2 less jq util-linux # (Ensure community repo is enabled)"
IS_SELINUX_DISTRO=false
;;
*)
# Fallback for unknown systems
MISSING_PKG_HINT="Please install manually: restic curl gnupg bzip2 less jq util-linux"
IS_SELINUX_DISTRO=false
;;
esac
}

log_message() {
local message="$1"
local timestamp
Expand Down Expand Up @@ -791,13 +837,7 @@ run_preflight_checks() {
local required_cmds=(restic curl flock jq less gpg bzip2)
for cmd in "${required_cmds[@]}"; do
if ! command -v "$cmd" &>/dev/null; then
local install_hint="On Debian-based systems, try: sudo apt install $cmd"
case "$cmd" in
gpg) install_hint="On Debian-based systems, try: sudo apt install gnupg";;
bzip2) install_hint="On Debian-based systems, try: sudo apt install bzip2";;
less) install_hint="On Debian-based systems, try: sudo apt install less";;
esac
handle_failure "Required command '$cmd' not found. $install_hint" "10"
handle_failure "Required command '$cmd' not found. Try: $MISSING_PKG_HINT" "10"
fi
done
if [[ "$verbosity" == "verbose" ]]; then echo -e "[${C_GREEN} OK ${C_RESET}]"; fi
Expand Down Expand Up @@ -1722,6 +1762,7 @@ fi
LOCK_FD=200

# 4. After lock, it's safe to run updates.
detect_distro
check_for_script_update
check_and_install_restic

Expand Down
2 changes: 1 addition & 1 deletion restic-backup.sh.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7a80f69fd6b2c4f9c4073abbe1f75504f69345634190a43cd4400975880fba15 restic-backup.sh
13de8cc2e5874e419ddc4a10f80ab56b76c5d4a6ba28af3b05fe79e6cf51173e restic-backup.sh
Loading