kyx revised this gist 1 week ago. Go to revision
No changes
kyx revised this gist 1 week ago. Go to revision
1 file changed, 75 insertions
dnf-auto-update.sh(file created)
| @@ -0,0 +1,75 @@ | |||
| 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 | |
Newer
Older