File::RsyncP - Perl Rsync client |
File::RsyncP - Perl Rsync client
use File::RsyncP;
my $rs = File::RsyncP->new({ logLevel => 1, rsyncCmd => "/bin/rsync", rsyncArgs => [ "--numeric-ids", "--perms", "--owner", "--group", "--devices", "--links", "--ignore-times", "--block-size=700", "--relative", "--recursive", "-v", ], });
# # Receive files from remote srcDirectory to local destDirectory # by running rsyncCmd with rsyncArgs. # $rs->remoteStart(1, srcDirectory); $rs->go(destDirectory); $rs->serverClose;
# # Send files to remote destDirectory from local srcDirectory # by running rsyncCmd with rsyncArgs. # $rs->remoteStart(0, destDirectory); $rs->go(srcDirectory); $rs->serverClose;
# # Receive files from a remote module to local destDirectory by # connecting to an rsyncd server. ($module is the name from # /etc/rsyncd.conf.) # my $port = 873; $rs->serverConnect($host, $port); $rs->serverService($module, $authUser, $authPasswd, 0); $rs->serverStart(1, "."); $rs->go(destDirectory); $rs->serverClose;
# # Get finals stats. This is a hashref containing elements # totalRead, totalWritten, totalSize, plus whatever the FileIO # module might add. # my $stats = $rs->statsFinal;
File::RsyncP is a perl implementation of an Rsync client. It is compatible with Rsync 2.5.5 (protocol version 26). It can send or receive files, either by running rsync on the remote machine, or connecting to an rsyncd deamon on the remote machine.
What use is File::RsyncP? The main purpose is that File::RsyncP separates all file system I/O into a separate module, which can be replaced by any module of your own design. This allows rsync interfaces to non-filesystem data types (eg: databases) to be developed with relative ease.
File::RsyncP was initially written to provide an Rsync interface for BackupPC, http://backuppc.sourceforge.net. See BackupPC for programming examples.
File::RsyncP does not yet provide a command-line interface that mimics native Rsync. Instead it provides an API that makes it possible to write simple scripts that talk to rsync or rsyncd.
The File::RsyncP::FileIO module contains the default file system access functions. File::RsyncP::FileIO may be subclassed or replaced by a custom module to provide access to non-filesystem data types.
First some background. When you run rsync is parses its command-line arguments, then it either connects to a remote rsyncd daemon, or runs an rsync on the remote machine via ssh or rsh. At this point there are two rsync processes: the one you invoked and the one on the remote machine. The one on the local machine is called the client, and the one on the remote machine is the server. One side (either the client or server) will send files and the other will receive files. The sending rsync generates a file list and sends it to the receiving side. The receiving rsync will fork a child process.
File::RsyncP does not (yet) have a command-line script that mimics rsync's startup processing. Think of File::RsyncP as one level below the command-line rsync. File::RsyncP implements the client side of the connection, and File::RsyncP knows how to run the remote side (eg, via rsh or ssh) or to connect to a remote rsyncd daemon. File::RsyncP automatically adds the internal --server and --sender options (if necessary) to the options passed to the remote rsync.
To initiate any rsync session the File::RsyncP->new function should be called. It takes a hashref of parameters:
rsyncCmd can either be a single string giving the path of the rsync command to run (eg: /bin/rsync) or a list containing the command and arguments, eg:
rsyncCmd => [qw( /bin/ssh -l user host /bin/rsync )],
or:
rsyncCmd => ["/bin/ssh", "-l", $user, $host, "/bin/rsync"],
Also, rsyncCmd can also be set to a code reference (ie: a perl sub).
In this case the code is called without arguments or other processing.
It is up to the perl code you supply to exec()
the remote rsync.
This option is ignored if you are connecting to an rsyncd daemon.
This option is ignored if you are connecting to an rsyncd daemon.
This can be replaced with a new module of your choice, or you can subclass File::RsyncP::FileIO.
alarm()
and it is the caller's responsbility to catch the
alarm signal.
An example of calling File::RsyncP->new is:
my $rs = File::RsyncP->new({ logLevel => 1, rsyncCmd => ["/bin/rsh", $host, "-l", $user, "/bin/rsync"], rsyncArgs => [ "--numeric-ids", "--perms", "--owner", "--group", "--devices", "--links", "--ignore-times", "--block-size=700", "--relative", "--recursive", "-v", ], });
A fuller example showing most of the parameters and qw()
for the
rsyncArgs is:
my $rs = File::RsyncP->new({ logLevel => 1, rsyncCmd => ["/bin/rsh", $host, "-l", $user, "/bin/rsync"], rsyncArgs => [qw( --numeric-ids --perms --owner --group --devices --links --ignore-times --block-size=700 --relative --recursive -v )], logHandler => sub { my($str) = @_; print MyHandler "log: $str\n"; }; fio => File::RsyncP::FileIO->new({ logLevel => 1, });
});
File::RsyncP can talk to a remote rsync using this sequence of functions:
If the client is receiving files from the server then remoteSend should be non-zero and remoteDir is the source directory on the remote machine. If the client is sending files to the remote server then remoteSend should be zero and remoteDir is the destination directory on the remote machine. Returns undef on success and non-zero on error.
go(localDir)
serverClose()
go()
to finish up. Returns undef on success.
statsFinal()
An example of sending files to a remote rsync is:
# # Send files to remote destDirectory from local srcDirectory # by running rsyncCmd with rsyncArgs. # $rs->remoteStart(0, destDirectory); $rs->go(srcDirectory); $rs->serverClose;
An example of receiving files from a remote rsync is:
# # Receive files from remote srcDirectory to local destDirectory # by running rsyncCmd with rsyncArgs. # $rs->remoteStart(1, srcDirectory); $rs->go(destDirectory); $rs->serverClose;
File::RsyncP can connect to a remote Rsync daemon using this sequence of functions:
See the rsyncd.conf manual page for more information. For example, if a host called navajo had a /etc/rsyncd.conf contains these lines:
[test] path = /data/test comment = test module auth users = craig, celia secrets file = /etc/rsyncd.secrets
and /etc/rsyncd.secrets contained:
craig:xxx
then you could connect to this rsyncd using:
$rs->serverConnect("navajo", 873); $rs->serverService("test", "craig", "xxx", 0);
The value of the authRequired argument doesn't matter in this case.
On error serverService returns a string error message. On success it returns undef.
go(localDir)
serverClose()
go()
to finish up. Returns undef on success.
An example of sending files to a remote rsyncd daemon is:
# # Send files to a remote module from a local srcDirectory by # connecting to an rsyncd server. ($module is the name from # /etc/rsyncd.conf.) # my $port = 873; $rs->serverConnect($host, $port); $rs->serverService($module, $authUser, $authPasswd); $rs->serverStart(0, "."); $rs->go(srcDirectory); $rs->serverClose;
An example of receiving files from a remote rsyncd daemon is:
# # Receive files from a remote module to local destDirectory by # connecting to an rsyncd server. ($module is the name from # /etc/rsyncd.conf.) # my $port = 873; $rs->serverConnect($host, $port); $rs->serverService($module, $authUser, $authPasswd); $rs->serverStart(1, "."); $rs->go(destDirectory); $rs->serverClose;
The initial version of File::RsyncP (0.10) has a number of limitations:
--numeric-ids --perms|-p --owner|-o --group|-g --devices|D --links|-l --ignore-times|I --block-size=i --verbose|-v --recursive|-r --relative|-R
Hardlinks are currently not supported. Other options that only affect the remote side (eg: --exclude or --include when receiving files from the remote) will work correctly since they are passed to the remote Rsync unchanged.
File::RsyncP::FileList was written by Craig Barratt <cbarratt@users.sourceforge.net> based on rsync 2.5.5.
Rsync was written by Andrew Tridgell <tridge@samba.org> and Paul Mackerras. It is available under a GPL license. See http://rsync.samba.org
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License in the LICENSE file along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
See http://perlrsync.sourceforge.net for File::RsyncP's SourceForge home page.
See the File::RsyncP::FileIO manpage, the File::RsyncP::Digest manpage, and the File::RsyncP::FileList manpage.
Also see BackupPC's lib/BackupPC/Xfer/Rsync.pm for other examples.
File::RsyncP - Perl Rsync client |