Created
January 23, 2026 08:17
-
-
Save commandline-johnny/a76c305ea2ed1305f1823e301f3d5f68 to your computer and use it in GitHub Desktop.
Debian 12 to 13 upgrade script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -e | |
| # Debian 12 to 13 Upgrade Script | |
| # Run with: sudo bash upgrade-debian.sh | |
| # | |
| # Provided as-is, no warranty implied. | |
| # Color codes for output | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| NC='\033[0m' # No Color | |
| # Check if running as root | |
| if [[ $EUID -ne 0 ]]; then | |
| echo -e "${RED}Error: This script must be run as root${NC}" | |
| exit 1 | |
| fi | |
| # Check current Debian version | |
| current_version=$(cat /etc/debian_version) | |
| echo -e "${GREEN}Current Debian version: $current_version${NC}" | |
| # Confirmation prompt | |
| echo -e "${YELLOW}WARNING: This will upgrade your system from Debian 12 to Debian 13${NC}" | |
| echo -e "${YELLOW}Make sure you have backups of important data!${NC}" | |
| read -p "Do you want to continue? (yes/no): " confirm | |
| if [[ ! $confirm =~ ^[Yy](es)?$ ]]; then | |
| echo "Upgrade cancelled." | |
| exit 0 | |
| fi | |
| # Log file | |
| LOG_FILE="/var/log/debian-upgrade-$(date +%Y%m%d-%H%M%S).log" | |
| echo "Logging to: $LOG_FILE" | |
| # Function to log and display | |
| log_and_echo() { | |
| echo -e "$1" | tee -a "$LOG_FILE" | |
| } | |
| log_and_echo "${GREEN}Starting upgrade process...${NC}" | |
| # Step 1: Update current system | |
| log_and_echo "${GREEN}Step 1: Updating current Debian 12 system...${NC}" | |
| apt update 2>&1 | tee -a "$LOG_FILE" | |
| DEBIAN_FRONTEND=noninteractive apt upgrade -y 2>&1 | tee -a "$LOG_FILE" | |
| DEBIAN_FRONTEND=noninteractive apt full-upgrade -y 2>&1 | tee -a "$LOG_FILE" | |
| # Step 2: Create comprehensive backup | |
| BACKUP_DIR="/root/debian-upgrade-backup-$(date +%Y%m%d-%H%M%S)" | |
| log_and_echo "${GREEN}Step 2: Creating backup in $BACKUP_DIR...${NC}" | |
| mkdir -p "$BACKUP_DIR" | |
| # Backup sources.list files | |
| log_and_echo "Backing up APT sources..." | |
| cp /etc/apt/sources.list "$BACKUP_DIR/sources.list" | |
| if [ -d /etc/apt/sources.list.d ]; then | |
| cp -r /etc/apt/sources.list.d "$BACKUP_DIR/" | |
| fi | |
| # Backup package list | |
| log_and_echo "Backing up installed package list..." | |
| dpkg --get-selections > "$BACKUP_DIR/package-selections.txt" | |
| apt-mark showmanual > "$BACKUP_DIR/manual-packages.txt" | |
| # Backup important config directories | |
| log_and_echo "Backing up configuration files..." | |
| tar -czf "$BACKUP_DIR/etc-backup.tar.gz" /etc/ 2>/dev/null || true | |
| # Create system info snapshot | |
| log_and_echo "Creating system info snapshot..." | |
| { | |
| echo "=== System Information ===" | |
| uname -a | |
| echo "" | |
| echo "=== Debian Version ===" | |
| cat /etc/debian_version | |
| echo "" | |
| echo "=== Disk Usage ===" | |
| df -h | |
| echo "" | |
| echo "=== Memory ===" | |
| free -h | |
| } > "$BACKUP_DIR/system-info.txt" | |
| log_and_echo "${GREEN}Backup completed in: $BACKUP_DIR${NC}" | |
| # Step 3: Update sources.list to Debian 13 (trixie) | |
| log_and_echo "${GREEN}Step 3: Updating repository sources to Debian 13 (trixie)...${NC}" | |
| sed -i 's/bookworm/trixie/g' /etc/apt/sources.list | |
| if [ -d /etc/apt/sources.list.d ]; then | |
| sed -i 's/bookworm/trixie/g' /etc/apt/sources.list.d/*.list 2>/dev/null || true | |
| fi | |
| # Step 4: Update package lists | |
| log_and_echo "${GREEN}Step 4: Updating package lists...${NC}" | |
| apt update 2>&1 | tee -a "$LOG_FILE" | |
| # Step 5: Perform the upgrade | |
| log_and_echo "${GREEN}Step 5: Performing distribution upgrade...${NC}" | |
| DEBIAN_FRONTEND=noninteractive apt upgrade -y 2>&1 | tee -a "$LOG_FILE" | |
| DEBIAN_FRONTEND=noninteractive apt full-upgrade -y 2>&1 | tee -a "$LOG_FILE" | |
| # Step 6: Clean up | |
| log_and_echo "${GREEN}Step 6: Cleaning up...${NC}" | |
| apt autoremove -y 2>&1 | tee -a "$LOG_FILE" | |
| apt autoclean 2>&1 | tee -a "$LOG_FILE" | |
| # Check new version | |
| new_version=$(cat /etc/debian_version) | |
| log_and_echo "${GREEN}Upgrade complete!${NC}" | |
| log_and_echo "${GREEN}New Debian version: $new_version${NC}" | |
| log_and_echo "${YELLOW}A reboot is required. Reboot now? (yes/no):${NC}" | |
| read -p "" reboot_confirm | |
| if [[ $reboot_confirm == "yes" ]]; then | |
| log_and_echo "${GREEN}Rebooting system...${NC}" | |
| reboot | |
| else | |
| log_and_echo "${YELLOW}Please reboot manually when ready.${NC}" | |
| log_and_echo "${YELLOW}Run: sudo reboot${NC}" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment