dnf-auto-update.sh
· 1.8 KiB · Bash
Raw
#!/usr/bin/env bash
set -euo pipefail
# --- Réglages ---
LOGFILE="/var/log/auto-upgrade.log"
LOCKFILE="/var/run/auto-upgrade.lock"
# --- Vérification root ---
if [ "$(id -u)" -ne 0 ]; then
echo "Ce script doit être exécuté en root." >&2
exit 1
fi
# --- Logging ---
umask 022
mkdir -p "$(dirname "$LOGFILE")"
if [ -t 1 ]; then
exec > >(tee -a "$LOGFILE") 2>&1
else
exec >>"$LOGFILE" 2>&1
fi
echo "===== $(date -Is) : démarrage auto-upgrade (AlmaLinux) ====="
# --- Lock pour éviter les collisions ---
exec 9>"$LOCKFILE"
if ! flock -n 9; then
echo "Un autre auto-upgrade est déjà en cours, arrêt."
exit 0
fi
# --- Fonction de détection reboot ---
reboot_required() {
if command -v needs-restarting >/dev/null 2>&1; then
if needs-restarting -r >/dev/null 2>&1; then
return 1 # pas de reboot
else
return 0 # reboot requis
fi
fi
local running latest
running="$(uname -r)"
latest="$(rpm -q kernel-core --qf '%{VERSION}-%{RELEASE}.%{ARCH}\n' 2>/dev/null | sort -V | tail -n1)"
[ -z "$latest" ] && latest="$(rpm -q kernel --qf '%{VERSION}-%{RELEASE}.%{ARCH}\n' 2>/dev/null | sort -V | tail -n1)"
if [ -n "$latest" ] && [ "$latest" != "$running" ]; then
return 0
fi
return 1
}
# --- Étapes de mise à jour ---
echo "[1/4] dnf makecache --refresh"
dnf -y makecache --refresh
echo "[2/4] dnf upgrade --refresh"
dnf -y upgrade --refresh
echo "[3/4] dnf distro-sync"
dnf -y distro-sync
echo "[4/4] dnf autoremove"
dnf -y autoremove || true
# --- Reboot si nécessaire ---
if reboot_required; then
echo "Reboot nécessaire (kernel ou libs critiques)."
/sbin/reboot
else
echo "Pas besoin de reboot."
fi
echo "===== $(date -Is) : fin auto-upgrade ====="
echo "--------------------------------------------------------"
exit 0
| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | # --- Réglages --- |
| 5 | LOGFILE="/var/log/auto-upgrade.log" |
| 6 | LOCKFILE="/var/run/auto-upgrade.lock" |
| 7 | |
| 8 | # --- Vérification root --- |
| 9 | if [ "$(id -u)" -ne 0 ]; then |
| 10 | echo "Ce script doit être exécuté en root." >&2 |
| 11 | exit 1 |
| 12 | fi |
| 13 | |
| 14 | # --- Logging --- |
| 15 | umask 022 |
| 16 | mkdir -p "$(dirname "$LOGFILE")" |
| 17 | if [ -t 1 ]; then |
| 18 | exec > >(tee -a "$LOGFILE") 2>&1 |
| 19 | else |
| 20 | exec >>"$LOGFILE" 2>&1 |
| 21 | fi |
| 22 | |
| 23 | echo "===== $(date -Is) : démarrage auto-upgrade (AlmaLinux) =====" |
| 24 | |
| 25 | # --- Lock pour éviter les collisions --- |
| 26 | exec 9>"$LOCKFILE" |
| 27 | if ! flock -n 9; then |
| 28 | echo "Un autre auto-upgrade est déjà en cours, arrêt." |
| 29 | exit 0 |
| 30 | fi |
| 31 | |
| 32 | # --- Fonction de détection reboot --- |
| 33 | reboot_required() { |
| 34 | if command -v needs-restarting >/dev/null 2>&1; then |
| 35 | if needs-restarting -r >/dev/null 2>&1; then |
| 36 | return 1 # pas de reboot |
| 37 | else |
| 38 | return 0 # reboot requis |
| 39 | fi |
| 40 | fi |
| 41 | |
| 42 | local running latest |
| 43 | running="$(uname -r)" |
| 44 | latest="$(rpm -q kernel-core --qf '%{VERSION}-%{RELEASE}.%{ARCH}\n' 2>/dev/null | sort -V | tail -n1)" |
| 45 | [ -z "$latest" ] && latest="$(rpm -q kernel --qf '%{VERSION}-%{RELEASE}.%{ARCH}\n' 2>/dev/null | sort -V | tail -n1)" |
| 46 | if [ -n "$latest" ] && [ "$latest" != "$running" ]; then |
| 47 | return 0 |
| 48 | fi |
| 49 | return 1 |
| 50 | } |
| 51 | |
| 52 | # --- Étapes de mise à jour --- |
| 53 | echo "[1/4] dnf makecache --refresh" |
| 54 | dnf -y makecache --refresh |
| 55 | |
| 56 | echo "[2/4] dnf upgrade --refresh" |
| 57 | dnf -y upgrade --refresh |
| 58 | |
| 59 | echo "[3/4] dnf distro-sync" |
| 60 | dnf -y distro-sync |
| 61 | |
| 62 | echo "[4/4] dnf autoremove" |
| 63 | dnf -y autoremove || true |
| 64 | |
| 65 | # --- Reboot si nécessaire --- |
| 66 | if reboot_required; then |
| 67 | echo "Reboot nécessaire (kernel ou libs critiques)." |
| 68 | /sbin/reboot |
| 69 | else |
| 70 | echo "Pas besoin de reboot." |
| 71 | fi |
| 72 | |
| 73 | echo "===== $(date -Is) : fin auto-upgrade =====" |
| 74 | echo "--------------------------------------------------------" |
| 75 | exit 0 |