Created
May 2, 2024 19:52
-
-
Save earentir/a300653d7e48cf07439cf0d3d57740df to your computer and use it in GitHub Desktop.
upgradeFedora.sh
This file contains 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 | |
# Make sure DNF is installed | |
if ! command -v dnf &> /dev/null; then | |
echo "dnf could not be found. Please install dnf first." | |
exit | |
fi | |
# Determine if we are in the middle of an upgrade | |
UPGRADE_IN_PROGRESS=false | |
LOGFILE=$(ls -t $HOME/fedoraUpgrade-*.log 2>/dev/null | head -n 1) | |
if [[ -f $LOGFILE ]]; then | |
LAST_STEP=$(tail -n 1 $LOGFILE) | |
if [[ $LAST_STEP != *"Done" ]]; then | |
UPGRADE_IN_PROGRESS=true | |
# Fetch the release version from the last log file name | |
LOGFILE_RELEASE_VER=$(basename $LOGFILE | grep -oP '(?<=fedoraUpgrade-)\d+') | |
RELEASE_VER=$LOGFILE_RELEASE_VER | |
fi | |
fi | |
if [[ $UPGRADE_IN_PROGRESS == false ]]; then | |
CURRENT_VER=$(awk '{print $3}' /etc/redhat-release) | |
if [[ ! $CURRENT_VER =~ ^[0-9]+$ ]]; then | |
echo "Invalid Fedora version number: $CURRENT_VER" | |
exit 1 | |
fi | |
RELEASE_VER=$(($CURRENT_VER + 1)) | |
# Define log file | |
LOGFILE="$HOME/fedoraUpgrade-$RELEASE_VER.log" | |
else | |
# Fetch current version from /etc/redhat-release | |
CURRENT_VER=$(awk '{print $3}' /etc/redhat-release) | |
fi | |
echo "Current Fedora version: $CURRENT_VER" | |
echo "Next Fedora version: $RELEASE_VER" | |
# Define upgrade steps | |
declare -A STEPS | |
STEPS=( | |
["Upgrade"]="dnf upgrade --refresh -y" | |
["Install Plugin"]="dnf install dnf-plugin-system-upgrade -y" | |
["Download System Upgrade"]="dnf system-upgrade download --refresh --releasever=$RELEASE_VER -y" | |
["Reboot"]="dnf system-upgrade reboot" | |
["Clean System Upgrade"]="dnf system-upgrade clean" | |
["Clean Packages"]="dnf clean packages" | |
) | |
# Check if step was completed | |
function step_completed() { | |
local step=$1 | |
grep -q "fedoraUpgrade->$step" "$LOGFILE" && grep -q "fedoraUpgrade->$step Done" "$LOGFILE" | |
} | |
# Run upgrade steps | |
for step in "${!STEPS[@]}"; do | |
command=${STEPS[$step]} | |
if [[ -f $LOGFILE && $(step_completed $step) == true ]]; then | |
echo "Step '$step' already completed, skipping." | |
continue | |
fi | |
echo "fedoraUpgrade->$step" >> $LOGFILE | |
if [[ $step == "Reboot" ]]; then | |
read -p "Are you ready to reboot? (y/n) " -n 1 -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
REBOOT_TIMESTAMP=$(date +%s) | |
echo "RebootStartedOn $REBOOT_TIMESTAMP" >> $LOGFILE | |
eval $command | |
exit | |
else | |
echo "Skipping reboot." | |
continue | |
fi | |
else | |
eval $command | |
echo "fedoraUpgrade->$step Done" >> $LOGFILE | |
fi | |
done | |
if grep -q "RebootStartedOn" "$LOGFILE"; then | |
REBOOT_TIMESTAMP=$(grep "RebootStartedOn" "$LOGFILE" | awk '{print $2}') | |
CURRENT_TIMESTAMP=$(date +%s) | |
REBOOT_DURATION=$((CURRENT_TIMESTAMP - REBOOT_TIMESTAMP)) | |
echo "The reboot took $REBOOT_DURATION seconds." | |
fi | |
echo "Upgrade process completed successfully." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment