Thread Poard: Loeschscript gesucht
(12 answers)
Opened by Strat at 2007-12-16 14:03
*) behält alle Dateien der letzten 5 Tage
*) behält alle Dateien von einem 11. oder 21. der letzten 6 Monate *) behält alle Dateien von einem 01. eines jeden Monats Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 #!/usr/bin/perl use strict; use warnings; use Date::Calc qw(Add_Delta_Days Add_Delta_YM Today); use File::Spec; use POSIX; my $directory = './'; my $logfile = './deleted_files.log'; my @files = _get_filenames( $directory ); @files = _clean_list( @files ); _delete_files( $directory, $logfile, @files ); ### # Subroutines ### sub _get_filenames{ my ($dir) = @_; opendir my $dirh, $dir or die $!; my @files = grep{ /^\w+\_\d{8}-\d{6}\.tar\.gz$/ }readdir $dirh; closedir $dirh; return @files; } sub _clean_list{ my @files = @_; my @dates; my @today = Today(); # keep today's files push @dates, sprintf "%04d%02d%02d", @today; # keep files of 5 last days for (1..4){ push @dates, sprintf "%04d%02d%02d", Add_Delta_Days( @today, $_ * -1 ); } # keep files of 11th, 21st of the last 6 months for (0..5){ my ($year,$month,$day) = Add_Delta_YM( @today, 0, $_ * -1 ); for( 11, 21 ){ push @dates, sprintf "%04d%02d%02d", $year,$month, $_; } } # filter filenames @files = grep{ my ($f) = $_ =~ /^\w+\_(\d{8})/; !( grep{ $f == $_ }@dates or $f =~ /01$/ ) }@files; } sub _delete_files{ my ($dir,$log,@files) = @_; open my $fh_log, '>>', $log or die $!; for my $file ( @files ){ my $time = POSIX::strftime( '%Y%m%d-%H%M%S', localtime); my $fqfn = File::Spec->catfile( $dir, $file ); unlink $fqfn and print $fh_log $time,"\t",$file,"\n"; } close $fh_log; } OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/) -- Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html Perl-Entwicklung: http://perl-services.de/ |