Skip to content

Instantly share code, notes, and snippets.

@Xeroday
Created July 21, 2013 22:27
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save Xeroday/6050249 to your computer and use it in GitHub Desktop.
Save Xeroday/6050249 to your computer and use it in GitHub Desktop.
Deletes .tar.gz files older than 7 days.
<?php
$files = glob("*.tar.gz");
foreach($files as $file) {
if(is_file($file)
&& time() - filemtime($file) >= 7*24*60*60) { // 7 days
unlink($file);
}
}
?>
@TurnKeyMSP
Copy link

TurnKeyMSP commented May 25, 2017

Have had a lot of errors with this script not following the time-based rule; I am going to try it again but may have continued trouble.

For instance, if I wanted to do cleaning at 1 day, 4 hours, 20 minutes, and no seconds, I used: 01x04x20x00, 1x04x20x00, 1x4x20x00, and 1x4x20, but it was erasing them if they were there at all.


<?php $files = glob("*.tar.gz"); foreach($files as $file) { if(is_file($file) && time() - filemtime($file) >= 1*28*20*00) { // 7 days unlink($file); } } ?>

Additionally, the line '$files = glob(".tar.gz");' is not directory-aware, and should be modified with the absolute path to the directory of the backups that are to be cleaned, else lose all ".
"tar.gz" files on root.

I was able to get the script working once, but I am using it in two places with remote and local backups, and it just deletes all or none right now.

  • Tyler Scafidi
    Scafidi Enterprises

@TurnKeyMSP
Copy link

TurnKeyMSP commented Jun 19, 2017

Can you help me figure out the time counter format (i.e. 01 v 1 v 00*24 for 1 day, and 05 v 5 v 00 minutes, etc.

@RMisaki
Copy link

RMisaki commented Feb 11, 2019

Have had a lot of errors with this script not following the time-based rule; I am going to try it again but may have continued trouble.

For instance, if I wanted to do cleaning at 1 day, 4 hours, 20 minutes, and no seconds, I used: 01x04x20x00, 1x04x20x00, 1x4x20x00, and 1x4x20, but it was erasing them if they were there at all.

<?php $files = glob("*.tar.gz"); foreach($files as $file) { if(is_file($file) && time() - filemtime($file) >= 1*28*20*00) { // 7 days unlink($file); } } ?>

Additionally, the line '$files = glob(".tar.gz");' is not directory-aware, and should be modified with the absolute path to the directory of the backups that are to be cleaned, else lose all ".
"tar.gz" files on root.

I was able to get the script working once, but I am using it in two places with remote and local backups, and it just deletes all or none right now.

* Tyler Scafidi
  Scafidi Enterprises

Hi Tyler, I just noticed you probably got a little confused about the time script, for me, it looks like 7 days, multiplied for 24 hours, then for 60 minutes and then for 60 seconds, so you get the total time in seconds;
If you multiply anything by zero it's zero, so it'll delete anything higher than zero. You were probably looking for something like: 28,4 hours * 60 minutes * 60 seconds, so you'll get arround a day, four hours and some minutes converted in seconds.

If you're still in doubt, that's your answer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment