mirror of
https://codeberg.org/Tealk/systemupdate.git
synced 2024-12-29 10:27:05 +01:00
188 lines
4.6 KiB
Bash
188 lines
4.6 KiB
Bash
|
#!/bin/bash
|
||
|
mkdir -p ~/.cache/updates
|
||
|
|
||
|
##*===============================================
|
||
|
##* VARIABLE DECLARATION
|
||
|
##*===============================================
|
||
|
WORK_PATH=~/.cache/updates # Currently the log files are stored here
|
||
|
|
||
|
# Function: logging
|
||
|
# Purpose: Creates a log file for script output and manages log rotation
|
||
|
# Parameters: None
|
||
|
# Return value: None
|
||
|
function logging() {
|
||
|
# Check if the folder already exists
|
||
|
if [[ ! -d "${WORK_PATH}" ]]; then
|
||
|
mkdir -p "${WORK_PATH}"
|
||
|
fi
|
||
|
|
||
|
# Redirect all output to the log file
|
||
|
LOG_DATE=$(date +"%Y-%m-%d")
|
||
|
exec &> >(tee -a "${WORK_PATH}/updates_info-${LOG_DATE}.log")
|
||
|
|
||
|
# Delete old log files
|
||
|
find "${WORK_PATH}" -name "updates_info-*.log" -type f -mtime +"${NUM_DAYS_LOG}" -exec rm {} \;
|
||
|
}
|
||
|
|
||
|
# Function: time_start
|
||
|
# Purpose: Records the start time of the script execution
|
||
|
# Parameters: None
|
||
|
# Return value: None
|
||
|
function time_start() {
|
||
|
# Start date and time
|
||
|
START_TIME=$(date +"%Y-%m-%d %H:%M:%S")
|
||
|
|
||
|
# Command to write start date and time to the log file and overwrite the file
|
||
|
echo "Script started at ${START_TIME}"
|
||
|
}
|
||
|
|
||
|
# Function: time_end
|
||
|
# Purpose: Calculates the script execution duration and outputs end time
|
||
|
# Parameters: None
|
||
|
# Return value: None
|
||
|
function time_end() {
|
||
|
# End date and time
|
||
|
END_TIME=$(date +"%Y-%m-%d %H:%M:%S")
|
||
|
|
||
|
# Calculate duration in seconds
|
||
|
SECONDS=$(($(date -d "${END_TIME}" +%s) - $(date -d "${START_TIME}" +%s)))
|
||
|
|
||
|
# Extract hours, minutes, and seconds from the seconds
|
||
|
HOURS=$((SECONDS / 3600))
|
||
|
MINUTES=$(((SECONDS % 3600) / 60))
|
||
|
SECONDS=$((SECONDS % 60))
|
||
|
|
||
|
# Command to output end date, time, and duration
|
||
|
echo "Script finished at ${END_TIME} (Duration: ${HOURS} hours ${MINUTES} minutes ${SECONDS} seconds)"
|
||
|
}
|
||
|
|
||
|
# Function: update_arch
|
||
|
# Purpose: Updating Arch or distros based on it
|
||
|
# Parameters: None
|
||
|
# Return value: None
|
||
|
function update_arch() {
|
||
|
if command -v yay &> /dev/null; then
|
||
|
yay -Su
|
||
|
yay -Yc
|
||
|
elif command -v pamac &> /dev/null; then
|
||
|
sudo pamac update
|
||
|
sudo pamac upgrade
|
||
|
else
|
||
|
sudo pacman -Syu
|
||
|
sudo pacman -Rcs "$(pacman -Qdtq)"
|
||
|
fi
|
||
|
echo "Alle Systemupdates wurden durchgeführt"
|
||
|
}
|
||
|
|
||
|
# Function: update_debian
|
||
|
# Purpose: Updating Debian or distros based on it
|
||
|
# Parameters: None
|
||
|
# Return value: None
|
||
|
function update_debian() {
|
||
|
sudo apt-get update
|
||
|
sudo apt-get upgrade -y
|
||
|
sudo apt autoremove -y
|
||
|
sudo snap refresh
|
||
|
echo "Alle Systemupdates wurden durchgeführt"
|
||
|
}
|
||
|
|
||
|
# Function: update_fedora
|
||
|
# Purpose: Updating Fedora
|
||
|
# Parameters: None
|
||
|
# Return value: None
|
||
|
function update_fedora() {
|
||
|
sudo dnf check-update
|
||
|
sudo dnf update -y
|
||
|
sudo dnf autoremove -y
|
||
|
echo "Alle Systemupdates wurden durchgeführt"
|
||
|
}
|
||
|
|
||
|
# Function: update_opensuse
|
||
|
# Purpose: Updating openSUSE
|
||
|
# Parameters: None
|
||
|
# Return value: None
|
||
|
function update_opensuse() {
|
||
|
sudo zypper dup -d
|
||
|
sudo zypper dup
|
||
|
echo "Alle Systemupdates wurden durchgeführt"
|
||
|
}
|
||
|
|
||
|
# Function: update_gentoo
|
||
|
# Purpose: Updating openSUSE
|
||
|
# Parameters: None
|
||
|
# Return value: None
|
||
|
function update_gentoo() {
|
||
|
sudo emerge --sync
|
||
|
sudo emerge -avuDN @world
|
||
|
sudo emerge --depclean
|
||
|
echo "Alle Systemupdates wurden durchgeführt"
|
||
|
}
|
||
|
|
||
|
# Function: update_flatpak
|
||
|
# Purpose: Updating flatpak
|
||
|
# Parameters: None
|
||
|
# Return value: None
|
||
|
function update_flatpak() {
|
||
|
flatpak update
|
||
|
flatpak uninstall --unused
|
||
|
echo "Alle Flatpak Updates wurden durchgeführt"
|
||
|
}
|
||
|
|
||
|
# Function: update_snap
|
||
|
# Purpose: Updating snap
|
||
|
# Parameters: None
|
||
|
# Return value: None
|
||
|
function update_snap() {
|
||
|
sudo snap refresh
|
||
|
echo "Alle Snap Updates wurden durchgeführt"
|
||
|
}
|
||
|
|
||
|
# Function: cleanup_snap
|
||
|
# Purpose: Remove old snap packages
|
||
|
# Parameters: None
|
||
|
# Return value: None
|
||
|
function cleanup_snap() {
|
||
|
LANG=C snap list --all | awk '/disabled/{print $1, $3}' |
|
||
|
while read snapname revision; do
|
||
|
snap remove "$snapname" --revision="$revision"
|
||
|
done
|
||
|
}
|
||
|
|
||
|
echo "Dieses Skript installiert Updates auf dem Computer"
|
||
|
|
||
|
logging
|
||
|
|
||
|
sudo echo "Die Paketdatenbank wird aktualisiert. Das kann einen Moment dauern."
|
||
|
|
||
|
time_start
|
||
|
|
||
|
# Bestimmen der Linux-Distribution anhand der verfügbaren Update-Befehle
|
||
|
if command -v pacman &> /dev/null; then
|
||
|
update_arch
|
||
|
elif command -v apt-get &> /dev/null; then
|
||
|
update_debian
|
||
|
elif command -v dnf &> /dev/null; then
|
||
|
update_fedora
|
||
|
elif command -v zypper &> /dev/null; then
|
||
|
update_opensuse
|
||
|
elif command -v emerge &> /dev/null; then
|
||
|
update_gentoo
|
||
|
else
|
||
|
echo "Die Distribution wird nicht unterstützt."
|
||
|
fi
|
||
|
|
||
|
# Überprüfen, ob Flatpak installiert ist
|
||
|
if command -v flatpak &> /dev/null; then
|
||
|
update_flatpak
|
||
|
else
|
||
|
echo "Flatpak ist nicht installiert."
|
||
|
fi
|
||
|
|
||
|
# Überprüfen, ob Snap installiert ist
|
||
|
if command -v snap &> /dev/null; then
|
||
|
update_snap
|
||
|
else
|
||
|
echo "Snap ist nicht installiert."
|
||
|
fi
|
||
|
|
||
|
time_end
|