Files
Homelab/up.sh
2026-02-04 22:12:47 +01:00

189 lines
5.1 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
set -e
# --- Colors and Formatting ---
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} $*${RESET}"; }
success() { echo -e "${GREEN}$*${RESET}"; }
warn() { echo -e "${YELLOW}$*${RESET}"; }
error() { echo -e "${RED}$*${RESET}"; }
# --- Detect Package Manager ---
info "Detecting package manager..."
if command -v nala &>/dev/null; then
PM="nala"
elif command -v apt &>/dev/null; then
PM="apt"
elif command -v dnf &>/dev/null; then
PM="dnf"
elif command -v yum &>/dev/null; then
PM="yum"
elif command -v pacman &>/dev/null; then
PM="pacman"
elif command -v zypper &>/dev/null; then
PM="zypper"
else
error "No supported package manager found (apt/nala/dnf/yum/pacman/zypper)."
exit 1
fi
success "Detected: $PM"
# --- System Update and Cleanup ---
info "Starting system update and maintenance..."
# Update and upgrade system
case "$PM" in
nala)
info "Updating package list and upgrading system (nala)..."
sudo nala update
sudo nala upgrade -y
sudo nala autoremove -y --purge
sudo nala clean
;;
apt)
info "Updating package list and upgrading system (apt)..."
sudo apt update
sudo apt upgrade -y
sudo apt autoremove -y --purge
sudo apt clean
;;
dnf)
info "Updating and cleaning system (dnf)..."
sudo dnf upgrade --refresh -y
sudo dnf autoremove -y
sudo dnf clean all
;;
yum)
info "Updating and cleaning system (yum)..."
sudo yum update -y
sudo yum autoremove -y
sudo yum clean all
;;
pacman)
info "Updating and cleaning system (pacman)..."
sudo pacman -Syu --noconfirm
sudo pacman -Rns $(pacman -Qtdq) --noconfirm 2>/dev/null || true
sudo pacman -Sc --noconfirm
;;
zypper)
info "Updating and cleaning system (zypper)..."
sudo zypper refresh
sudo zypper update -y
sudo zypper clean --all
;;
esac
# --- Docker System Prune ---
if command -v docker &>/dev/null; then
info "Pruning Docker system..."
sudo docker system prune -a -f --volumes
success "Docker system pruned."
else
warn "Docker not installed. Skipping Docker prune."
fi
# --- Flatpak Update ---
if command -v flatpak &>/dev/null; then
info "Updating Flatpak packages..."
flatpak update -y
success "Flatpak packages updated."
else
warn "Flatpak not installed. Skipping Flatpak update."
fi
# --- Snap Update ---
if command -v snap &>/dev/null; then
info "Updating Snap packages..."
sudo snap refresh
success "Snap packages updated."
else
warn "Snap not installed. Skipping Snap update."
fi
# --- Clean Journal Logs ---
info "Cleaning journal logs (older than 7 days)..."
sudo journalctl --vacuum-time=7d
success "Journal logs cleaned."
# --- Clean Thumbnails Cache ---
info "Cleaning thumbnails cache..."
rm -rf ~/.cache/thumbnails/*
success "Thumbnails cache cleaned."
# --- Clean Temporary Files ---
info "Cleaning /tmp and user cache..."
sudo rm -rf /tmp/* ~/.cache/*
success "Temporary files and cache cleaned."
# --- Update Locate Database ---
info "Updating locate database..."
if command -v updatedb &>/dev/null; then
sudo updatedb
else
info "Installing mlocate/plocate for 'updatedb'..."
case "$PM" in
apt|nala)
sudo apt install -y plocate || sudo apt install -y mlocate
;;
dnf)
sudo dnf install -y plocate || sudo dnf install -y mlocate
;;
yum)
sudo yum install -y mlocate
;;
pacman)
sudo pacman -S --noconfirm mlocate
;;
zypper)
sudo zypper install -y mlocate
;;
esac
sudo updatedb
fi
success "Locate database updated."
# --- Remove Orphaned Packages (Pacman/DNF) ---
case "$PM" in
pacman)
info "Removing orphaned packages (pacman)..."
sudo pacman -Rns $(pacman -Qtdq) --noconfirm 2>/dev/null || true
;;
dnf)
info "Removing orphaned packages (dnf)..."
sudo dnf remove --unused -y
;;
esac
# --- Check if Restart is Recommended ---
REBOOT_REQUIRED=false
# Check for kernel updates (Debian/Ubuntu)
if [ -f /var/run/reboot-required ]; then
REBOOT_REQUIRED=true
fi
# Check for pending Docker daemon restart
if sudo systemctl is-active docker >/dev/null 2>&1 && [ -n "$(sudo docker info 2>/dev/null | grep 'Pending updates')" ]; then
REBOOT_REQUIRED=true
fi
# Check for other common restart triggers (e.g., glibc, systemd)
if [ -f /var/run/reboot-required.pkgs ]; then
REBOOT_REQUIRED=true
fi
# --- Final Message ---
echo -e "${GREEN}${BOLD}✅ System update and maintenance completed!${RESET}"
if [ "$REBOOT_REQUIRED" = true ]; then
warn "⚠ A system restart is recommended to apply all updates (e.g., kernel, Docker, or critical libraries)."
warn " This is especially important for cryptocurrency nodes, Proxmox, and UniFi services."
else
echo -e "${GREEN}✅ No restart required.${RESET}"
fi