Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Net::FTP backup remote machine

by mandog (Curate)
on Oct 26, 2001 at 04:07 UTC ( [id://121577]=perlcraft: print w/replies, xml ) Need Help??

   1: This very simple script is used to backup a 
   2: remote directory of tarred and zipped files. 
   3: 
   4: I'd be curous about better ways to do this. 
   5: In particular, I'm interested in a better 
   6: way to test if a remote file is a directory 
   7: or a file or a better way to determine if a file has already been copied<P>
   8: 
   9: #~/usr/local/bin/perl -w
  10: 
  11: use strict;
  12: use Net::FTP;
  13: 
  14: my $host='myhost.org';
  15: my $uname='backup';
  16: my $pword='really_secure';
  17: 
  18: my $rdir='/backup';
  19: my $ldir='D:/archives/brave-backup';
  20: 
  21: 
  22: #########################################
  23: #                                       #
  24: # no user server servicable parts below #
  25: #                                       #
  26: #########################################
  27: 
  28: my $ftp = Net::FTP->new($host, Debug => 0,
  29: 			Passive =>1, Hash=>1) 
  30: 	or die("$@ couldn't construct ftp object");
  31: 
  32: print "logging on to $host\n";
  33: $ftp->login($uname,$pword)
  34: 	or die("couldn't login to $host, as $uname\n");
  35: 
  36: $ftp->cwd($rdir)
  37: 	or die("couldn't cd to remote dir: $rdir\n");
  38: 
  39: my @r_files=$ftp->ls()
  40: 	or die("couldn't cd to list dir: $rdir\n");
  41: 
  42: chdir($ldir)
  43: 	or die("couldn't open to local dir: $ldir\n");
  44: 
  45: opendir(DH,'.')
  46: 	or die("couldn't cd to local dir: $ldir\n");
  47: 
  48: my @l_files=readdir(DH) 
  49: 	or die ("couldn't list local dir: $ldir\n");
  50: 
  51: closedir(DH) 
  52: 	or die("couldn't close local dir: $ldir");
  53: 
  54: my %backed_up_files;
  55: foreach (@l_files){
  56: 	$backed_up_files{$_}=1;
  57: }
  58: 
  59: $ftp->binary()
  60: 	or die("couldn't set mode to BINARY\n");
  61: foreach(@r_files){
  62: 	
  63: 	if (!$backed_up_files{$_} && !Is_Dir($_)){
  64: 		print "\tgetting $_\n";
  65: 		$ftp->get($_)
  66: 			or die("couldn't get remote file: $_");
  67: 	}
  68: }
  69: 
  70: $ftp->quit;
  71: 
  72: sub Is_Dir($){
  73: 	my $maybe_dir=shift;
  74: 	my $result=$ftp->cwd($maybe_dir);
  75: 	$ftp->cwd($rdir); #return to our original dir
  76: 	return $result;
  77: }

Replies are listed 'Best First'.
Re: Net::FTP backup remote machine
by mikeB (Friar) on Oct 26, 2001 at 21:42 UTC
    I have a similar script here. It uses the -e file test operator to check for the local file. It also uses the -s file test operator to get the size of the local file after the transfer and compares it to the size of the remote file as a sanity check against file system problems, etc.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-25 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found