add update.sh: OS detection + package list update
All checks were successful
Check scripts syntax / check-scripts-syntax (push) Successful in 41s
All checks were successful
Check scripts syntax / check-scripts-syntax (push) Successful in 41s
- New update.sh: detects Alpine, Arch, Debian/Ubuntu, RHEL/Fedora/CentOS,
openSUSE, Void, Gentoo and updates the package index only (no upgrades)
- clean.sh fixes:
- Fix ((cleaned_count++)) exiting under set -e on first successful rm
- Stop recursively deleting /var/cache and /root/.cache wholesale
- Add CACHE_RETENTION_DAYS (7d) — clean_directory now only deletes
files older than retention period instead of wiping everything
- Remove dangerous rm -rf fallback in clean_directory
- Narrow CACHE_DIRS to /var/cache/apt instead of /var/cache
- Fix /home/*/.cache pattern to only target pip/npm caches
This commit is contained in:
52
clean.sh
52
clean.sh
@@ -20,8 +20,9 @@ readonly YELLOW='\033[1;33m'
|
||||
# Cleanup configuration
|
||||
readonly LOG_RETENTION_DAYS=30
|
||||
readonly JOURNAL_RETENTION_DAYS=7
|
||||
readonly CACHE_RETENTION_DAYS=7
|
||||
readonly TEMP_DIRS=("/tmp" "/var/tmp")
|
||||
readonly CACHE_DIRS=("/var/cache" "/root/.cache")
|
||||
readonly CACHE_DIRS=("/var/cache/apt" "/root/.cache")
|
||||
|
||||
#==============================================================================
|
||||
# UTILITY FUNCTIONS
|
||||
@@ -56,6 +57,7 @@ get_dir_size() {
|
||||
}
|
||||
|
||||
# Safe directory cleanup with size reporting
|
||||
# Deletes files older than CACHE_RETENTION_DAYS instead of wiping everything
|
||||
clean_directory() {
|
||||
local dir="$1"
|
||||
local description="$2"
|
||||
@@ -74,16 +76,26 @@ clean_directory() {
|
||||
|
||||
log_step "$description (was $size_before)..."
|
||||
|
||||
# Use find with -delete for safer cleanup
|
||||
if find "$dir" -mindepth 1 -delete 2>/dev/null; then
|
||||
log_success "$description: freed $size_before"
|
||||
else
|
||||
# Fallback to rm if find fails
|
||||
if rm -rf "$dir"/* 2>/dev/null; then
|
||||
log_success "$description: freed $size_before"
|
||||
# Use find to delete files older than retention period
|
||||
# -type f -mtime +N: only files older than N days (not directories)
|
||||
local deleted=false
|
||||
if find "$dir" -type f -mtime +"${CACHE_RETENTION_DAYS:-7}" -delete 2>/dev/null; then
|
||||
deleted=true
|
||||
fi
|
||||
|
||||
# Clean up empty directories left behind (but don't remove the dir itself)
|
||||
find "$dir" -mindepth 1 -type d -empty -delete 2>/dev/null || true
|
||||
|
||||
if $deleted; then
|
||||
local size_after
|
||||
size_after=$(get_dir_size "$dir")
|
||||
if [[ "$size_after" == "$size_before" ]]; then
|
||||
log_info "$description: nothing old enough to clean (retention: ${CACHE_RETENTION_DAYS:-7}d)"
|
||||
else
|
||||
log_warning "$description: partial cleanup completed"
|
||||
log_success "$description: freed (was $size_before, now $size_after)"
|
||||
fi
|
||||
else
|
||||
log_warning "$description: partial cleanup completed"
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -269,13 +281,19 @@ cleanup_cache_dirs() {
|
||||
fi
|
||||
done
|
||||
|
||||
# Clean additional cache locations
|
||||
# Clean safe cache locations only
|
||||
# NOTE: We do NOT recursively delete /var/cache or /root/.cache as a whole,
|
||||
# as that can break package managers and remove important data.
|
||||
local additional_caches=(
|
||||
"/var/lib/apt/lists"
|
||||
"/var/cache/apt/archives"
|
||||
"/var/cache/apt/archives/partial"
|
||||
"/var/cache/debconf"
|
||||
"/var/lib/apt/lists"
|
||||
"/root/.npm"
|
||||
"/root/.pip"
|
||||
"/home/*/.cache"
|
||||
"/root/.cache/pip"
|
||||
"/root/.cache/npm"
|
||||
"/home/*/.cache/pip"
|
||||
"/home/*/.cache/npm"
|
||||
"/home/*/.npm"
|
||||
"/home/*/.pip"
|
||||
)
|
||||
@@ -300,12 +318,16 @@ cleanup_logs() {
|
||||
|
||||
# Find and remove old log files
|
||||
while IFS= read -r -d '' logfile; do
|
||||
rm -f "$logfile" 2>/dev/null && ((cleaned_count++))
|
||||
if rm -f "$logfile" 2>/dev/null; then
|
||||
((++cleaned_count)) || true
|
||||
fi
|
||||
done < <(find /var/log -type f -name "*.log" -mtime +"$LOG_RETENTION_DAYS" -print0 2>/dev/null || true)
|
||||
|
||||
# Clean compressed logs
|
||||
while IFS= read -r -d '' logfile; do
|
||||
rm -f "$logfile" 2>/dev/null && ((cleaned_count++))
|
||||
if rm -f "$logfile" 2>/dev/null; then
|
||||
((++cleaned_count)) || true
|
||||
fi
|
||||
done < <(find /var/log -type f \( -name "*.log.gz" -o -name "*.log.bz2" -o -name "*.log.xz" \) -mtime +"$LOG_RETENTION_DAYS" -print0 2>/dev/null || true)
|
||||
|
||||
if [[ $cleaned_count -gt 0 ]]; then
|
||||
|
||||
Reference in New Issue
Block a user