<?php
/* 	REMold.php v1.0 will scan given path for files or directories 
	(directories are not scanned recursively and calculated as file)
	that are older then a give time span (set at $max_days_old) AND
	deletes them untill the path (du -h -s) size is lower then $max_archive_size
	If the directory usage of a given $dir lower then $max_archive_size the script
	will NOT delete older (then $max_days_old) files.
	REMold.php by Dmitry Sherman dmitry@interhost.co.il
*/

//	I suggest to put this script in daily crontab:
//	0 0 * * * php /home/user/remold.php > /dev/null

$dir = "/home/iron/";	//backups path
$max_archive_size = '20000';	//max backups directory size in MEGABYTES. 20000 = 20GB
$max_days_old = '14';	//max days old backup. 14 = 2 weeks
$check_ext = '1';	//check file with extensions. Usually you will NOT want to change it.
					

/*Do not edit below */

$handle=opendir($dir);

while ($file = readdir($handle)) 
{
	if ($file != "." && $file != "..") //do not check UNIX "." & ".." files
	{
	
	$ext=explode(".",$file);
		if (!$ext[1] || $check_ext and $ext[1] != 'php')  
		// exclude files without extensions (if $check_ext isn't set) and php files
		{
			$du=getDirectorySize($dir); //get disk usage of $dir path
$du_form = sizeFormat($du['size']); //human format of du
$du_megs = $du['size'] / 1024 / 1024 ; //calc dir usage in megabytes instead of bytes.
$diff = mktime() - filemtime($file); //calc time-span difference.
$diff /= 60; $diff /= 60; $diff /= 24; //calc time-span in days instead of seconds.
echo $file."=".round($diff)." days old.\n";
echo $du_form."\n";
			if ($diff > $max_days_old && $du_megs > $max_archive_size) //condition for deleting
			{ 
				echo "removing old $file \n"; 
				$cmd = 'rm -rf '.$file;
				shell_exec($cmd); 
			}
		}
	}	

}

closedir($handle);

function getDirectorySize($path) 
{ 
  $totalsize = 0; 
  $totalcount = 0; 
  $dircount = 0; 
  if ($handle = opendir ($path)) 
  { 
    while (false !== ($file = readdir($handle))) 
    { 
      $nextpath = $path . '/' . $file; 
      if ($file != '.' && $file != '..' && !is_link ($nextpath)) 
      { 
        if (is_dir ($nextpath)) 
        { 
          $dircount++; 
          $result = getDirectorySize($nextpath); 
          $totalsize += $result['size']; 
          $totalcount += $result['count']; 
          $dircount += $result['dircount']; 
        } 
        elseif (is_file ($nextpath)) 
        { 
          $totalsize += filesize ($nextpath); 
          $totalcount++; 
        } 
      } 
    } 
  } 
  closedir ($handle); 
  $total['size'] = $totalsize; 
  $total['count'] = $totalcount; 
  $total['dircount'] = $dircount; 
  return $total; 
} 

function sizeFormat($size) 
{ 
    if($size<1024) 
    { 
        return $size." bytes"; 
    } 
    else if($size<(1024*1024)) 
    { 
        $size=round($size/1024,1); 
        return $size." KB"; 
    } 
    else if($size<(1024*1024*1024)) 
    { 
        $size=round($size/(1024*1024),1); 
        return $size." MB"; 
    } 
    else 
    { 
        $size=round($size/(1024*1024*1024),1); 
        return $size." GB"; 
    } 

}
?>

