Using Puppet’s “tidy” resource to clean up old files

This will be a short one, I promise!

Let’s say you’re managing backups on a host and only want to keep the last couple of days worth of snapshots. You could construct some find..xargs shenanigans to do the job, but why not manage that with Puppet? This is what tidy is for.

This snippet of Puppet will clean every directory / file out of /srv/backups that is 3 days or older. At least… that’s what it says it does. Let’s check out the contents of the backups directory after a successful Puppet run:

Why is it keeping all those older (and empty) directories!? I told it to rmdirs.

Well, it turns out that tidy uses atime rather than mtime to determine whether or not it’s time to age-out a file. atime represents the “last time a file / directory was accessed” (rather than modified). When Puppet comes through to determine what needs to be cleaned up, it reads each file (bumping the atime). Check it out:

Luckily, the tidy resource has a type parameter we can use to specify that it use “mtime” rather than “atime”. Updating our resource to the following will work as expected:

Easy!