File: //bigscoots/cpanel/disk_cleanup.sh
#!/bin/bash
# === Load shared functions and env ===
source /bigscoots/includes/common.sh
# === Config ===
LOG_DIR="/root/.bigscoots/cpanel/disk"
DATESTAMP=$(date +%F)
LOG_FILE="$LOG_DIR/cleanup_${DATESTAMP}.log"
THRESHOLD=95
MOUNT_POINT="/"
CHANNEL="#test"
EMOJI=":wastebasket:"
TAG="Disk Cleanup Triggered"
DOMAIN=""
# Ensure log directory exists
mkdir -p "$LOG_DIR"
# Get disk usage before cleanup
usage_before=$(df -h "$MOUNT_POINT" | awk 'NR==2 {print $5}' | tr -d '%')
msg_before="Disk usage before cleanup: ${usage_before}%"
# Exit if usage is below threshold
if (( usage_before < THRESHOLD )); then
echo "$(date): Usage at ${usage_before}%, no cleanup needed." >> "$LOG_FILE"
exit 0
fi
# === Start Logging ===
{
echo "Cleanup started at $(date)"
echo "$msg_before"
} >> "$LOG_FILE"
# === Remove backup directories ===
find /home/ -type d \( \
-path '*/.trash' -o \
-path '*/.spam' -o \
-path '*/softaculous_backups' -o \
-path '*/wp-content/updraft' -o \
-path '*/wp-content/wpvividbackups' -o \
-path '*/wp-content/ewww' -o \
-path '*/wp-content/backups-dup-pro' -o \
-path '*/wp-content/backups-dup-lite' -o \
-path '*/wp-content/backup-db' -o \
-path '*/wp-content/uploads/backupbuddy_backups' -o \
-path '*/wp-content/uploads/ai1wm-backups' -o \
-path '*/wp-content/uploads/backwpup' -o \
-path '*/wp-content/uploads/backwpup-*' -o \
-path '*/wp-content/uploads/backup-guard' -o \
-path '*/wp-content/uploads/backupcreator' -o \
-path '*/wp-content/uploads/wp-clone' -o \
-path '*/wp-content/uploads/ShortpixelBackups' -o \
-path '*/wp-content/uploads/snapshots' -o \
-path '*/wp-content/uploads/aiowps_backups' -o \
-path '*/wp-content/uploads/mainwp/backup' -o \
-path '*/wp-content/managewp' -o \
-path '*/wp-content/backupwordpress-*' -o \
-path '*/administrator/components/com_akeeba/backup' \
\) -execdir rm -rfv {} \; -prune >> "$LOG_FILE" 2>&1
# === Remove backup files ===
find /home/ -type f \( \
-name 'cgi-bin.zip' -o \
-name 'imscclone.zip' -o \
-name '.well-known.zip' -o \
-name 'debug.log' -o \
-name '*err.log' -o \
-name 'cpmove-*.tar.gz' -o \
-name 'wp-content.zip' -o \
-name 'error_log' -o \
-name 'Cpanel_Form_file.upload.*' -o \
-name 'public_html.tar.gz' -o \
-name '*wpress' \
\) -delete -print >> "$LOG_FILE" 2>&1
# === Get disk usage after cleanup ===
usage_after=$(df -h "$MOUNT_POINT" | awk 'NR==2 {print $5}' | tr -d '%')
msg_after="Disk usage after cleanup: ${usage_after}%"
# === Log completion ===
{
echo "$msg_after"
echo "Cleanup finished at $(date)"
} >> "$LOG_FILE"
# === Send Slack notification ===
send_slack_alert "$CHANNEL" "$EMOJI" "$TAG" "$DOMAIN" "$msg_before\n$msg_after\nLog: $LOG_FILE"