#!/bin/bash # Maintenance script for Talk2Me application # This script helps manage temporary files and disk space UPLOAD_FOLDER="${UPLOAD_FOLDER:-/tmp/talk2me_uploads}" MAX_AGE_MINUTES=5 echo "Talk2Me Maintenance Script" echo "=========================" # Function to check disk usage check_disk_usage() { echo -e "\nDisk Usage:" df -h "$UPLOAD_FOLDER" 2>/dev/null || df -h /tmp } # Function to show temp file stats show_temp_stats() { echo -e "\nTemporary File Statistics:" if [ -d "$UPLOAD_FOLDER" ]; then file_count=$(find "$UPLOAD_FOLDER" -type f 2>/dev/null | wc -l) total_size=$(du -sh "$UPLOAD_FOLDER" 2>/dev/null | cut -f1) echo " Upload folder: $UPLOAD_FOLDER" echo " File count: $file_count" echo " Total size: ${total_size:-0}" if [ $file_count -gt 0 ]; then echo -e "\n Oldest files:" find "$UPLOAD_FOLDER" -type f -printf '%T+ %p\n' 2>/dev/null | sort | head -5 fi else echo " Upload folder does not exist: $UPLOAD_FOLDER" fi } # Function to clean old temp files clean_temp_files() { echo -e "\nCleaning temporary files older than $MAX_AGE_MINUTES minutes..." if [ -d "$UPLOAD_FOLDER" ]; then # Count files before cleanup before_count=$(find "$UPLOAD_FOLDER" -type f 2>/dev/null | wc -l) # Remove old files find "$UPLOAD_FOLDER" -type f -mmin +$MAX_AGE_MINUTES -delete 2>/dev/null # Count files after cleanup after_count=$(find "$UPLOAD_FOLDER" -type f 2>/dev/null | wc -l) removed=$((before_count - after_count)) echo " Removed $removed files" else echo " Upload folder does not exist: $UPLOAD_FOLDER" fi } # Function to setup upload folder setup_upload_folder() { echo -e "\nSetting up upload folder..." if [ ! -d "$UPLOAD_FOLDER" ]; then mkdir -p "$UPLOAD_FOLDER" chmod 755 "$UPLOAD_FOLDER" echo " Created: $UPLOAD_FOLDER" else echo " Exists: $UPLOAD_FOLDER" fi } # Function to monitor in real-time monitor_realtime() { echo -e "\nMonitoring temporary files (Press Ctrl+C to stop)..." while true; do clear echo "Talk2Me File Monitor - $(date)" echo "================================" show_temp_stats check_disk_usage sleep 5 done } # Main menu case "${1:-help}" in status) show_temp_stats check_disk_usage ;; clean) clean_temp_files show_temp_stats ;; setup) setup_upload_folder ;; monitor) monitor_realtime ;; all) setup_upload_folder clean_temp_files show_temp_stats check_disk_usage ;; *) echo "Usage: $0 {status|clean|setup|monitor|all}" echo "" echo "Commands:" echo " status - Show current temp file statistics" echo " clean - Clean old temporary files" echo " setup - Create upload folder if needed" echo " monitor - Real-time monitoring" echo " all - Run setup, clean, and show status" echo "" echo "Environment Variables:" echo " UPLOAD_FOLDER - Set custom upload folder (default: /tmp/talk2me_uploads)" ;; esac