免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1654 | 回复: 3
打印 上一主题 下一主题

ftpbackup script [复制链接]

论坛徽章:
1
2015元宵节徽章
日期:2015-03-06 15:50:39
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-08 18:35 |只看该作者 |倒序浏览
我在perlmonk上找大一个很有用的脚本,它用于备份远程主机的数据相信会很有用,只是我还读不懂,期望 对大家能有帮助。

<code>
#!/usr/bin/perl
#
# Entries in the file and host list should be seperated with
# a new row. spaces or other signs will make the script fail.
# You can use comments in the host and file lists if you put
# a '#' sign in the beginning of the line.
#
use Net::FTP;
use strict;

my $usage = "$0 filelist hostlist username password directoryn";
my $filelist = $ARGV[0] or die "$usage";
my $hostlist = $ARGV[1] or die "$usage";
my $username = $ARGV[2] or die "$usage";
my $password = $ARGV[3] or die "$usage";
my $home = $ARGV[4] or die "$usage";
my $debug = 5;
our $nr1 = 0;

open(FilesToGet, $filelist)
or die "can't open filelist $filelist: $!n";

open(HostsToGet, $hostlist)
or die "can't open hostlist $hostlist: $!n";

my @hostarr = <HostsToGet>;
chomp @hostarr;
my $hostarrnr = @hostarr;

my @filearr = <FilesToGet>;
chomp @filearr;
my $filearrnr = @filearr;

@hostarr = grep !/^#/, @hostarr;
@filearr = grep !/^#/, @filearr;

my $filearrnr = @filearr;
my $hostarrnr = @hostarr;

print "nHosts: $hostarrnrn";
print "Files: " . ($filearrnr * $hostarrnr) . "nn";

sub ftpbackup {
    if (my $pid = fork) {
        print STDERR "Connecting to $hostarr[$nr1]n" if $debug > 3;
        my $ftp = Net::FTP->new($hostarr[$nr1]);
        $ftp->login($username, $password);
        $ftp->cwd($home);
        for (my $nr2 = 0; $nr2 < $filearrnr; $nr2++) {
            print STDERR "Getting $filearr[$nr2]n" if $debug > 3;
            $ftp->get($filearr[$nr2],("$hostarr[$nr1]." . $filearr[$nr2]));
        }
        $ftp->quit;
        exit;
    }
}

for ($nr1 = 0; $nr1 < $hostarrnr; $nr1++) {
    ftpbackup();
}
<*code>

论坛徽章:
0
2 [报告]
发表于 2005-11-08 20:45 |只看该作者
ft,不知你在哪里找来的这个script,code繁杂不说,变量defined非常混乱,既有重复的my申明,又非常不明智在subroutine里使用外部变量,在大型program里,这样随时会引起fatal errors的。
至于script本身,并没有什么好说的,一个Net::FTP模块的应用罢了。

论坛徽章:
0
3 [报告]
发表于 2005-11-08 21:34 |只看该作者
是一个普通的 FTP 备份程序,虽然代码写得不很漂亮,但是没像你说的“繁杂”(还那么短的代码)。
“变量 defined 非常混乱”?用了 “use strict”,运行也没有什么错,只是变量的名字可能起得不太好。

“既有重复的my申明,又非常不明智在subroutine里使用外部变量”
哪有重复的 my 声明?在 subroutine 里就绝对不能使用“外部变量”(你指的是“在 subroutine 外的变量”?)?

还有,这只是一段完成简单任务的小脚本,为什么偏要用“大型program”来跟它相比?

“不知你在哪里找来的这个script”
请你看清楚别人的帖子,楼主在第一行就说明:“我在perlmonk上找……”。

[ 本帖最后由 subs 于 2005-11-8 21:39 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2005-11-08 21:37 |只看该作者
原帖由 subs 于 2005-11-8 21:34 发表
是一个普通的 FTP 备份程序,虽然代码写得不很漂亮,但是没像你说的“繁杂”(还那么短的代码)。
“变量 defined 非常混乱”?用了 “use strict”,运行也没有什么错,只是变量的名字可能起得不太好。

“既 ...



ft,你加上use warnings运行这段script再来跟偶说吧!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP