HEX
Server: Apache
System: Linux br1102.hostgator.com.br 5.14.0-687.17.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Mon Jun 22 07:21:26 EDT 2026 x86_64
User: rkcent87 (3754)
PHP: 8.3.32
Disabled: NONE
Upload Files
File: //usr/share/doc/perl-Net-OpenSSH/examples/keep_in_sync.pl
#!/usr/bin/perl

use strict;
use warnings;

# This script monitorizes a directory using inotify and when some
# change is detected, uses rsync to update a remote copy.
#
# See: http://stackoverflow.com/q/6781104/124951


use Net::OpenSSH;
use Linux::Inotify2;
use Time::HiRes qw(sleep);

my $usage = "Usage:\n  $0 local_dir [user\@]host remote_dir\n\n";

@ARGV == 3 or die $usage;
my ($local, $host, $remote) = @ARGV;

-d $local or die $usage;

my $ssh = Net::OpenSSH->new($host);
$ssh->error and die "unable to connect to remote host: " . $ssh->error;

my $inotify = Linux::Inotify2->new;
$inotify->watch ($local, IN_MODIFY|IN_MOVED_TO);

$ssh->rsync_put({verbose => 1, glob => 1}, "$local/*", $remote);

while (1) {
    my @events = $inotify->read or die "read error: $!";
    my %changed;
    $changed{"$local/$_->{name}"} = 1 for @events;
    $ssh->rsync_put({verbose => 1}, keys %changed, $remote);
    sleep 0.1;
}