perl多线程rsync备份文件到远端主机
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,需求:主机上有上百G的备份文件要rsync到远端主机,我们将大文件进行切割为几十个小文件进行多线程传输。这里使用14个1G的文件进行演示:[root@vm0 test]# pwd/root/test[
千家信息网最后更新 2026年11月02日perl多线程rsync备份文件到远端主机
需求:
主机上有上百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 perluse 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 tqroot 6377 2152 88 19:05 pts/3 00:00:12 perl ./tq.pl[root@vm0 pl]# pstree -p 6377perl(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 rsyncroot 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::lansggtestroot 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::lansggtestroot 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::lansggtestroot 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::lansggtestroot 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::lansggtestroot 6399 2193 0 19:06 pts/2 00:00:00 grep rsync
线程
文件
主机
备份
目录
脚本
队列
传输
功能
程序
最大
代表
总数
情况
日志
核心
用量
邮件
需求
作业
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
深圳科技互联网
卓越科技(互联网 )
佰商互联网科技有限公司
互联网科技公司做什么
科技伦理 互联网公司
上海电发互联网科技有限公司
宁波青枫互联网科技有限公司
互联网科技热点资讯
科技与互联网哪个更好
互联网经济与金融科技
厦门千工互联网科技有限公司
互联网科技产业特征
辽宁互联网新科技
苏州铭阳互联网科技有限公司
互联网保险科技创业
东莞新华互联网科技学校
互联网科技大赛获奖感言
北京市互联网新科技
互联网科技实习生岗位
互联网科技自媒体灵感
北京路客互联网科技有限公司
科技股互联网
互联网科技垄断
北京互联网科技公司
互联网科技公司怎么样
互联网科技博主是什么意思
武汉健翔互联网络科技有限公司
湖南一路畅行互联网科技
互联网科技研发水平
允成互联网科技有限公司融资