#!/usr/bin/perl -w use strict; use warnings; use Net::SFTP; use Net::SFTP::Attributes; unless( @ARGV == 5) { print <<END; Proper Usage: perl sync local_path remote path user pass END exit(0); } my($local_path, $host, $remote_path, $user, $pass) = @ARGV; my(%args) = ( 'user' => $user, 'password' => $pass ); my $sftp = Net::SFTP->new($host, %args) or die "pasta\n"; die "SFTP Connection Failed\n" if $sftp->status; #read_local_dir("$local_path/"); #print "----\n"; #$sftp->ls("/"); #read_remote_dir($sftp,"$remote_path/"); sync($sftp, $local_path, $remote_path, ""); sub sync { my $sftp = shift; my $local = shift; my $remote = shift; my $path = shift; my( $loc_file, $loc_dir) = read_local_dir("$local/$path"); my( $rem_file, $rem_dir) = read_remote_dir($sftp, "$remote/$path"); while( my($lk,$lv) = each %$loc_file ) { my $flag = 0; while( my($rk,$rv) = each %$rem_file ) { if($lk eq $rk) { if($lv > $rv) { # put $lk $sftp->put("$local/$path/$lk","$remote/$path/$lk"); } elsif($lv < $rv) { # get $rk $sftp->get("$remote/$path/$lk", "$local/$path/$lk"); } $flag = 1; } } if( $flag == 0 ) { # put $lk $sftp->put("$local/$path/$lk","$remote/$path/$lk"); } } while( my($rk,$rv) = each %$rem_file ) { my $flag = 0; while( my($lk,$lv) = each %$loc_file ) { if( $lk eq $rk ) { $flag = 1; } } if( $flag == 0 ) { # get $rk $sftp->get("$remote/$path/$rk", "$local/$path/$rk"); } } while( my($lk,$lv) = each %$loc_dir ) { my $flag = 0; next if $lk =~ /^\./; while( my($rk,$rv) = each %$rem_dir ) { if( $lk eq $rk ) { sync( $sftp, $local, $remote, "$path/$lk"); $flag = 1; } } if( $flag == 0 ) { # create dir my $att = Net::SFTP::Attributes->new(Stat => [stat "$local/$path/ +$lk"]); $sftp->do_mkdir( "$remote/$path/$lk" , $att); # sync it sync( $sftp, $local, $remote, "$path/$lk" ); } } while( my($rk,$rv) = each %$rem_dir ) { my $flag = 0; next if $rk =~ /^\./; while( my($lk,$lv) = each %$loc_dir ) { if( $lk eq $rk ) { $flag = 1; } } if( $flag == 0 ) { # create dir mkdir("$local/$path/$rk"); # sync it sync( $sftp, $local, $remote, "$path/$rk" ); } } } sub read_local_dir { my $path = shift; my $file = {}; my $dir = {}; my $f; opendir DIR, $path or die "Failed to open $path: $!\n"; while($f = readdir DIR) { my(@stats) = stat "$path/$f"; if(-d "$path/$f"){ $dir->{$f} = $stats[9]; } else { $file->{$f} = $stats[9]; } } return ($file, $dir); } sub read_remote_dir { my $sftp = shift; my $path = shift; my $file = {}; my $dir = {}; $sftp->ls( $path , sub { my $ref = shift; if( $ref->{'longname'} =~ /^d/ ) { $dir->{$ref->{'filename'}} =$ref->{'a'}->mtime(); } else { $file->{$ref->{'filename'}} =$ref->{'a'}->mtime(); } }); return ($file, $dir); }

In reply to SFTP Sync by neosamuri

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.