0byt3m1n1
Path:
/
data
/
16
/
inventory
/
[
Home
]
File: bkupclean.pl
#!/usr/local/bin/perl $file = $ARGV[0]; chomp($file); open LIST, "<${file}"; @FILEPARTS = (split /\// ,$file); $file = pop(@FILEPARTS); open BKUPS, ">/data/16/inventory/done/Backups/bkups-${file}"; #REsults file open LOGS, ">/data/16/inventory/done/Logs/logs-${file}"; #REsults file $bkcnt = 0; $bksum = 0; $logcnt = 0; $logsum = 0; while(<LIST>) { $dir = $_; chomp($dir); ($bkcnt, $bksum) = backup($dir,$bkcnt,$bksum) if ($dir =~ /backup/); ($logsum, $logcnt) = logcheck($dir,$logcnt,$logsum) if ($dir =~ /meta/); ($logsum, $logcnt) = logcheck($dir,$logcnt,$logsum) if ($dir =~ /htdocs/); }#End loop of all sites found. sub backup { $backup = $_[0]; $bkcnt = $_[1]; $bksum = $_[2]; if (-e $backup) { $bkupsize = 0; $bkupsize = sprintf("%.4f", ((lstat($backup))[7])/1024/1024); print BKUPS "$bkupsize,$backup\n"; #rm goes here; $bksum = $bksum + $bkupsize; $bkcnt ++; } return($bkcnt, $bksum); } sub logcheck { $logs = $_[0]; $logcnt = $_[1]; $logsum = $_[2]; chomp($logs); if (-e $logs) { opendir(LOGS, $logs); #Build a list of log files. @LOGS = readdir(LOGS); @LOGS = (grep(!/^(\.)/, @LOGS)); closedir(LOGS); foreach(@LOGS) { #Loop through the logs $logfile = $_; chomp($logfile); $logpath = "$logs/$logfile"; #This is the full path to the log file. if ((-f $logpath) && (! -l $logpath) && (! -d $logpath) && ((-C $logpath) > 29) && (($logpath =~ /(\/data\/(\d+\/){5}user\/\d+\/)(htdocs\/log\/((access_log\.)|(error_log\.)).*)/) || ($logpath =~ /(\/data\/(\d+\/){5}meta\/\d+\/)logs\/((access_log\.)|(error_log\.)).*/))) { #Check that we're working on a file, not a link, older than 29 days, and that it at least looks sorta like a log. $logsize = sprintf("%.4f", ((lstat($logpath))[7])/1024/1024); #`rm -rf $logpath`; print LOGS "$logsize,$logpath\n"; $logsum = $logsum + $logsize; $logcnt ++; } #End if } #End loop of individual logs. } #End logs IF return ($logsum, $logcnt); } #End sub logcheck. $gb = "GB"; $bksum = sprintf("%.4f", $bksum/1024); print BKUPS "Deleted $bkcnt backup files totaling $bksum$gb\n"; $logsum = sprintf("%.4f", $logsum/1024); print LOGS "Deleted $logcnt log files totaling $logsum$gb\n"; #print STATUS "Found a total of $htcnt sites\n"; close LOGS; close BKUPS;