perl下网站批量更新文件的应用
创始人
2024-02-14 01:00:15
0

在发布网站的时候,由于要更新的文件比较多,有时候漏传,或者传错了文件,这样后果是很严重的,直接引起网站的异常,所以写了一个自动同步测试服务器的程序,以批量完成文件的更新。

环境,一台测试服务器,一台网站服务器
需求,将测试服务器更新的文件拉取到网站服务器上,网站对应的目录结构都相同。
思路,用perl实现ftp的下载机制,自动将更新文件村放到相应目录,将更新的旧文件按照当前日志进行备份,同时显示更新文件的进度(百分比)。

执行结果如下:

[root@drbd-2 crontab]# ./test.pl
 60% [==========================================                                  ]
3 files is download ok!
2 files is lost...........
this files have losted :
/var/ftp/pub/aa
/var/ftp/pub/bb


代码如下:


#!/usr/bin/perl -w

use strict;

use Net::FTP;
use File::Copy;
use Date::Manip;
use File::Basename;
use Term::ProgressBar;

my $server      = '192.168.1.100';
my $port        = '21';
my $user        = 'upload';
my $pw          = '123456';
my $file        = '/usr/local/crontab/upload';
my $backup_path = '/var/ftp/backup';

open FILE, "< $file"
  or die "can't open $file..... ($!)";

my @failed_files = ();

sub ftp() {
    my $ftp =
      Net::FTP->new( $server, Port => $port, Debug => 0, Timeout => 600 )
      or die "Cannot connect.\n";
    $ftp->login( $user, $pw )
      or die "Could not login.\n";

    my $get = 0;

    open F2, "< $file" or die $!;
    my $number = grep { !/^\s*$/ } ;
    close F2;

    my $progress = Term::ProgressBar->new( { count => $number } );

    $ftp->binary;

    foreach () {

        $_ =~ tr/\t //d;
        if ( $_ =~ /^\s*$/ ) {
            next;
        }

        chomp;

        my $server_path = dirname($_);
        my $server_file = basename($_);

        my $temp_path = substr( $server_path, index( $server_path, "pub" ) );


        my $local_path = $server_path;
        if ( -e $local_path ) {

        }
        else {
            $ENV{"umask"} = "022";
            system("mkdir -p $server_path");
        }

        my $local_file = $server_path . "/$server_file";
        if ( -e $local_file ) {


            my $now_time = localtime();
            my $today_dir = UnixDate( "$now_time", "%Y-%m-%d_%H:%M" );
            system("mkdir -p $backup_path/$today_dir/$temp_path");
            move( "$local_file", $backup_path . "/$today_dir/$temp_path" )
              or die "mv file failed: $!";
        }

        $ftp->cwd($server_path)
          or die "Cannot change working directory. $ftp->message\n";
        if ( $ftp->get( $server_file, $local_file ) ) {
            $progress->update( ++$get );
        }
        else {
            push( @failed_files, $local_file );
        }
    }

    print "$get files is download ok!" . "\n";
    my $lost = $number - $get;
    print "$lost files is lost..........." . "\n";
}

&ftp();

for (@failed_files) {
    print;
    print "\n";

}

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...