#!/usr/bin/perl # syncsite.pl - by Davidcopperfield # mirrors a site with Net::FTP # usage: ./syncsite.pl mysite.server.net username pass /home/user/site use Net::FTP; use strict; #Server my $server = $ARGV[0]; #Username my $user = $ARGV[1]; #Password my $pass = $ARGV[2]; #Directory on localhost to download to my $localdir = $ARGV[3]; my @files; #Files in current directory my @dirs = '/'; #Directories #FTP client my $ftp = Net::FTP->new($server, Debug => 0, passive => 1) or die "Cannot connect to $server : $@"; #Login to remotehost $ftp->login($user,$pass) or die "Cannot login: $@"; #Binary transfer mode $ftp->binary; #Get all the files #Get all files in root directory and begin directory array foreach my $dir (@dirs) { #Switch to next directory on list $ftp->cwd($dir); #Make the directory on localhost mkdir "$localdir/$dir"; #List all files in this directory my @files = $ftp->ls; #Loop through files foreach my $file (@files) { #Check if they are "normal" files (Assumes all files with no extension are directories) if ($file=~/.*\..*/) { #Get file to corresponding place on localhost print "Getting: $file to $localdir/$dir\n"; $ftp->get($file,"$localdir/$dir/".$file); } else { #Found a directory, add it to the list with its path print "Making directory: $file\n"; push @dirs, $ftp->pwd.$file; } } } #Close connection $ftp->quit;