-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleardocker
More file actions
116 lines (98 loc) · 3.69 KB
/
cleardocker
File metadata and controls
116 lines (98 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env bash
###############################################################################
# cleanup-all.sh – Full cleanup + weekly auto-scheduling in cron
# Usage: sudo ./cleanup-all.sh
###############################################################################
set -euo pipefail
IFS=$'\n\t'
# Formatting functions
blue() { printf "\033[1;34m%s\033[0m\n" "$*"; }
green() { printf "\033[1;32m%s\033[0m\n" "$*"; }
separator() {
echo -e "\n=============================="
blue "$1"
echo "=============================="
}
# Ensures execution as root
[[ $EUID -ne 0 ]] && { echo "⚠️ Please run as root (sudo)."; exit 1; }
# Absolute path to this script
SCRIPT_PATH="$(readlink -f "$0")"
# 1. Auto-scheduling in cron (only on first run)
CRON_LINE="0 3 * * 1 $SCRIPT_PATH >> /var/log/cleanup.log 2>&1"
if crontab -l 2>/dev/null | grep -F "$SCRIPT_PATH" >/dev/null; then
separator "⏱️ Cron already configured"
else
(crontab -l 2>/dev/null; echo "$CRON_LINE") | crontab -
separator "✅ Scheduled: every Monday at 03:00"
fi
# Start timer
START=$(date +%s)
# 2. Disk usage before
separator "📊 Disk usage BEFORE"
df -hT | awk 'NR==1 || /\/dev\/sda1/'
# 3. APT – cache, orphan packages
separator "🧹 APT – cache & orphans"
CACHE_BEFORE=$(du -sh /var/cache/apt 2>/dev/null | cut -f1)
apt-get clean
apt-get autoclean -y
apt-get autoremove --purge -y
CACHE_AFTER=$(du -sh /var/cache/apt 2>/dev/null | cut -f1)
echo "➜ APT Cache: $CACHE_BEFORE → $CACHE_AFTER"
# 4. Old kernels
separator "🧹 Removing old kernels"
CURRENT=$(uname -r)
for K in $(dpkg -l 'linux-image-*' | awk '/^ii/{print $2}' | grep -v "$CURRENT"); do
echo "➜ Removing $K"
apt-get purge -y "$K" || true
done
command -v update-grub &>/dev/null && update-grub
# 5. Journald
separator "🧹 Trimming systemd logs (200 MB)"
journalctl --vacuum-size=200M
# 6. /tmp and /var/tmp
separator "🧹 Cleaning /tmp and /var/tmp"
find /tmp /var/tmp -mindepth 1 -maxdepth 1 -exec rm -rf {} +
# 7. Orphan libraries (deborphan)
separator "🧹 Orphan libraries"
command -v deborphan &>/dev/null || apt-get install -y deborphan
deborphan | xargs --no-run-if-empty apt-get purge -y
# 8. Snap / Flatpak
separator "🧹 Snap & Flatpak"
if command -v snap &>/dev/null; then
snap list --all | awk '/disabled/{print $1,$3}' | \
while read name rev; do snap remove "$name" --revision="$rev"; done || true
fi
command -v flatpak &>/dev/null && flatpak uninstall -y --unused || true
# 9. User Caches
separator "🧹 Cleaning user caches"
for dir in /home/*; do
if [ -d "$dir/.cache" ]; then
echo "➜ Cleaning cache for user $(basename $dir)"
rm -rf "$dir/.cache/*"
fi
done
# 10. Old Logs
separator "🧹 Cleaning old logs"
find /var/log -type f -name "*.log.*" -delete
find /var/log -type f -name "*.gz" -delete
find /var/log -type f -name "*.1" -delete
# 11. Open “deleted” files
separator "🔍 Open deleted files"
command -v lsof &>/dev/null || apt-get install -y lsof
lsof +L1 | awk '{printf "• %s (%s bytes)\n",$9,$7}' | head -n20 || echo "Nothing found."
# 12. Docker cleanup
if command -v docker &>/dev/null; then
separator "🐳 Docker cleanup"
echo -e "\n🔍 Pruning stopped containers…"; docker container prune -f
echo -e "\n🧼 Pruning unused images…"; docker image prune -a -f
echo -e "\n🧹 Pruning orphan volumes…"; docker volume prune -f
echo -e "\n📦 Current state:"; docker system df
else
separator "🐳 Docker not found – skipping"
fi
# 13. Summary and disk usage after
END=$(date +%s)
separator "✅ Cleanup finished in $((END-START)) seconds"
green "💾 Disk usage AFTER:"
df -hT | awk 'NR==1 || /\/dev\/sda1/'
echo -e "\n🚀 Done! This script now runs every Monday at 03:00."