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...
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.
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)
I assume the above should have been [snipped].correct
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
2. Step through list of files sorting into a list that we can then retrieve all but the 30 newestalso 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?
# 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?
If that is correct then I should change the above to something like... my @remote_list = $ftp->ls("*-www.tar.gz");
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?
http://danconia.org
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>