您的位置:首页 > 产品设计 > UI/UE

perl多线程rsync备份文件到远端主机

2016-06-11 19:43 585 查看
需求:
主机上有上百G的备份文件要rsync到远端主机,我们将大文件进行切割为几十个小文件进行多线程传输。
这里使用14个1G的文件进行演示:
[root@vm0 test]# pwd
/root/test
[root@vm0 test]# ll
总用量 13631540
-rw-r--r--. 1 root root 1073741824 6月  11 18:29 test10.data
-rw-r--r--. 1 root root 1073741824 6月  11 18:30 test11.data
-rw-r--r--. 1 root root 1073741824 6月  11 18:31 test12.data
-rw-r--r--. 1 root root 1073741824 6月  11 18:31 test13.data
-rw-r--r--. 1 root root 1073741824 6月  11 18:32 test14.data
-rw-r--r--. 1 root root 1073741824 6月  11 18:23 test2.data
-rw-r--r--. 1 root root 1073741824 6月  11 18:24 test3.data
-rw-r--r--. 1 root root 1073741824 6月  11 18:25 test4.data
-rw-r--r--. 1 root root 1073741824 6月  11 18:26 test5.data
-rw-r--r--. 1 root root 1073741824 6月  11 18:26 test6.data
-rw-r--r--. 1 root root 1073741824 6月  11 18:27 test7.data
-rw-r--r--. 1 root root 1073741824 6月  11 18:28 test8.data
-rw-r--r--. 1 root root 1073741824 6月  11 18:29 test9.data
[root@vm0 test]#
脚本名:tq.pl
#!/usr/bin/env perl

use strict;
use threads;
use Thread::Queue;
use File::Find;
use File::Rsync;
use POSIX qw(strftime);

#本地主机文件目录
my $srcFilePath='/root/test/';
#使用队列,将要备份的文件逐一插入队列
my $fileQueue = Thread::Queue->new();
#远端主机备份目录
my $remotedir='lansgg@192.168.137.129::lansggtest';
#最大线程数
my $thread_max = 5;
my $backupTime = strftime("%Y%m%d%H%M%S",localtime(time));
print "begin : $backupTime\n";

#检索要备份目录下的所有文件,. 除外。 linux中 . 代表当前目录
sub findAllFile {
unless ( $_ eq '.'){
print "corrent file : $File::Find::name \n";
$fileQueue->enqueue($_);
}
}

find(\&findAllFile,$srcFilePath);

#使用rsync进行传输
sub rsync {
my $file = shift;
print "rsync -- $file \n";
my $obj = File::Rsync->new(
{
archive    => 1,
compress => 1,
checksum => 1,
recursive => 1,
times => 1,
#    verbose => 1,
timeout => 300,
progress => 1,
stats => 1,
'ignore-times' => 1,
'password-file' => './rsync.pass',
}
);

$obj->exec( { src => "$srcFilePath$file", dest => $remotedir } ) or warn "rsync Failed ! \n";

#print $obj->out;

}
#检查队列中未传输的文件
while ($fileQueue->pending()){
if (scalar(threads->list()) < $thread_max ){
my $readQueue = $fileQueue->dequeue();
#        print "current file Queue is $readQueue \n";
#生成线程
threads->create(\&rsync,$readQueue);

#查看当前线程总数
my $thread_count = threads->list();
#        print "thread_count is $thread_count\n";
}
#确定当前线程是否作业完成,进行回收
foreach my $thread (threads->list(threads::all)){
if ($thread->is_joinable()){
$thread->join();
}
}
}

#join掉剩下的线程(因为在while中的队列为空时,可能还有线程在执行,但是此时程序将退出while循环,所以这里需要额外程序join掉剩下的线程)

foreach my $thread ( threads->list(threads::all) ) {
$thread->join();
}

$backupTime = strftime("%Y%m%d%H%M%S",localtime(time));
print "end : $backupTime\n";
此脚本是使用了核心功能,后期可以加上日志记录,邮件发送等功能。

当我们执行该脚本时,查看主机线程情况
[root@vm0 pl]# ps -ef |grep tq
root       6377   2152 88 19:05 pts/3    00:00:12 perl ./tq.pl
[root@vm0 pl]# pstree -p 6377
perl(6377)─┬─rsync(6379)
├─rsync(6381)
├─rsync(6383)
├─rsync(6385)
├─rsync(6387)
├─{perl}(6378)
├─{perl}(6380)
├─{perl}(6382)
├─{perl}(6384)
└─{perl}(6386)

[root@vm0 pl]# ps -ef |grep rsync
root       6379   6377 14 19:05 pts/3    00:00:14 rsync --archive --checksum --compress --ignore-times --progress --recursive --stats --times --password-file=./rsync.pass --timeout=300 /root/test//test13.data lansgg@192.168.137.129::lansggtest
root       6381   6377 14 19:05 pts/3    00:00:14 rsync --archive --checksum --compress --ignore-times --progress --recursive --stats --times --password-file=./rsync.pass --timeout=300 /root/test//test12.data lansgg@192.168.137.129::lansggtest
root       6383   6377 14 19:05 pts/3    00:00:14 rsync --archive --checksum --compress --ignore-times --progress --recursive --stats --times --password-file=./rsync.pass --timeout=300 /root/test//test1.data lansgg@192.168.137.129::lansggtest
root       6385   6377 14 19:05 pts/3    00:00:14 rsync --archive --checksum --compress --ignore-times --progress --recursive --stats --times --password-file=./rsync.pass --timeout=300 /root/test//test8.data lansgg@192.168.137.129::lansggtest
root       6387   6377 12 19:05 pts/3    00:00:12 rsync --archive --checksum --compress --ignore-times --progress --recursive --stats --times --password-file=./rsync.pass --timeout=300 /root/test//test3.data lansgg@192.168.137.129::lansggtest
root       6399   2193  0 19:06 pts/2    00:00:00 grep rsync
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  queue rsync perl