MFDoom has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I'm a newbie when it comes to perl. Just messed around with it really, nothing major. What I'm trying to do now is make a script with Net::FTP the will download everything from a remote site into the directory I am running the script from. Essentially I want to be able to run a script and have a full backup of my site downloaded. The problem I am having is I can't figure out how to have it download all directories and files within them, anyone with some familiarity with this module have any suggestions?

Replies are listed 'Best First'.
Re: Net::FTP Question
by sithsasquatch (Scribe) on Aug 03, 2005 at 23:54 UTC
    Seems like you would want to investigate file recursion here and here. Also, this Q and A deals with recursive processing a single directory.

    This deals with the FTP aspects, and this with retrieving online pages.

    I hope they are of some use. The Q&A and tutorial sections may also be useful. (I don't have any experience with that module, so I'm not much help there.)
Re: Net::FTP Question
by spiritway (Vicar) on Aug 04, 2005 at 04:29 UTC

    You didn't say what OS you're using. A non-Perl solution in a *nix environment would be to use wget.

      Well, the wget is a solution to what I needed to do, and works fine, but for myself, I'm still trying to figure it out with Net::FTP. This is what I have so far as far as code goes:
      #!/usr/bin/perl use warnings; use strict; use Net::FTP; my $ftp; #Net::FTP object my $is_error = 0; my $conn_success = 0; print "<?>~ Enter FTP Host: "; my $hostname = <STDIN>; chomp($hostname); while($conn_success != 1) { $ftp = Net::FTP->new($hostname,Timeout => 60, Passive => 1) or + $is_error = 1; if($is_error){ print "<!>~ Couldn't connect to $hostname."; } print "\n<->~ Connected to $hostname"; print "\n<?>~ Enter FTP username: "; my $username = <STDIN>; chomp($username); print "\n<?>~ Enter FTP password: "; my $password = <STDIN>; chomp($password); print "Trying to connect with\n <->Username: $username\n <->Pa +ssword: $password\n"; $ftp->login("$username","$password") or $is_error = 1; if($is_error){ print "\n<!>~ Login Failed!\n"; } else { print "<->~ Login Successful\n"; } $is_error = 0; $conn_success = 1; } $ftp->binary; file_dload($ftp->ls); $ftp->quit; sub file_dload { my @files = @_; foreach my $file(@files){ $ftp->get($file); #insert recursion for directories here } }
      My biggest hurdle here is I can't figure out how to code it so the ftp connection differentiates between the directories. I was thinking of using $ftp->dir and regular expressions to match entries that start with drwx, etc. But I'm not sure how I would cut that entry in my array from drwxr-xr-x 12 owner group 1536 Aug 4 13:07 dir_name to just have the dir_name, then I could just $ftp->cwd(dir_name) and start the recursive call to the subroutine