#!/bin/bash # =========================================== # Cleanup Script for Social Media Automation # Run weekly via cron: # 0 3 * * 0 /path/to/cleanup.sh >> /var/log/cleanup.log 2>&1 # =========================================== set -e # Configuration PROJECT_DIR="${PROJECT_DIR:-/root/Facebook-X-Threads-Automation}" LOG_RETENTION_DAYS="${LOG_RETENTION_DAYS:-30}" DOCKER_LOG_MAX_SIZE="${DOCKER_LOG_MAX_SIZE:-100m}" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' log() { echo -e "[$(date '+%Y-%m-%d %H:%M:%S')] $1" } success() { log "${GREEN}$1${NC}" } warning() { log "${YELLOW}$1${NC}" } log "Starting cleanup process..." # =========================================== # 1. CLEAN DOCKER LOGS # =========================================== log "Cleaning Docker container logs..." # Truncate Docker logs (requires root) if [ -d /var/lib/docker/containers ]; then for container_dir in /var/lib/docker/containers/*/; do log_file="${container_dir}*-json.log" for f in $log_file; do if [ -f "$f" ]; then size_before=$(du -h "$f" | cut -f1) if truncate -s 0 "$f" 2>/dev/null; then log " Truncated: $(basename $(dirname $f)) ($size_before)" fi fi done done success "Docker logs cleaned" else warning "Docker log directory not found (might need sudo)" fi # =========================================== # 2. CLEAN APPLICATION LOGS # =========================================== log "Cleaning application logs older than $LOG_RETENTION_DAYS days..." if [ -d "$PROJECT_DIR/logs" ]; then count=$(find "$PROJECT_DIR/logs" -name "*.log" -type f -mtime +$LOG_RETENTION_DAYS 2>/dev/null | wc -l) find "$PROJECT_DIR/logs" -name "*.log" -type f -mtime +$LOG_RETENTION_DAYS -delete 2>/dev/null || true log " Deleted $count old log file(s)" fi # =========================================== # 3. CLEAN NGINX LOGS # =========================================== log "Rotating nginx logs..." if docker ps --format '{{.Names}}' | grep -q "social-automation-nginx"; then docker exec social-automation-nginx nginx -s reopen 2>/dev/null && \ success "Nginx logs rotated" || warning "Could not rotate nginx logs" fi # =========================================== # 4. CLEAN DOCKER SYSTEM # =========================================== log "Cleaning Docker system..." # Remove unused images, containers, networks docker system prune -f --volumes 2>/dev/null && \ success "Docker system cleaned" || warning "Could not clean Docker system" # Remove dangling images dangling=$(docker images -f "dangling=true" -q 2>/dev/null | wc -l) if [ $dangling -gt 0 ]; then docker rmi $(docker images -f "dangling=true" -q) 2>/dev/null || true log " Removed $dangling dangling image(s)" fi # =========================================== # 5. CLEAN TEMP FILES # =========================================== log "Cleaning temporary files..." # Python cache find "$PROJECT_DIR" -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true find "$PROJECT_DIR" -type f -name "*.pyc" -delete 2>/dev/null || true find "$PROJECT_DIR" -type f -name "*.pyo" -delete 2>/dev/null || true # Pytest cache rm -rf "$PROJECT_DIR/.pytest_cache" 2>/dev/null || true success "Temporary files cleaned" # =========================================== # 6. DISK USAGE REPORT # =========================================== log "─────────────────────────────────────────" log "Disk Usage Report:" log " Project: $(du -sh $PROJECT_DIR 2>/dev/null | cut -f1)" log " Backups: $(du -sh $PROJECT_DIR/backups 2>/dev/null | cut -f1 || echo 'N/A')" log " Docker: $(docker system df --format '{{.Size}}' 2>/dev/null | head -1 || echo 'N/A')" log " Disk: $(df -h / | awk 'NR==2 {print $4 " free of " $2}')" log "─────────────────────────────────────────" success "Cleanup process completed!"