For the case 14824, we need a script that will put into P3:
- detect file size in a directory
- if file size exceed threshold, delete the file.
- the script will run from crontab hourly.
- the program not able to delete any file in /usr /lib /bin /sbin /boot
Usage: delfilebz.pl
The attachment is the sample program that delete file base on days.
v3上面有一些文件,时间长了会变大,当超过一定大小时要求系统自动删除。
#!/usr/bin/perl
#$filepath="/var/squid/tmp/dgvirus/";
#$days="2";
#($filepath, $days) = @ARGV;
$filepath= $ARGV[0]; $size= $ARGV[1]; $size*=1024; $size*=1024;
# testing if command line is incomplete
if ($filepath eq "") { die "You have to specify a path, where the deleting should start\n"; } if ($size eq "") { die "You have to specify a number of size to define old files\n"; }
# completing filepath if just a . was given
if ($filepath eq ".") { $filepath = `pwd`; chomp($filepath); }
# no deleting on systemfolders
@systemfolders = qw(/ /bin /boot /home /initrd /lib /mnt /proc /root /sbin /sys /var /usr); foreach $word (@systemfolders) { if ($filepath eq $word) { die "No deleting on systemfolders!\n"; } if ($filepath eq $word . "/") { die "No deleting on systemfolders!\n"; } }
# saving directory in a tar-file if wanted and asking where to put the tar-file
#print "Do you want to save the old files in a tar-file before deleting?\n";
#print "y/n?\n";
$answer ="n"; chomp ($answer);
if ($answer =~ /y/i) { print "Insert the path, where you want to put the tarfile\n"; $answer2 = <STDIN>; chomp($answer2); if ($answer2 eq ".") { $answer2 = `pwd`; chomp($answer2); } $returnpath = `pwd`; chomp($returnpath); chdir($answer2) || die "Cannot jump to your inserted path for the tarfile: $!"; chdir ($returnpath); print "The tar-file named savedfiles.tar is put to $answer2\n"; }
@field = (); $i = 0; $n = 1;
# calling subroutine to open the given directory and redoing this for subdirectories
openbla($filepath); { @field2 = @field; @field = (); foreach(@field2) { openbla($_); } if (@field != ()) { redo; } } # printing the final number of deleted files for information
print `date`."$i files deleted\n";
# subroutine to open a directory. testing of each item in the directory if it
# is a subdirectory or a plain file. subdirectories are pushed in an array for
# recursive deleting. plain files are deleted. saving files before deleting in
# a tarfile if wanted
if (@ARGV<2) { die "not enough arument! usage: perl delfilebz.pl .\n"; } sub openbla { my($fpath) = @_; chdir($fpath) || die "Cannot change to given directory: $!"; opendir(DIR, ".") || die "Cannot open directory: $!"; foreach(readdir(DIR)) { if (!($_ =~ /^\.$/)) { if (!($_ =~ /^\..$/)) { if (-d $_) { $path = `pwd`; chomp($path); $path = $path . "/" . $_; push(@field, $path); } if (-f $_) { if (-s $_ > $size) { if ($answer =~ /y/i) { my($tarsource) = `which tar`; chomp($tarsource); if ($n == 2) { system "$tarsource rfv $answer2/savedfiles.tar $_"; } if ($n == 1) { system "$tarsource cfv $answer2/savedfiles.tar $_"; $n = 2; } } unlink($_) || warn "Having trouble deleting $_: $!"; $i++; } } } } } closedir(DIR) || die "Cannot close directory: $!"; }
|
阅读(279) | 评论(0) | 转发(0) |