Automated Log Rotation and Archival Tool
Build a Perl tool that keeps log folders under control, meant to run every night from cron. It finds log files that are too big or too old, compresses them with gzip into dated archive folders (such as archive/2026/03/15/), and deletes archives older than the retention period you set. Everything is configured in a file, a dry-run mode shows exactly what would happen without touching anything, and every action is logged. It's careful by design: it never deletes a log until its compressed copy has been verified. You'll learn walking folders with File::Find, compression with Compress::Zlib, file dates and sizes, safe file operations, config files, and building tools that are safe to automate.
- Estimate
- ~9h
- Steps
- 6
- Completed by
- 0
- Proposed by
- codeseed.app
File::Find · Compress::Zlib
Project roadmap
- 01
Find rotation candidates
~1.5hWrite a script using File::Find to walk the configured log folders, matching files by a pattern (such as *.log), and select those larger than a size limit or older than an age limit, using stat for the size and modification time. Skip files that are already compressed, and symbolic links. Print each candidate with the reason it was selected. Done when: on a test folder with files of different sizes and dates, exactly the right files are listed.
- 02
Compress and archive
~2hCompress each candidate with Compress::Zlib (gzopen and gzwrite, reading in chunks) into archive/YYYY/MM/DD/name.log.gz, creating the folders with File::Path. Write to a temporary name first, then verify the result by decompressing it and comparing its size (or a checksum) with the original. Only after that, rename the archive to its final name and truncate or delete the original. Done when: archived logs decompress to exactly the original content.
- 03
Prune old archives
~1hDelete archived files older than the retention period (for example 30 days), then remove archive folders that became empty. Never delete anything outside the archive folder: check every path before deleting it. Done when: only archives older than the retention period are deleted.
- 04
Add a config file and dry run
~1.5hRead settings from a config file (a simple key = value format, or YAML with YAML::Tiny): the log folders, patterns, size and age limits, archive folder and retention days. Validate them, and refuse to run when a folder doesn't exist. Add --dry-run, which prints every action it would take without changing anything, and --config to choose the file. Done when: --dry-run lists the planned actions, and the files are untouched afterwards.
- 05
Log actions and prepare for cron
~1.5hLog every action (compressed, verified, deleted and skipped, with sizes) and every error with a timestamp to a log file, and print a short summary at the end. Use a lock file (flock) so two runs never overlap. Exit with a non-zero code on errors. Add an example crontab line to README.md. Done when: running the tool from cron works, and a second overlapping run exits immediately.
- 06
Test it
~1.5hWrite tests with Test::More that build a temporary folder with files of set sizes and dates (using utime to change the dates), then check selection, archiving and verification, pruning, dry run and the refusal to delete outside the archive. Done when: prove -l t/ passes.
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack