Search code examples
perlrsync

How to get the amount of data transfered with Perl File::Rsync


I used to tar ball and SFTP my backups but they are growing to large and I'm needlessly transferring data I already have a copy of so decided to update my code to utilize rsync via File::Rsync and it's delta algorithm. I can successfully transfer the data as required but I can't seem to find a way to record the amount of data that was transferred. Previously I could stat the generated tar but since I'm not likely moving all files in the source directory how can I know the amount moved?

The destination parent directory may not exist so it is created (mkdir -p /path/to/create) via Net::SSH::Perl, point being I can run cmds against the destination before or after rsync.

I will include my code, I doubt it's actually useful but if I don't someone will just ask for it anyway. Suggestions anyone?

use File::Rsync;

$obj = File::Rsync->new( { verbose => 1,
                           archive => 1,
                          compress => 1,
                         recursive => 1,
                               rsh => '/usr/bin/ssh -i /user/.ssh/id_rsa',
                            delete => 1,
                      'rsync-path' => '/usr/bin/rsync'

       } );

$obj->exec( { src => "$backupwhat",
             dest => "host.co.uk:/remote_backup/$backupto"
             } ) or warn "rsync failed\n"; 

Solution

  • At the end of the day, File::Rsync is rather simplistic wrapper on top of rsync command (almost to the point of being pointless, in my humble opinion).

    You may just as well bypass it and just use rsync directly. In this case, if you add rsync option --stats, it will print information you are looking for at the end of transfer process. All you have to do is to parse output using regex to get your data, something like this:

    my $output = qx{rsync -av --delete --stats "$backupwhat" "host.co.uk:/remote_backup/$backupto"};
    warn("rsync failed?") if ($? / 256);
    my ($sent)     = ($output =~ /Total bytes sent: (\d+)/);
    my ($received) = ($output =~ /Total bytes received: (\d+)/);