<?php
/****
_________________________________________________________________________________________________________________________
Function :
Delete old stats directories to save disc space
For each stats directory found to be older than <threshold> delete the dir.
_____________________________________________________________________________________________________________________
Install:
- Place the script in your /project home directory/html/ops directory
________________________________________________________________________________________________________________________
Inputs:
- None
_________________________________________________________________________________________________________________________
!!! IMPORTANT !!! Pre run changes:
- Set the mode variable to "scan" for a test run or "DELETE" for the real thing
- Set the $lookback threshold value you wish. Its midnight today less 'n' days as shown below. Typically 2-3 days.
_________________________________________________________________________________________________________________________
Outputs are self explanatory:
- Generally intended to give a success or fail indication followed by a list of directories deleted or scanned.
_________________________________________________________________________________________________________________________
Run instructions:
- Run using php. i.e. "php delete_old_stats_dirs.php".
- Run from your project home directory/html/ops.
_________________________________________________________________________________________________________________________
Known:
- Links are ignored on purpose - only real files are processed.
_________________________________________________________________________________________________________________________
Notes:
- PHP 5 Dev and test. FC6.
- Use at you own risk
_________________________________________________________________
Author: Ian Tighe aka Tigher. 22 April 2007.
Copyright: Project Neuron 2007
BOINC projects may use this code freely.
_________________________________________________________________________________________________________________________
*/
define("OK", 0);
define("INVALID_MODE", -3);
/////////////////////////////////////////////////////////
// YOU MUST CHANGE THE LINES BELOW FOR A REAL DELETE RUN USING A REAL LOOKBACK PERIOD OF YOUR CHOICE.
//
$mode = "DELETE"; //DELETE means delete files. scan means look and do NOT delete - a dry run as it were
$lookback = 2; //days
//
//
/////////////////////////////////////////////////////////
if (!($mode == "scan") && !($mode == "DELETE"))
{
print "The mode ($mode) you have set is not known. It must be either \"scan\" or \"DELETE\"\n";
exit ( INVALID_MODE ); //unknown mode of running
}
$dir = ".."; //works because this is run from html/ops
print "Mode = ".($mode=="scan" ? "Scan" : "Delete")."\n";
print "Looking back $lookback days and older\n";
$threshold = mktime(0, 0, 0, date("m"), date("d")-$lookback, date("Y"));
$count = stats_delete($dir, $threshold);
print "Directories deleted = $count\n";
exit (OK);
function stats_delete($dir, $threshold)
{
global $mode;
$count = 0 ;
$handle = opendir($dir);
while ( ($file = readdir($handle)) !== false)
{
if ( is_dir($dir."/".$file)
&& $file !== '..'
&& $file !== '.'
&& ( preg_match( '(stats_([0-9]){4}_(([0-9]|[0-9][0-9])_){4,4}([0-9]|[0-9][0-9]))', $file)) > 0)
{
if (($filetime = filemtime($dir."/".$file)) < $threshold)
{
$stats_handle = opendir($dir."/".$file);
while ( ($stats_file = readdir($stats_handle)) !== false)
{
if ( !is_dir($dir."/".$stats_file) && $stats_file !== '..' && $stats_file !== '.' )
{
if ($mode == "scan")
{
print "$mode : ".$dir."/".$file."/".$stats_file." would have been deleted\n";
}
elseif ($mode == "DELETE")
{
if (unlink($dir."/".$file."/".$stats_file))
{
print "$mode : ".$dir."/".$file."/".$stats_file." has been deleted\n";
}
else
{
print "There was an error trying to delete ".$dir."/".$file."/".$stats_file."\n";
}
}
else
{
print "Mode '$mode' is not recognised\n";
}
}
}
if ($mode == "scan")
{
print "$mode : Directory $dir"."/".$file." would have been deleted\n";
$count++;
}
elseif ($mode == "DELETE")
{
if (rmdir($dir."/".$file))
{
print "$mode : Directory $dir"."/".$file." has been been deleted\n";
$count++;
}
else
{
print "There was an error trying to delete ".$dir."/".$file."\n";
}
}
else
{
print "Mode '$mode' is not recognised\n";
exit (INVALID_MODE);
}
}
}
}
return $count;
}
?>