This Bash script is designed to clean up old log files, systemd journals, Docker resources, and temporary/session files on a Linux system. It can be configured via a .env file and supports automatic self-updates from a remote repository.
- Shell 100%
|
|
||
|---|---|---|
| cleanup.sh | ||
| README.md | ||
Cleanup Script
Description
This Bash script is designed to clean up old log files, systemd journals, Docker resources, and temporary/session files on a Linux system. It can be configured via a .env file and supports automatic self-updates from a remote repository.
Features
- Deletes old log files based on configurable retention time and patterns
- Cleans systemd journal logs
- Removes unused Docker images, containers, and volumes
- Cleans up session/temp files while retaining a specified number
- Supports configuration via
.envfile - Automatically checks for script updates (can be disabled)
Installation
Install the script using curl:
curl https://git.onesystems.ch/System-Tools/Cleanup/raw/branch/main/cleanup.sh | bash -s install
This will:
- Download the script to
/opt/cleanup/cleanup.sh - Set execute permissions
- Create a symlink in
/etc/cron.dailyto run daily
Configuration
After the first run, a .env file will be created in the same directory. You can modify it to configure the script:
Example .env:
MONATE_ALT=3
LOG_PFADE=("/var/log" "/opt/ddd/log")
LOG_MUSTER=("*.gz" "*.log.[0-9]*" "*.log.[0-9]*.gz")
JOURNAL_BEREINIGEN=true
JOURNAL_MAX_ALT="3months"
DOCKER_BEREINIGEN=true
SESSION_PFADE=("/var/www/app/sessions" "/opt/tempfiles")
SESSION_DATEIEN_BEHALTEN=100
SESSION_MIN_ALT_TAGE=1
ONLINE_UPDATE=true
Variable Descriptions:
MONATE_ALT: Log files older than this (in months) will be deletedLOG_PFADE: Paths to log directoriesLOG_MUSTER: File name patterns to match for deletionJOURNAL_BEREINIGEN: Whether to clean up systemd journal logsJOURNAL_MAX_ALT: Retention policy for journal logs (e.g. "3months")DOCKER_BEREINIGEN: Whether to clean up Docker resourcesSESSION_PFADE: Paths to session/temp directoriesSESSION_DATEIEN_BEHALTEN: Minimum number of session files to retainSESSION_MIN_ALT_TAGE: Minimum age (in days) for session files to be deletedONLINE_UPDATE: Enables or disables automatic update checks
Logging
- All actions are logged in
/opt/cleanup/logs/cleanup-YYYYMMDD-HHMMSS.log - Logs older than 7 days are automatically removed
Manual Execution
You can run the script manually at any time:
/opt/cleanup/cleanup.sh
Notes
- The script checks for updates from the GitLab repository and updates itself if a new version is available.
- You can disable this behavior by setting
ONLINE_UPDATE=falsein the.envfile.
License
MIT License – free for commercial and private use.