perl多线程rsync备份文件到远端主机
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,需求:主机上有上百G的备份文件要rsync到远端主机,我们将大文件进行切割为几十个小文件进行多线程传输。这里使用14个1G的文件进行演示:[root@vm0 test]# pwd/root/test[
千家信息网最后更新 2025年12月03日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安全错误
数据库的锁怎样保障安全
数据库字典表是做什么的
sql局域网无法连接数据库
短视频app用阿里云服务器
多名专家助力网络安全宣传
中国数据库有多少
韶关戴尔服务器多重优惠
ibm服务器改装raid教程
软件开发周期怎么那么慢
数据库怎么控制字段不能为空
广东软件开发者攻略
服务器间距
软件开发在校的项目经历
天融信网络安全售后
吉林市服务器上架
网络安全周活动目的
扫码溯源软件开发
奶块服务器多少钱一台
众鑫网络技术有限公司
马来西亚服务器设置
哈尔滨众琼互联网科技有限公司
数据库有没有前途
教育软件开发可行性
手机清理软件开发
有数据库连接的网站
上海银庭网络技术有限公司咋样
剑灵龙女捏脸数据库
socket连接服务器端
腾讯云搭建直接服务器
宁夏思杰网络技术有限公司
数据库修饰符