log-rotation-usb-storage.sh

Banana Pi BPI-RT2: AX3000 WiFi 6 Router Board · File : log-rotation-usb-storage.sh · Language : Bash · Use case : Archiver les logs système sur un disque USB 3.0 pour éviter la saturation de la Flash NAND.

Log rotation script moving system files to external USB storage.

The BPI-RT2 has limited NAND Flash (128 MB). This script uses the USB 3.0 port for log storage. Configure auto-mounting via /etc/fstab or a udev script. Schedule execution via crontab (e.g., @daily). Ensure write permissions are correct on the USB volume.

log-rotation-usb-storage.sh

#!/bin/sh
# Rotation des logs vers USB pour BPI-RT2

USB_MOUNT="/mnt/sda1"
LOG_DIR="/var/log"
TARGET_DIR="$USB_MOUNT/logs"

# Montage automatique (à adapter selon fstab ou auto-mount)
if [ ! -d "$USB_MOUNT" ]; then
    mkdir -p "$USB_MOUNT"
    mount /dev/sda1 "$USB_MOUNT"
fi

if [ -d "$TARGET_DIR" ]; then
    echo "$(date): Rotation des logs..." >> "$TARGET_DIR/rotation.log"
    
    # Copie des logs critiques
    cp "$LOG_DIR/messages" "$TARGET_DIR/messages.$(date +%Y%m%d)"
    cp "$LOG_DIR/system.log" "$TARGET_DIR/system.$(date +%Y%m%d)" 2>/dev/null
    
    # Compression et nettoyage local (libère la Flash)
    gzip "$TARGET_DIR/messages.$(date +%Y%m%d)"
    rm -f "$LOG_DIR/messages.old"
    mv "$LOG_DIR/messages" "$LOG_DIR/messages.old"
    touch "$LOG_DIR/messages"
    
    echo "Rotation terminée." >> "$TARGET_DIR/rotation.log"
else
    echo "Erreur: Répertoire cible non trouvé sur USB."
fi

← Back to the board