#!/usr/bin/env bash # Server Cleanup Script (Podman + System) set -euo pipefail PODMAN_USER="user" source /srv/podman/.env source /srv/logging.sh if [ "$EUID" -ne 0 ]; then echo "Error: This script must be run as root." exit 1 fi get_free_space() { df / --output=avail | tail -1 } PRE_CLEAN=$(get_free_space) echo "--- Starting server cleanup ---" # 1. APT cleanup if command -v apt-get >/dev/null 2>&1; then echo "[APT] Cleaning package cache..." apt-get update >/dev/null apt-get autoclean -y >/dev/null apt-get clean fi # 2. Podman cleanup if command -v podman >/dev/null 2>&1; then echo "[PODMAN] Cleaning unused resources..." sudo -iu $PODMAN_USER podman system prune -a -f > /dev/null sudo -iu $PODMAN_USER podman volume prune -f > /dev/null fi # 3. System logs echo "[LOGS] Vacuum logs older than 15 days..." journalctl --vacuum-time=15d >/dev/null # 4. Temp files echo "[TEMP] Cleaning temp files older than 15 days..." find /tmp -mindepth 1 -mtime +15 -delete 2>/dev/null || true find /var/tmp -mindepth 1 -mtime +15 -delete 2>/dev/null || true echo "--- Cleanup finished ---" POST_CLEAN=$(get_free_space) RECLAIMED_KB=$((POST_CLEAN - PRE_CLEAN)) if [ "$RECLAIMED_KB" -le 0 ]; then echo "No significant space reclaimed." else RECLAIMED_HUMAN=$(numfmt --to=iec --suffix=B "$((RECLAIMED_KB * 1024))") echo "Space reclaimed: $RECLAIMED_HUMAN" fi curl -m 10 --retry 5 ${HC_URL}/cleanup