Files
Server-scripts/utils/upgrade.sh
T

272 lines
6.4 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
###############################################################################
# Script Name: system-update.sh
#
# Description:
# Performs automated system maintenance and package updates.
#
# Tasks:
# - apt update
# - apt upgrade
# - apt dist-upgrade
# - apt autoclean
# - apt autoremove
# - flatpak update
# - cleanup old log files
# - update Docker images
# - restart Docker service
# - maintain SpotDL Python virtual environment
# - install/update spotdl, yt-dlp and youtube-dl
# - optional email reporting
#
# Requirements:
# - apt
# - flatpak (optional)
# - python3-venv
# - docker (optional)
# - mail (optional, for -mail mode)
#
# Usage:
#
# Normal execution:
# ./system-update.sh
#
# Email report mode:
# ./system-update.sh -mail
#
# Logs:
# ~/.LOG/log_update_<timestamp>.log
#
###############################################################################
set -o pipefail
home_dir="/home/lucas"
venv_dir="${home_dir}/tools/spotdl_venv"
green="\e[32m"
red="\e[31m"
norm="\e[0m"
MAIL=false
write_out() {
if [[ "${MAIL}" == "true" ]]; then
printf "$@\n" >> /tmp/update.txt
else
printf "$@\n"
fi
}
echo "Start Update"
mkdir -p "${home_dir}/.LOG"
log_date="$(date +"%m%d%H")"
if [[ "$1" == "-mail" ]]; then
> /tmp/update.txt
exec 3>&1 4>&2 \
1>"${home_dir}/.LOG/log_update_${log_date}.log" \
2>&1
MAIL=true
green=""
red=""
norm=""
fi
write_out "Start: $(date)"
###############################################################################
# System Updates
###############################################################################
###############################################################################
# Standard updates
###############################################################################
CMD=(
"apt -y update"
"apt -y upgrade"
"apt -y dist-upgrade"
"apt -y autoclean"
"apt -y autoremove"
"find ${home_dir}/.LOG/ -name 'log_update_*' -mtime +6 -type f -delete"
)
MSG=(
"update"
"upgrade"
"dist-upgrade"
"autoclean"
"autoremove"
"log-cleanup"
)
for ((i=0; i<${#CMD[@]}; i++)); do
printf "Running: %s\n" "${CMD[$i]}"
if eval "${CMD[$i]}"; then
write_out "${MSG[$i]} [${green}OK${norm}]"
else
write_out "${MSG[$i]} [${red}FAIL${norm}]"
fi
done
###############################################################################
# Flatpak Update
###############################################################################
if command -v flatpak >/dev/null 2>&1; then
echo "Starting Flatpak Update..."
flatpak update -y --noninteractive
ret=$?
echo "Flatpak returned: ${ret}"
if [[ $ret -eq 0 ]]; then
write_out "flatpak-update [${green}OK${norm}]"
else
write_out "flatpak-update [${red}FAIL${norm}]"
fi
fi
echo "Finished Package Update"
###############################################################################
# SpotDL Virtual Environment
###############################################################################
if [[ ! -d "$venv_dir" ]] && [[ "$1" == "--create-venv" ]]; then
echo "Creating SpotDL virtual environment"
mkdir -p "${home_dir}/tools"
python3 -m venv "$venv_dir"
fi
if [[ -d "$venv_dir" ]]; then
if [[ ! -x "${venv_dir}/bin/pip3" ]]; then
echo "Installing pip"
"${venv_dir}/bin/python" -m ensurepip --upgrade
fi
echo "Updating venv tooling"
"${venv_dir}/bin/pip3" install --upgrade \
pip \
setuptools \
wheel
###############################################################################
# Ensure required packages exist
###############################################################################
mkdir -p "${home_dir}/.local/bin"
packages=(
spotdl
yt-dlp
youtube-dl
)
for package in "${packages[@]}"; do
echo "Checking package: ${package}"
if ! "${venv_dir}/bin/pip3" show "$package" >/dev/null 2>&1; then
echo "Installing ${package}"
"${venv_dir}/bin/pip3" install "$package"
else
echo "Updating ${package}"
"${venv_dir}/bin/pip3" install --upgrade "$package"
fi
if [[ -x "${venv_dir}/bin/${bin}" ]]; then
ln -sf "${venv_dir}/bin/${package}" "${home_dir}/.local/bin/${package}"
fi
if [[ $? -eq 0 ]]; then
write_out "${package} [${green}OK${norm}]"
else
write_out "${package} [${red}FAIL${norm}]"
fi
done
fi
###############################################################################
# Docker Updates
###############################################################################
if command -v docker >/dev/null 2>&1; then
echo "Updating Docker images"
docker images --format "{{.Repository}}:{{.Tag}}" |
while read -r image; do
case "$image" in
qbittorrent-qbittorrent:*)
echo "Updating custom qBittorrent image..."
(
cd /docker/qbittorrent &&
docker compose build --pull &&
docker compose up -d
)
;;
*"<none>"*)
;;
*)
docker pull "$image"
;;
esac
done
systemctl restart docker
if [[ $? -eq 0 ]]; then
write_out "docker [${green}OK${norm}]"
else
write_out "docker [${red}FAIL${norm}]"
fi
fi
if [ -f /docker/qbittorrent/docker-compose.yml ]; then
cd /docker/qbittorrent || exit 1
 
if docker compose config >/dev/null 2>&1; then
echo "Updating custom qBittorrent image..."
docker compose build --pull
docker compose up -d
fi
fi
###############################################################################
# Mail Report
###############################################################################
if [[ "${MAIL}" == "true" ]]; then
mail_str="$(cat /tmp/update.txt)"
echo "$mail_str" | mail \
-s "Automated Server Message - Upgrade" \
lucas-f@live.at
fi
write_out "Finished: $(date)"