for testing create the folders
mkdir /tmp/{src1,src2,src3,dest1,dest2,dest3}
So, here it is
Thanks, George, based on your hints, I use the following one for my case:
-----------
#!/usr/bin/perl -w
# example of parallel rsync
use strict;
use warnings;
use feature 'say';
foreach (1..3) {
mkdir('/tmp/src'.$_, 0777);
}
my $parallel_jobs = 2;
my $rsync_path = '/usr/local/bin/rsync';
my %rsync_jobs =(
'test rsync 1' => {
'local folder' => '/tmp/src1',
'remote server' => 'ftp.cn.debian.org',
'remote folder' => '::debian',
'rsync switches' => '-a -z -v -P --partial --delete' },
'test rsync 2' => {
'local folder' => '/tmp/src2',
'remote server' => 'ftp.cn.debian.org',
'remote folder' => '::debian',
'rsync switches' => '-a -z -v -P --partial --delete' },
'test rsync 3' => {
'local folder' => '/tmp/src3',
'remote server' => 'ftp.cn.debian.org',
'remote folder' => '::debian',
'rsync switches' => '-a -z -v -P --partial --delete' } );
# Some quick checks
die "Could not found executable \"$rsync_path\" for user \"".
getpwuid($>) ."\"\n" unless -x $rsync_path;
-d $rsync_jobs{$_}->{'local folder'} || die "Could not found the local
source readable directory \"$rsync_jobs{$_}->{'local folder'}\" for user
\"". getpwuid($>) ."\", for the rsync job \"$_\"\n" for keys %rsync_jobs;
# Create job chunks with members as many as the $parallel_jobs
my %chunk = (id=>1, data=>[]);
foreach (sort {$a cmp $b} keys %rsync_jobs)
{
if ($parallel_jobs == scalar @{$chunk{data}})
{
&Parallel_execution_of_chunk;
$chunk{data}=[];
$chunk{id}++
}
push @{$chunk{data}}, $_
}
&Parallel_execution_of_chunk;
sub Parallel_execution_of_chunk
{
say "Starting jodid $chunk{id} of ". scalar @{$chunk{data}} ." parallel
jobs";
my @Threads;
$|=1;
for my $chunk (@{$chunk{data}})
{
my $answer = fork;
die "Perl process $$ Could not fork\n" unless defined $answer;
if ( $answer == 0 )
{
print "rsync thread $$ started\n";
my $command = "$rsync_path $rsync_jobs{$chunk}->{'rsync
switches'}
$rsync_jobs{$chunk}->{'local folder'}/ $rsync_jobs{$chunk}->{'remote
server'}:$rsync_jobs{$chunk}->{'remote folder'}";
open SHELL, '-|', "$command 2>&1" or die "Could not
run rsync :
\"$command\" because $? , $^N\n";
while (<SHELL>) { print "jodid $chunk{id} , rsync \"$chunk
\" $_" }
close SHELL;
exit 0
}
else
{
push @Threads, $answer
}
}
print "Waiting the tids: @Threads\n";
foreach my $tid (@Threads) { waitpid($tid, 0); print "Thread $tid
finished\n" }
}
-----------
But when I run it with the following command:
werner@debian:~$ ./prsync.pl
I meet the following errors:
....
jodid 1 , rsync "test rsync 2" sh: 2: /tmp/src2/: Permission denied
jodid 1 , rsync "test rsync 1" sh: 2: /tmp/src1/: Permission denied
Thread 5490
finished
Thread 5491
finished
Starting jodid 2 of 1 parallel
jobs
Waiting the tids: 5498
rsync thread 5498 started
Use of uninitialized value in concatenation (.) or string at ./prsync.pl
line 78.
.....
rsync error: syntax or usage error (code 1) at main.c(1622)
[Receiver=3.1.1pre1]
jodid 2 , rsync "test rsync 3" sh: 2: /tmp/src3/: Permission denied
Thread 5498
finished
Regards