A lightweight, efficient Bash CLI utility designed to automate the cleanup of old backup archives. This tool scans the backup directory passed by the user, identifies compressed .tar.gz files older than 30 days, deletes them to save disk space, and maintains a detailed log history of all actions.
- Smart Cleanup: Automatically targets files with the
.tar.gzextension. - Time-Based Rotation: Only removes archives older than 30 days (mtime +30).
- Automated Logging: Maintains
archive-logs-history.logwithin the target directory for audit trails. - Safety Checks: Validates directory existence and input arguments before execution.
- Global Access: Designed to run as a system-wide command.
To use this tool globally from any directory, follow these steps:
-
Clone the script:
git clone https://github.com/2Kelvin/archives-cleanup.git -
Change into the directory
cd archives-cleanup -
Make the script executable
chmod +x cleanup-tool
-
Move script to global bin: Move the script to
/usr/local/binto ensure it is available in your system$PATH.sudo mv cleanup-tool /usr/local/bin/cleanup-tool
Because this tool is installed in a protected system directory and often manages backups in restricted folders, it should be executed with root privileges or from a user with sudo.
To run the tool globally, use:
sudo cleanup-tool /path/to/your/backupsIf you have backups stored in /var/backups/my-logs-backup-folder, simply run:
sudo cleanup-tool /var/backups/my-logs-backup-folder
This utility is designed for headless execution and is perfect for scheduled system maintenance. Since it is installed in /usr/local/bin, it can be easily integrated into your preferred scheduling engine; systemmd timers or cron jobs.
The tool maintains a continuous audit trail by creating or updating a log file named archive-logs-history.log directly within your target directory.
- Timestamps: Precise records of when the cleanup was initiated.
- Deletion Manifest: A specific list of every
.tar.gzfilename removed during the session. - Status Reports: Clear confirmation of "Success" or specific error codes if the process fails.
cat /path/to/your/archive-logs-history.log