#!/bin/bash # ============================================================================= # maintenance.sh - Maintenance automatique du photomaton # ============================================================================= # Usage: bash maintenance.sh # Appele quotidiennement par photobooth-maintenance.timer # ============================================================================= set -uo pipefail LOG_TAG="photomaton-maintenance" PHOTOBOOTH_DATA="${HOME}/photobooth-data" PHOTOS_DIR="${PHOTOBOOTH_DATA}/media" LOG_DIR="${PHOTOBOOTH_DATA}/log" # Nombre de jours de retention des photos (0 = garder indefiniment) PHOTO_RETENTION_DAYS=${PHOTO_RETENTION_DAYS:-0} # Nombre de jours de retention des logs LOG_RETENTION_DAYS=7 # Taille max du dossier photos en Mo (0 = pas de limite) MAX_PHOTOS_SIZE_MB=${MAX_PHOTOS_SIZE_MB:-0} log_info() { logger -t "$LOG_TAG" "[INFO] $1"; echo "[INFO] $1"; } log_warn() { logger -t "$LOG_TAG" "[WARN] $1"; echo "[WARN] $1"; } echo "=== Maintenance Photomaton - $(date) ===" # ------------------------------------------------------------------- # 1. NETTOYAGE DES LOGS # ------------------------------------------------------------------- log_info "Nettoyage des logs..." # Logs photobooth-app if [ -d "$LOG_DIR" ]; then deleted=$(find "$LOG_DIR" -name "*.log" -mtime +${LOG_RETENTION_DAYS} -delete -print 2>/dev/null | wc -l) log_info " $deleted ancien(s) log(s) photobooth supprime(s)" fi # Logs systeme comprimes find /var/log -name "*.gz" -mtime +3 -delete 2>/dev/null || true find /var/log -name "*.[0-9]" -mtime +3 -delete 2>/dev/null || true # Journald - garder seulement 50 Mo journalctl --vacuum-size=50M 2>/dev/null || true log_info "Logs nettoyes" # ------------------------------------------------------------------- # 2. NETTOYAGE DES PHOTOS (si retention configuree) # ------------------------------------------------------------------- if [ "$PHOTO_RETENTION_DAYS" -gt 0 ] && [ -d "$PHOTOS_DIR" ]; then log_info "Nettoyage des photos de plus de ${PHOTO_RETENTION_DAYS} jours..." deleted=$(find "$PHOTOS_DIR" -type f \( -name "*.jpg" -o -name "*.png" -o -name "*.gif" \) \ -mtime +${PHOTO_RETENTION_DAYS} -delete -print 2>/dev/null | wc -l) log_info " $deleted photo(s) supprimee(s)" fi # Limite de taille if [ "$MAX_PHOTOS_SIZE_MB" -gt 0 ] && [ -d "$PHOTOS_DIR" ]; then current_size=$(du -sm "$PHOTOS_DIR" 2>/dev/null | awk '{print $1}') if [ "${current_size:-0}" -gt "$MAX_PHOTOS_SIZE_MB" ]; then log_warn "Dossier photos: ${current_size}Mo > limite ${MAX_PHOTOS_SIZE_MB}Mo" log_warn "Suppression des photos les plus anciennes..." # Supprimer les plus vieux fichiers jusqu'a etre sous la limite while [ "$(du -sm "$PHOTOS_DIR" | awk '{print $1}')" -gt "$MAX_PHOTOS_SIZE_MB" ]; do oldest=$(find "$PHOTOS_DIR" -type f \( -name "*.jpg" -o -name "*.png" \) -printf '%T+ %p\n' 2>/dev/null | sort | head -1 | awk '{print $2}') if [ -n "$oldest" ]; then rm -f "$oldest" else break fi done fi fi # ------------------------------------------------------------------- # 3. NETTOYAGE CUPS # ------------------------------------------------------------------- log_info "Nettoyage des jobs d'impression..." # Supprimer les jobs termines/en erreur cancel -a 2>/dev/null || true # Reactiver les imprimantes si necessaire for printer in $(lpstat -p 2>/dev/null | grep "disabled" | awk '{print $2}'); do cupsenable "$printer" 2>/dev/null || true log_info " Imprimante reactivee: $printer" done log_info "Jobs d'impression nettoyes" # ------------------------------------------------------------------- # 4. NETTOYAGE CACHE SYSTEME # ------------------------------------------------------------------- log_info "Nettoyage du cache systeme..." # Cache apt apt-get clean 2>/dev/null || true apt-get autoclean 2>/dev/null || true # Cache thumbnails rm -rf ~/.cache/thumbnails/* 2>/dev/null || true # Fichiers temporaires find /tmp -type f -mtime +1 -delete 2>/dev/null || true log_info "Cache systeme nettoye" # ------------------------------------------------------------------- # 5. VERIFICATION DE L'INTEGRITE # ------------------------------------------------------------------- log_info "Verification de l'integrite du systeme..." # Verifier le filesystem (sans corriger, juste signaler) disk_errors=$(dmesg | grep -c "EXT4-fs error" 2>/dev/null || echo "0") if [ "$disk_errors" -gt 0 ]; then log_warn " $disk_errors erreur(s) filesystem detectee(s) dans dmesg!" fi # Espace disque disk_usage=$(df / | awk 'NR==2 {print $5}' | tr -d '%') log_info " Espace disque utilise: ${disk_usage}%" # Uptime uptime_info=$(uptime -p) log_info " Uptime: $uptime_info" # ------------------------------------------------------------------- # 6. RAPPORT # ------------------------------------------------------------------- echo "" echo "=== Rapport de maintenance ===" echo " Date: $(date)" echo " Espace disque: ${disk_usage}%" if [ -d "$PHOTOS_DIR" ]; then photo_count=$(find "$PHOTOS_DIR" -type f \( -name "*.jpg" -o -name "*.png" -o -name "*.gif" \) 2>/dev/null | wc -l) photo_size=$(du -sh "$PHOTOS_DIR" 2>/dev/null | awk '{print $1}') echo " Photos: ${photo_count} fichiers (${photo_size})" fi echo " Uptime: $uptime_info" echo "" log_info "Maintenance terminee"