#!/bin/bash # LXS - Update server # Description: Refresh package lists and upgrade installed packages # Author: LXS # Date: 2025 # Load LXS common library (colors, separator, run_spinner, loggers, helpers) LXS_RAW_BASE="${LXS_RAW_BASE:-https://git.hyko.cx/hykocx/lxs/raw/branch/main}" _lib=$(curl -fsSL "${LXS_RAW_BASE}/lib/common.sh") || { echo "Failed to fetch lib/common.sh" >&2; exit 1; } eval "$_lib" unset _lib export LXS_LOG_FILE="/tmp/lxs_update_server.log" require_root "$0" "$@" set -u # ═══════════════════════════════════════════════════════════════════════════ # Arguments # ═══════════════════════════════════════════════════════════════════════════ DO_FULL_UPGRADE=0 DO_AUTOREMOVE=1 DO_AUTOCLEAN=1 ASSUME_YES=0 for arg in "$@"; do case "$arg" in --full|--dist-upgrade) DO_FULL_UPGRADE=1 ;; --no-autoremove) DO_AUTOREMOVE=0 ;; --no-autoclean) DO_AUTOCLEAN=0 ;; -y|--yes) ASSUME_YES=1 ;; -h|--help) cat <&2 exit 1 ;; esac done # ═══════════════════════════════════════════════════════════════════════════ # Pre-checks # ═══════════════════════════════════════════════════════════════════════════ require_debian_ubuntu || exit 1 if ! command -v apt-get >/dev/null 2>&1; then err "apt-get is not available on this system" exit 1 fi UPGRADE_CMD="upgrade" UPGRADE_LABEL="Upgrade installed packages" if [ $DO_FULL_UPGRADE -eq 1 ]; then UPGRADE_CMD="full-upgrade" UPGRADE_LABEL="Full-upgrade (may add/remove packages)" fi echo -e "${WHITE}${BOLD}LXS Server Update${NC}" show_separator echo "The following actions will be performed:" echo " • Refresh package lists (apt-get update)" echo " • ${UPGRADE_LABEL}" [ $DO_AUTOREMOVE -eq 1 ] && echo " • Remove unused packages (apt-get autoremove)" [ $DO_AUTOCLEAN -eq 1 ] && echo " • Clean old package archives (apt-get autoclean)" show_separator if [ $ASSUME_YES -ne 1 ]; then echo -e -n "${BOLD}Proceed? [y/N]: ${NC}" read -r reply case "$reply" in [yY]|[yY][eE][sS]) ;; *) info "Cancelled."; exit 0 ;; esac fi # ═══════════════════════════════════════════════════════════════════════════ # Run # ═══════════════════════════════════════════════════════════════════════════ apt_noninteractive wait_for_apt || exit 1 APT_OPTS='-y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"' run_spinner "Refreshing package lists..." "apt-get update" || { err "apt-get update failed — see ${LXS_LOG_FILE}" exit 1 } run_spinner "${UPGRADE_LABEL}..." "apt-get ${APT_OPTS} ${UPGRADE_CMD}" || { err "apt-get ${UPGRADE_CMD} failed — see ${LXS_LOG_FILE}" exit 1 } if [ $DO_AUTOREMOVE -eq 1 ]; then run_spinner "Removing unused packages..." "apt-get ${APT_OPTS} autoremove --purge" \ || warn "autoremove failed — see ${LXS_LOG_FILE}" fi if [ $DO_AUTOCLEAN -eq 1 ]; then run_spinner "Cleaning old archives..." "apt-get ${APT_OPTS} autoclean" \ || warn "autoclean failed — see ${LXS_LOG_FILE}" fi show_separator ok "Server updated" # ═══════════════════════════════════════════════════════════════════════════ # Reboot hint # ═══════════════════════════════════════════════════════════════════════════ if [ -f /var/run/reboot-required ]; then echo "" warn "A reboot is required to complete the update." if [ -f /var/run/reboot-required.pkgs ]; then echo -e "${GRAY}Packages requiring reboot:${NC}" sed 's/^/ • /' /var/run/reboot-required.pkgs fi if [ -t 0 ]; then echo "" echo -e -n "${BOLD}Reboot now? [y/N]: ${NC}" read -r reboot_reply case "$reboot_reply" in [yY]|[yY][eE][sS]) info "Rebooting..." systemctl reboot ;; *) info "Reboot skipped. Remember to reboot later." ;; esac fi fi