-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidy.sh
executable file
·64 lines (61 loc) · 1.81 KB
/
tidy.sh
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
#!/bin/bash
###
### Disk cleanup script for Debian
###
# Variables
# Set the number of days to keep logs
LOG_DAYS=7
# Get free disk space on root partition
DISK_SIZE=$(df -h / | tail -1 | awk '{ print $2 }' | sed 's/G//g')
DISK_SPACE_PRE=$(df -h / | tail -1 | awk '{ print $4 }' | sed 's/G//g')
# Clean up apt packages
echo "Clean up apt packages.."
apt autoremove --purge -y
echo "Done."
echo
echo "Clean up apt cache.."
apt clean -y
echo "Done."
echo
echo "Cleanup logging.."
journalctl --vacuum-time="$LOG_DAYS"d
echo "Done."
echo
echo "Cleanup kernels.."
# Cleanup Kernels
PVE_KERNELS=$( dpkg --list | awk '/ii/{ print $2}' | grep -E -i --color '^pve-kernel-([5-9]\.([0-9][0-2]?))(\.([0-9]{1,2}))?(-[0-9]-pve)?$' )
# If PVE_KERNELS is not empty, remove them
if [ -n "$PVE_KERNELS" ]; then
echo "Removing PVE_KERNELS"
for KERNEL in $PVE_KERNELS; do
echo "Removing $KERNEL"
apt remove -y "$KERNEL"
done
echo "Done."
echo
fi
echo "Done."
echo
echo "Cleanup old snaps.."
# Cleanup snap apps if snap is installed
if [ -x "$(command -v snap)" ]; then
echo "Removing old snap revisions"
set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
while read snapname revision; do
snap remove "$snapname" --revision="$revision"
done
fi
echo "Done."
echo
DISK_SPACE_POST=$(df -h / | tail -1 | awk '{ print $4 }' | sed 's/G//g')
# Calculate disk space freed up
DISK_SPACE_FREED=$(echo "$DISK_SPACE_POST - $DISK_SPACE_PRE" | bc)
echo "====================================================================="
echo " DISK SIZE: $DISK_SIZE GB"
echo " STARTING DISK FREE: $DISK_SPACE_PRE GB"
echo " FINAL DISK FREE: $DISK_SPACE_POST GB"
echo " DISK SPACE FREED: $DISK_SPACE_FREED GB"
echo "====================================================================="
echo
exit 0