Jas wrote:
Hmmm...

Wiggins D'Anconia wrote:

Confused...

Jas wrote:

I am not sure how I would be able to accomplish this, still fairly new to using Perl.

Objective: Connect to a remote machine and list all files matching a given pattern (i.e. 02242002-www.tar.gz, where 02242004 is the day of month). Delete all files that are older than 30 days from last file listed.



What does "last file listed" mean? Last updated, last by name, or last in sort order (as your dates do not sort properly, YYYYMMDD is a better format)?



Fixed the formating of date for files that are being backed up...



This should make it easier to sort them...


>>>
Here is what I have thus far, and it works (not too shaby for a newbie).

#!/usr/bin/perl


# not too shabby except you are missing two keys... use strict; # always use warnings; # usually


When using 'use strict;' I recieve errors.
Global symbol "$ftp" requires explicit package name at ./ftp.pl line 8.


Yep and you will, which is a good thing. You need to declare your variables into a particular scope, initially adding a 'my' before them will prevent the problem. Then you can read about scoping and namespaces later.



use Net::FTP;

$ftp = Net::FTP->new("test-serv", Debug => 1)or die "Could establish conneciton to test-serv: $@";


Attention to detail is a good habit to get into, even when that means proper grammar and spelling in error conditions... "Could not" and "connection"...



Fixed, thanks... might need to take a refresher course on english =)


$ftp->login("user", 'password')or die "Could not login ", $ftp->message;

foreach my $list(glob('*-www.tar.gz')) {
 $ftp->ls($list)or die "Could not list files ", $ftp->message; }


Your 'glob' is on the local machine and you are listing the specific filename on the remote server which must be a directory, and then throwing that value away (void context)??


foreach my $del(glob('



This was the begining of trying to sort files and delete any past 30 days. (not sure, still learning)



Gotcha, but it probably shouldn't be included.


I assume the above should have been [snipped].

foreach my $file (glob('/path/to/*-www.tar.gz')) {
 $ftp->put($file) or die "Could not transfer files ", $ftp->message; }


Ok now we are putting a set of local files to a remote location.



$ftp->quit;

I suppose I need to look up how to check dates and then do some sort of eval() but I am not sure... any help or pointers to a good tutorial on this kinda thing is appreciated.
Jas



Back to your objective, assuming it is correct...


1. List files on remote machine

correct


2. Step through list of files sorting into a list that we can then retrieve all but the 30 newest

also correct


3. Step through list of files needing deletion, deleting them from the remote source.

and again, correct



'eval' should be unnecessary.


Tell us what you really are doing :-)...


Well files look like so...
20040225-www.tar.gz
20040226-www.tar.gz
20031202-www.tar.gz // older than 30 days
So after establishing a connection and listing all files that are older than 30 days it should delete them. My problem is that I am so new to perl I am not sure how I can do a 'ls' command and store all of the listed files into an array or variable where I can then loop through that to find files matching <= 30. Make sense? I just don't want to fill up the server with old backups.


my @remote_list = $ftp->ls;


So this piece of code would put my 'ls' results into a variable correct?


Particularly into an array, which is an ordered list of scalars.



# massage @remote_list to only include those files we wish to delete # perldoc -f split # perldoc -f sort

foreach my $file (@remote_list) {
  $ftp->delete($file) or warn "Cannot delete remote file", $ftp->message;
}


And this foreach loop will delete 'everything' in the @remote_list variable?


Correct.


If that is correct then I should change the above to something like...
my @remote_list = $ftp->ls("*-www.tar.gz");


No, the 'ls' method from Net::FTP does not take a string to glob, it takes an optional directory and returns a list of *all* files in that directory, no matter what. It is then up to you to step through that list and select only the ones you want.


If that is part of what I need then the perldoc -f split and sort would be how I can check the file name correct?


Yes, or a regex....


I have seen your other e-mail but not sure it is related...

In the example below I have thrown in a few new topics, you should research them using perldoc. Particularly the documentation for the modules. I have tried to keep it fairly verbose, normally you can chop it down at the cost of readability.... on to your other message....

So for now we end up with something along these lines:

-- UNTESTED --

#!/usr/local/bin/perl
use strict;
use warnings;


use Time::Local; use Net::FTP;


my $ftp = new Net::FTP ("test-serv", Debug => 1) or die "Could not establish connection to test-serv: $@";



$ftp->login("user", 'password') or die "Could not login: ", $ftp->message;



my @complete_list = $ftp->ls;



my $thirty_days_of_secs = (30 * 24 * 60 * 60); my $midnight = timelocal(0, 0, 0, (localtime)[3,4,5]); my $thirty_days_ago = $midnight - $thirty_days_of_secs;


my @remove_list; foreach my $file (@complete_list) { if ($file =~ /^(\d{8})-www\.tar\.gz$/) { my $filedate = $1; my $fileyyyy = substr($filedate, 0, 4); my $filemm = substr($filedate, 4, 2); my $filedd = substr($filedate, 6, 2);


my $filetime = timelocal(0, 0, 0, $filedd, $filemm - 1, $fileyyyy); if ($filetime < $thirty_days_ago) { push @remove_list, $file; } else { warn "Skipping recent file: $file"; } } else { warn "Skipping improper format file: $file"; } }


foreach my $file (@remove_list) {
$ftp->del($file) or warn "Cannot delete remote file ($file): " . $ftp->message;
}



-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to