🧹 Nettoyage logs & espace
Purge logs anciens, journald, /tmp, analyse espace disque
bashScript
Nettoyage logs & espace disque
Script de maintenance : purge des logs anciens, nettoyage journald, /tmp et rapport d'espace.
Script
#!/bin/bash
nettoyage-logs.sh — Purge logs, journald, /tmp + rapport espace
Usage : ./nettoyage-logs.sh [--dry-run] [--retention-days 30]
set -uo pipefail
── Configuration ─────────────────────────────────────────────
LOG_RETENTION_DAYS=30 # Logs système
TMP_RETENTION_HOURS=24 # Fichiers /tmp
JOURNALD_RETENTION="2weeks"
DRY_RUN=false
REPORT_FILE="/var/log/nettoyage-$(date +%Y%m%d).log"
MIN_FREE_GB=5 # Alerte si espace libre < X Go
── Parse arguments ───────────────────────────────────────────
for arg in "$@"; do
case $arg in
--dry-run) DRY_RUN=true ;;
--retention-days=) LOG_RETENTION_DAYS="${arg#=}" ;;
esac
done
── Fonctions ─────────────────────────────────────────────────
log() { echo "[$(date '+%H:%M:%S')] $*" | tee -a "$REPORT_FILE"; }
run() {
if $DRY_RUN; then
log " [DRY-RUN] $*"
else
eval "$@" 2>&1 | while IFS= read -r line; do log " $line"; done
fi
}
disk_free_gb() { df -BG "$1" | awk 'NR==2{gsub("G","",$4); print $4}'; }
human_size() { du -sh "$1" 2>/dev/null | cut -f1; }
── Header ────────────────────────────────────────────────────
log "════════════════════════════════════════"
log " Nettoyage système — $(hostname)"
log " Mode: $( $DRY_RUN && echo 'DRY-RUN (simulation)' || echo 'RÉEL')"
log "════════════════════════════════════════"
── Espace avant ──────────────────────────────────────────────
log ""
log "📊 Espace disque AVANT"
df -h / /var /tmp 2>/dev/null | tee -a "$REPORT_FILE"
── 1. Logs système /var/log ───────────────────────────────────
log ""
log "📁 Logs anciens (>${LOG_RETENTION_DAYS}j) dans /var/log"
LOGS_SIZE=$(find /var/log -type f \( -name ".log" -o -name ".log." -o -name ".gz" \) \
-mtime +"$LOG_RETENTION_DAYS" -print 2>/dev/null | \
xargs -r du -ch 2>/dev/null | tail -1 | cut -f1)
log " Taille à supprimer : ${LOGS_SIZE:-0}"
run "find /var/log -type f \( -name '.log.' -o -name '*.gz' \) \
-mtime +${LOG_RETENTION_DAYS} -delete -print 2>/dev/null | \
wc -l | xargs -I{} echo '{} fichier(s) supprimé(s)'"
Tronquer les gros logs actifs (ne pas les supprimer)
for logfile in /var/log/syslog /var/log/auth.log /var/log/kern.log; do
if [[ -f "$logfile" ]]; then
SIZE_MB=$(du -sm "$logfile" | cut -f1)
if [[ $SIZE_MB -gt 100 ]]; then
log " ⚠ $logfile volumineux (${SIZE_MB}MB) — troncature"
run "truncate -s 10M $logfile"
fi
fi
done
── 2. Journald ───────────────────────────────────────────────
log ""
log "📋 Nettoyage journald (rétention: $JOURNALD_RETENTION)"
JOURNALD_BEFORE=$(journalctl --disk-usage 2>/dev/null | grep -oP '[\d.]+ [A-Z]' || echo "?")
log " Taille actuelle : $JOURNALD_BEFORE"
run "journalctl --vacuum-time=$JOURNALD_RETENTION --vacuum-size=500M"
── 3. /tmp ───────────────────────────────────────────────────
log ""
log "🗑 Fichiers /tmp (>${TMP_RETENTION_HOURS}h)"
TMP_SIZE=$(find /tmp -type f -atime +"$(( TMP_RETENTION_HOURS / 24 ))" \
-print 2>/dev/null | xargs -r du -ch 2>/dev/null | tail -1 | cut -f1)
log " Taille à supprimer : ${TMP_SIZE:-0}"
run "find /tmp -type f -atime +$(( TMP_RETENTION_HOURS / 24 )) -delete 2>/dev/null || true"
run "find /tmp -mindepth 1 -maxdepth 1 -type d -empty -delete 2>/dev/null || true"
── 4. Cache apt/dnf ─────────────────────────────────────────
log ""
log "📦 Cache gestionnaire de paquets"
if command -v apt-get &>/dev/null; then
log " apt cache : $(du -sh /var/cache/apt/archives 2>/dev/null | cut -f1)"
run "apt-get clean -y"
elif command -v dnf &>/dev/null; then
run "dnf clean all"
elif command -v yum &>/dev/null; then
run "yum clean all"
fi
── 5. Anciens kernels (Debian/Ubuntu) ───────────────────────
if command -v dpkg &>/dev/null; then
log ""
log "🐧 Anciens kernels"
CURRENT_KERNEL=$(uname -r)
OLD_KERNELS=$(dpkg --list 'linux-image-*' | \
awk '/^ii/{print $2}' | \
grep -v "$CURRENT_KERNEL" | \
grep -v 'linux-image-generic' || true)
if [[ -n "$OLD_KERNELS" ]]; then
log " Kernels à supprimer : $(echo "$OLD_KERNELS" | wc -l)"
run "apt-get purge -y $OLD_KERNELS"
else
log " Aucun ancien kernel trouvé"
fi
fi
── 6. Rapport final ──────────────────────────────────────────
log ""
log "📊 Espace disque APRÈS"
df -h / /var /tmp 2>/dev/null | tee -a "$REPORT_FILE"
Alerte espace libre
FREE_GB=$(disk_free_gb /)
if [[ "$FREE_GB" -lt "$MIN_FREE_GB" ]]; then
log "⚠️ ALERTE : Espace libre insuffisant (${FREE_GB}Go < ${MIN_FREE_GB}Go seuil)"
fi
log ""
log "✅ Nettoyage terminé — rapport : $REPORT_FILE"
Installation cron
# Nettoyage hebdomadaire (dimanche 3h)
echo "0 3 0 root /opt/scripts/nettoyage-logs.sh >> /var/log/nettoyage.log 2>&1" \
> /etc/cron.d/nettoyage-logs
🔧 Ouvrir tools.rdr-it.com — application complète →
Plus de 40 outils AdminSys gratuits · SSL · DNS · Docker · Nginx · SSH · Mermaid · et plus