Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- # $mysql_backup_dir
- # $seconds_to_save = $increments_to_save * $seconds_multiplier;
- # Just set the seconds_to_save number to
- # the correct number of seconds, i.e:
- # minute: 60 / hour: 3600 / day: 86400 / week: 604800
- #my $seconds_to_save = 604800;
- my $seconds_to_save = ( 3 * 86400 ) ; # 3 days
- # call this subroutine with the '$full_dir_name'
- my ($full_dir_name) = $ARGV[0];
- if ( not $full_dir_name ) { die "Need a directory name to clean..." };
- if ( not -e $full_dir_name ) { die "\nCould NOT Clean Old Files - $full_dir_name doesn't exist.\n" };
- print ("\nCleaning Old Files\n");
- my $save_time = time() - $seconds_to_save;
- my $deleted_files = 0;
- print ("\nRemoving Files Older than $seconds_to_save\n");
- opendir (DIRHANDLE, $full_dir_name) or die "Couldn't open $full_dir_name:$!";
- # we use $file_prefix to make it safer; we don't want to delete
- # any files except those matching the file spec
- my $file_prefix="menelvagor";
- my @filelist = grep { /^$file_prefix/ } readdir(DIRHANDLE);
- closedir (DIRHANDLE);
- my @sortlist = sort(@filelist);
- my $file_count = @sortlist;
- my $file_msg = "File Count in Backup Dir: $file_count \n\n";
- print ($file_msg);
- # loop through directory
- foreach my $infile (@sortlist) {
- my $infile_str = $infile;
- $infile = "$full_dir_name/$infile";
- my ($modtime) = (stat($infile))[9];
- if ( $modtime < $save_time ) {
- # file is older, so delete it
- my $delete_count = unlink "$infile";
- if ( ! -e $infile ) {
- $deleted_files++;
- print ("$delete_count File Removed: ($infile_str\)\n");
- }
- else { print ("\nProblem Removing File: $infile_str\n"); }
- }
- else { print ("- Keeping: $infile_str\n"); }
- } # end of file loop
- print "\nRemoved $deleted_files Files and/or Directories.\n";
- ~
- ~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement