#!/bin/bash # Script to check disk space usage on the server # Usage: ./check-disk-space.sh set -e # Configuration SERVER_IP="147.45.152.129" SERVER_USER="root" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' # No Color # Helper functions print_header() { echo "" echo -e "${CYAN}========================================${NC}" echo -e "${CYAN}$1${NC}" echo -e "${CYAN}========================================${NC}" } print_section() { echo "" echo -e "${BLUE}--- $1 ---${NC}" } print_info() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Function to format size in human readable format format_size() { local size=$1 if [ -z "$size" ]; then echo "0B" else echo "$size" fi } echo "🔍 Starting disk space diagnostics on ${SERVER_USER}@${SERVER_IP}..." echo "" ssh "$SERVER_USER@$SERVER_IP" << 'ENDSSH' set -e # Colors for remote output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' print_header() { echo "" echo -e "${CYAN}========================================${NC}" echo -e "${CYAN}$1${NC}" echo -e "${CYAN}========================================${NC}" } print_section() { echo "" echo -e "${BLUE}--- $1 ---${NC}" } print_info() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } # 1. General disk information print_header "1. GENERAL DISK INFORMATION" print_section "Disk Usage by Filesystem" df -h print_section "Inode Usage" df -i print_section "Root Filesystem Details" ROOT_USAGE=$(df -h / | tail -1 | awk '{print $5}' | sed 's/%//') ROOT_AVAILABLE=$(df -h / | tail -1 | awk '{print $4}') ROOT_USED=$(df -h / | tail -1 | awk '{print $3}') ROOT_TOTAL=$(df -h / | tail -1 | awk '{print $2}') echo "Root (/) filesystem:" echo " Total: $ROOT_TOTAL" echo " Used: $ROOT_USED" echo " Available: $ROOT_AVAILABLE" echo " Usage: ${ROOT_USAGE}%" if [ "$ROOT_USAGE" -gt 90 ]; then print_warning "Root filesystem usage is above 90%!" elif [ "$ROOT_USAGE" -gt 80 ]; then print_warning "Root filesystem usage is above 80%" fi # 2. Large directories analysis print_header "2. LARGE DIRECTORIES ANALYSIS" print_section "Top 20 Largest Directories in Root (/)" if [ -d "/" ]; then du -h --max-depth=1 / 2>/dev/null | sort -hr | head -20 || echo "Could not analyze root directory" fi print_section "Top 20 Largest Directories in Home (~)" if [ -d ~ ]; then du -h --max-depth=1 ~ 2>/dev/null | sort -hr | head -20 || echo "Could not analyze home directory" fi # 3. Project-specific directories print_header "3. PROJECT DIRECTORIES" print_section "Backend Project (~/mnemo_cards_backend)" if [ -d ~/mnemo_cards_backend ]; then BACKEND_SIZE=$(du -sh ~/mnemo_cards_backend 2>/dev/null | awk '{print $1}') echo "Total size: $BACKEND_SIZE" echo "" echo "Subdirectories:" du -h --max-depth=1 ~/mnemo_cards_backend 2>/dev/null | sort -hr | head -10 || true # Check build directory specifically if [ -d ~/mnemo_cards_backend/build ]; then BUILD_SIZE=$(du -sh ~/mnemo_cards_backend/build 2>/dev/null | awk '{print $1}') echo "" echo "Build directory size: $BUILD_SIZE" fi # Check .dart_tool if [ -d ~/mnemo_cards_backend/.dart_tool ]; then DART_TOOL_SIZE=$(du -sh ~/mnemo_cards_backend/.dart_tool 2>/dev/null | awk '{print $1}') echo ".dart_tool size: $DART_TOOL_SIZE" fi else print_warning "Backend directory not found" fi print_section "Common Packages" if [ -d ~/mnemo_cards_common ]; then COMMON_SIZE=$(du -sh ~/mnemo_cards_common 2>/dev/null | awk '{print $1}') echo "mnemo_cards_common: $COMMON_SIZE" du -h --max-depth=1 ~/mnemo_cards_common 2>/dev/null | sort -hr | head -5 || true fi if [ -d ~/mnemo_cards_common_backend ]; then COMMON_BACKEND_SIZE=$(du -sh ~/mnemo_cards_common_backend 2>/dev/null | awk '{print $1}') echo "mnemo_cards_common_backend: $COMMON_BACKEND_SIZE" du -h --max-depth=1 ~/mnemo_cards_common_backend 2>/dev/null | sort -hr | head -5 || true fi print_section "Web Application (/var/www/mnemo_cards)" if [ -d /var/www/mnemo_cards ]; then WEB_SIZE=$(du -sh /var/www/mnemo_cards 2>/dev/null | awk '{print $1}') echo "Total size: $WEB_SIZE" du -h --max-depth=1 /var/www/mnemo_cards 2>/dev/null | sort -hr | head -10 || true else print_warning "Web app directory not found" fi print_section "Backup Directories" BACKUP_COUNT=0 BACKUP_TOTAL=0 for backup_dir in /var/www/mnemo_cards.backup.*; do if [ -d "$backup_dir" ]; then BACKUP_COUNT=$((BACKUP_COUNT + 1)) BACKUP_SIZE=$(du -sm "$backup_dir" 2>/dev/null | awk '{print $1}') BACKUP_TOTAL=$((BACKUP_TOTAL + BACKUP_SIZE)) BACKUP_SIZE_HR=$(du -sh "$backup_dir" 2>/dev/null | awk '{print $1}') BACKUP_NAME=$(basename "$backup_dir") BACKUP_DATE=$(echo "$BACKUP_NAME" | grep -oE '[0-9]{8}_[0-9]{6}' || echo "unknown") echo "$BACKUP_NAME: $BACKUP_SIZE_HR (date: $BACKUP_DATE)" fi done if [ $BACKUP_COUNT -eq 0 ]; then echo "No backup directories found" else BACKUP_TOTAL_HR=$(numfmt --to=iec-i --suffix=B $((BACKUP_TOTAL * 1024 * 1024)) 2>/dev/null || echo "${BACKUP_TOTAL}MB") echo "" echo "Total backups: $BACKUP_COUNT" echo "Total backup size: ~$BACKUP_TOTAL_HR" if [ $BACKUP_TOTAL -gt 1024 ]; then print_warning "Backups are taking more than 1GB of space" fi fi # 4. System directories print_header "4. SYSTEM DIRECTORIES" print_section "Log Files (/var/log)" if [ -d /var/log ]; then LOG_SIZE=$(du -sh /var/log 2>/dev/null | awk '{print $1}') echo "Total log size: $LOG_SIZE" echo "" echo "Largest log directories:" du -h --max-depth=1 /var/log 2>/dev/null | sort -hr | head -10 || true # Check specific log directories for log_dir in nginx systemd journal; do if [ -d "/var/log/$log_dir" ]; then LOG_DIR_SIZE=$(du -sh "/var/log/$log_dir" 2>/dev/null | awk '{print $1}') echo "/var/log/$log_dir: $LOG_DIR_SIZE" fi done fi print_section "SSL Certificates (/etc/letsencrypt)" if [ -d /etc/letsencrypt ]; then SSL_SIZE=$(du -sh /etc/letsencrypt 2>/dev/null | awk '{print $1}') echo "Total SSL certificates size: $SSL_SIZE" du -h --max-depth=2 /etc/letsencrypt 2>/dev/null | sort -hr | head -10 || true else echo "Let's Encrypt directory not found" fi print_section "Temporary Files (/tmp)" if [ -d /tmp ]; then TMP_SIZE=$(du -sh /tmp 2>/dev/null | awk '{print $1}') TMP_COUNT=$(find /tmp -type f 2>/dev/null | wc -l) echo "Total /tmp size: $TMP_SIZE" echo "Number of files: $TMP_COUNT" if [ "$TMP_SIZE" != "0" ] && [ "$TMP_SIZE" != "" ]; then echo "Largest files in /tmp:" find /tmp -type f -exec du -h {} + 2>/dev/null | sort -hr | head -10 || true fi fi print_section "Cache Directories" if [ -d /var/cache ]; then CACHE_SIZE=$(du -sh /var/cache 2>/dev/null | awk '{print $1}') echo "/var/cache: $CACHE_SIZE" du -h --max-depth=1 /var/cache 2>/dev/null | sort -hr | head -10 || true fi if [ -d ~/.cache ]; then ROOT_CACHE_SIZE=$(du -sh ~/.cache 2>/dev/null | awk '{print $1}') echo "~/.cache: $ROOT_CACHE_SIZE" du -h --max-depth=1 ~/.cache 2>/dev/null | sort -hr | head -10 || true fi # 5. Docker check print_header "5. DOCKER (if installed)" if command -v docker &> /dev/null; then print_info "Docker is installed" print_section "Docker Images" docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" 2>/dev/null || echo "Could not list Docker images" DOCKER_IMAGES_SIZE=$(docker system df --format "{{.Size}}" 2>/dev/null | head -1 || echo "unknown") echo "Total Docker images size: $DOCKER_IMAGES_SIZE" print_section "Docker Containers" docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Size}}" 2>/dev/null || echo "Could not list Docker containers" print_section "Docker System Disk Usage" docker system df 2>/dev/null || echo "Could not get Docker disk usage" print_section "Docker Volumes" docker volume ls 2>/dev/null || echo "Could not list Docker volumes" if [ "$(docker volume ls -q 2>/dev/null | wc -l)" -gt 0 ]; then for volume in $(docker volume ls -q 2>/dev/null); do VOLUME_SIZE=$(docker system df -v 2>/dev/null | grep "$volume" | awk '{print $3}' || echo "unknown") echo "$volume: $VOLUME_SIZE" done fi else echo "Docker is not installed" fi # 6. Old files check print_header "6. OLD FILES CHECK" print_section "Old Log Files (older than 30 days)" OLD_LOG_COUNT=0 OLD_LOG_SIZE=0 if [ -d /var/log ]; then while IFS= read -r file; do if [ -f "$file" ]; then OLD_LOG_COUNT=$((OLD_LOG_COUNT + 1)) FILE_SIZE=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file" 2>/dev/null || echo 0) OLD_LOG_SIZE=$((OLD_LOG_SIZE + FILE_SIZE)) fi done < <(find /var/log -type f -mtime +30 2>/dev/null | head -20) if [ $OLD_LOG_COUNT -gt 0 ]; then OLD_LOG_SIZE_HR=$(numfmt --to=iec-i --suffix=B $OLD_LOG_SIZE 2>/dev/null || echo "${OLD_LOG_SIZE}B") echo "Found $OLD_LOG_COUNT old log files (sample of first 20)" echo "Total size: ~$OLD_LOG_SIZE_HR" echo "" echo "Sample old log files:" find /var/log -type f -mtime +30 -exec ls -lh {} \; 2>/dev/null | head -10 | awk '{print $9, "(" $5 ")"}' || true else echo "No old log files found (older than 30 days)" fi fi print_section "Old Backup Files" OLD_BACKUP_COUNT=0 for backup_dir in /var/www/mnemo_cards.backup.*; do if [ -d "$backup_dir" ]; then BACKUP_NAME=$(basename "$backup_dir") BACKUP_DATE=$(echo "$BACKUP_NAME" | grep -oE '[0-9]{8}_[0-9]{6}' || echo "") if [ -n "$BACKUP_DATE" ]; then BACKUP_TIMESTAMP=$(date -d "${BACKUP_DATE:0:4}-${BACKUP_DATE:4:2}-${BACKUP_DATE:6:2} ${BACKUP_DATE:9:2}:${BACKUP_DATE:11:2}:${BACKUP_DATE:13:2}" +%s 2>/dev/null || echo 0) CURRENT_TIMESTAMP=$(date +%s) DAYS_OLD=$(( (CURRENT_TIMESTAMP - BACKUP_TIMESTAMP) / 86400 )) if [ $DAYS_OLD -gt 30 ]; then OLD_BACKUP_COUNT=$((OLD_BACKUP_COUNT + 1)) BACKUP_SIZE=$(du -sh "$backup_dir" 2>/dev/null | awk '{print $1}') echo "$BACKUP_NAME: $BACKUP_SIZE (${DAYS_OLD} days old)" fi fi fi done if [ $OLD_BACKUP_COUNT -eq 0 ]; then echo "No old backup directories found (older than 30 days)" else print_warning "Found $OLD_BACKUP_COUNT old backup directories (older than 30 days)" fi print_section "Build Artifacts in Projects" BUILD_ARTIFACTS_SIZE=0 # Check for build directories for build_dir in ~/mnemo_cards_backend/build ~/mnemo_cards_common/build ~/mnemo_cards_common_backend/build; do if [ -d "$build_dir" ]; then BUILD_SIZE=$(du -sm "$build_dir" 2>/dev/null | awk '{print $1}') BUILD_ARTIFACTS_SIZE=$((BUILD_ARTIFACTS_SIZE + BUILD_SIZE)) BUILD_SIZE_HR=$(du -sh "$build_dir" 2>/dev/null | awk '{print $1}') echo "$build_dir: $BUILD_SIZE_HR" fi done if [ $BUILD_ARTIFACTS_SIZE -gt 0 ]; then BUILD_ARTIFACTS_SIZE_HR=$(numfmt --to=iec-i --suffix=B $((BUILD_ARTIFACTS_SIZE * 1024 * 1024)) 2>/dev/null || echo "${BUILD_ARTIFACTS_SIZE}MB") echo "Total build artifacts: ~$BUILD_ARTIFACTS_SIZE_HR" else echo "No build artifacts found" fi # Check for .dart_tool directories print_section ".dart_tool Directories" DART_TOOL_TOTAL=0 for dart_tool_dir in ~/mnemo_cards_backend/.dart_tool ~/mnemo_cards_common/.dart_tool ~/mnemo_cards_common_backend/.dart_tool; do if [ -d "$dart_tool_dir" ]; then DART_TOOL_SIZE=$(du -sm "$dart_tool_dir" 2>/dev/null | awk '{print $1}') DART_TOOL_TOTAL=$((DART_TOOL_TOTAL + DART_TOOL_SIZE)) DART_TOOL_SIZE_HR=$(du -sh "$dart_tool_dir" 2>/dev/null | awk '{print $1}') echo "$dart_tool_dir: $DART_TOOL_SIZE_HR" fi done if [ $DART_TOOL_TOTAL -gt 0 ]; then DART_TOOL_TOTAL_HR=$(numfmt --to=iec-i --suffix=B $((DART_TOOL_TOTAL * 1024 * 1024)) 2>/dev/null || echo "${DART_TOOL_TOTAL}MB") echo "Total .dart_tool size: ~$DART_TOOL_TOTAL_HR" else echo "No .dart_tool directories found" fi # Summary print_header "SUMMARY" echo "Key findings:" echo "" # Root filesystem usage ROOT_USAGE=$(df -h / | tail -1 | awk '{print $5}' | sed 's/%//') if [ "$ROOT_USAGE" -gt 90 ]; then print_warning "⚠️ Root filesystem usage is CRITICAL: ${ROOT_USAGE}%" elif [ "$ROOT_USAGE" -gt 80 ]; then print_warning "⚠️ Root filesystem usage is HIGH: ${ROOT_USAGE}%" else print_info "✓ Root filesystem usage: ${ROOT_USAGE}%" fi # Check for large directories echo "" echo "Recommendations:" if [ $BACKUP_COUNT -gt 5 ]; then print_warning "Consider cleaning old backups (found $BACKUP_COUNT backup directories)" fi if [ $OLD_LOG_COUNT -gt 0 ]; then print_warning "Consider rotating or cleaning old log files (found $OLD_LOG_COUNT files older than 30 days)" fi if [ $BUILD_ARTIFACTS_SIZE -gt 500 ]; then print_warning "Consider cleaning build artifacts (taking ~${BUILD_ARTIFACTS_SIZE}MB)" fi if [ $DART_TOOL_TOTAL -gt 500 ]; then print_warning "Consider cleaning .dart_tool directories (taking ~${DART_TOOL_TOTAL}MB)" fi if command -v docker &> /dev/null; then DOCKER_UNUSED=$(docker system df 2>/dev/null | grep -i "unused" | awk '{print $4}' || echo "") if [ -n "$DOCKER_UNUSED" ] && [ "$DOCKER_UNUSED" != "0B" ]; then print_warning "Consider running 'docker system prune' to free up unused Docker resources" fi fi echo "" print_info "Diagnostics completed!" ENDSSH echo "" echo -e "${GREEN}✅ Disk space diagnostics completed!${NC}" echo ""