scripts/bash/debian-major.sh
2025-04-14 09:22:15 +02:00

157 lines
5 KiB
Bash

#!/bin/sh
# ╭───────────────╮
# │ env functions │
# ╰───────────────╯
# ───────────────────────────────────< ANSI color codes >───────────────────────────────────
RED='\033[0;31m'
CYAN='\033[0;36m'
YELLOW='\033[0;33m'
LIGHT_GREEN='\033[0;92m'
BOLD='\033[1m'
NC='\033[0m' # No Color
echo_error() {
printf "${BOLD}${RED}ERROR: ${NC}${RED}%s${NC}\n" "$1" >&2
}
echo_info() {
printf "${BOLD}${CYAN}INFO: ${NC}${CYAN}%s${NC}\n" "$1"
}
echo_warning() {
printf "${BOLD}${YELLOW}WARNING: ${NC}${YELLOW}%s${NC}\n" "$1"
}
echo_note() {
printf "${BOLD}${LIGHT_GREEN}NOTE: ${NC}${LIGHT_GREEN}%s${NC}\n" "$1"
}
command_exists() {
command -v "$@" >/dev/null 2>&1
}
# ─────────────< Check if the user is root and set sudo variable if necessary >─────────────
check_root() {
if [ "$(id -u)" -ne 0 ]; then
if command_exists sudo; then
echo_info "User is not root. Using sudo for privileged operations."
_sudo="sudo"
else
echo_error "No sudo found and you're not root! Can't install packages."
return 1
fi
else
echo_info "Root access confirmed."
_sudo=""
fi
}
# ──────────────────────< Check if the given command exists silently >──────────────────────
command_exists() {
command -v "$@" >/dev/null 2>&1
}
# ╭────────────────────────────────────╮
# │ insert your scripts/functions here │
# ╰────────────────────────────────────╯
full_update_and_upgrade() {
if command_exists apt-get; then
echo_info "Updating sources.."
$_sudo apt-get update || echo_error "Something went wrong, please check your connection and permissions!"
$_sudo apt-get upgrade --assume-yes || echo_error "Something went wrong, please check your connection and permissions!"
$_sudo apt-get full-upgrade --assume-yes || echo_error "Something went wrong, please check your connection and permissions!"
else
echo_error "The OS is not suitable for this script!"
fi
}
clean_and_full_upgrade() {
if command_exists apt-get; then
echo_info "Cleaning the environment.."
$_sudo apt-get clean --assume-yes
full_update_and_upgrade
else
echo_error "The OS is not suitable for this script!"
fi
}
post_clean() {
if command_exists apt-get; then
$_sudo apt-get autoremove --assume-yes
else
echo_error "The OS is not suitable for this script!"
fi
}
detect_version() {
. /etc/os-release
case "$VERSION_CODENAME" in
bookworm)
cur_os="bookworm"
tar_os="trixie"
;;
buster)
cur_os="buster"
tar_os="bullseye"
;;
bullseye)
cur_os="bullseye"
tar_os="bookworm"
;;
*)
echo_error "$VERSION_CODENAME is either not a debian version, or just simply not defined"
return 1
;;
esac
}
update_sources() {
# Create backup directory if it doesn't exist
BACKUP_DIR="/var/backups/apt-sources"
echo_info "Creating backup directory at $BACKUP_DIR..."
$_sudo mkdir -p "$BACKUP_DIR"
echo_info "Backing up current sources lists..."
$_sudo cp /etc/apt/sources.list "$BACKUP_DIR/sources.list.backup.$(date +%Y%m%d)"
echo_info "Updating sources from Bookworm to Trixie..."
# Replace bookworm with trixie in main sources.list
$_sudo sed -i "s/$cur_os/$tar_os/g" /etc/apt/sources.list
# Check and update any additional source files in sources.list.d
if [ -d "/etc/apt/sources.list.d" ]; then
for sourcefile in /etc/apt/sources.list.d/*.list; do
if [ -f "$sourcefile" ]; then
filename=$(basename "$sourcefile")
echo_info "Backing up and updating $sourcefile..."
$_sudo cp "$sourcefile" "$BACKUP_DIR/${filename}.backup.$(date +%Y%m%d)"
$_sudo sed -i "s/$cur_os/$tar_os/g" "$sourcefile"
fi
done
fi
echo_note "Sources have been updated to Trixie. Please run a full system update."
echo_warning "Make sure to review the changes and ensure all repositories are compatible with Trixie!"
echo_info "Backups stored in $BACKUP_DIR"
echo ""
__lsb_release__="$(lsb_release -a)"
echo_info "$__lsb_release__"
}
# ───────────────────────────────< main function to execute >───────────────────────────────
main() {
if check_root; then
full_update_and_upgrade
if detect_version; then
update_sources
fi
clean_and_full_upgrade
post_clean
else
echo_error "Something went terribly wrong!"
fi
}
main