#!/usr/bin/perl -w use strict; use warnings; use File::Copy; use Net::FTP; use Net::Netrc; my $linux = "....."; my $recipient = "....."; my $server = "....."; my $user = "....."; my $password = "....."; # ftp directories my $directory1 = "/ftp/B1"; my $directory2 = "/ftp/B2"; chdir "/var/files" or die "/var/files: $!\n"; -f "/var/files/info" or die "No info NO TRANSFER !\n"; my @ftp_locations = ($directory_1, $directory_2); # open the file safely or complain it wouldn't open open(FILE, "<", "info") or die "Failed to open info file: $!"; for(my $i = 1; $i <= 2; $i++) { $_ = ; s/\W*$//; # remove trailing whitespace next if (!$_); # skip empty lines # check that we get our match. If not, # complain and move on. Want to see two # filenames. unless(/^([\w.-]) \s+ ([\w.-])$/x) { print STDERR "$_ is not a valid line"; next; } my ($old, $new) = ($1, $2); rename $old, $new; # Now that we have our filenames, grab an # ftp directory from the list unless(@ftp_locations) { die "Not enough specified ftp_locations for ". "given number of files!"; } my $destination = shift @ftp_locations; # ftp transfer my $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3); $ftp or die "$server: cannot connect: $@"; # If you don't use ~/.netrc $ftp->login ($user,$password) or die "$_: cannot logon: " . $ftp->message; # change remote directory for the first file $ftp->cwd($destination); # Send file to ftp server $ftp->put($new) or die "$server: cannot put $new: " . $ftp->message; #Quit FTP When finished $ftp->quit; # Sleep for 20 minutes before processing next file. sleep (20 * 60) } # send the mail, only when transfer completed open(MAIL, "|/usr/sbin/sendmail -t") || die "Cant send mail. Reason: $!"; print MAIL "from:$linux\n"; print MAIL "to:$recipient\n"; print MAIL "subject: file transfer was successfully !\n"; print MAIL "file transfer was successfully ! \n\n"; print MAIL "Time: ", scalar localtime, "\n"; close(MAIL); # when transfer completed, create backup subdirectory and move all file there my @dt = localtime; my $subfolder_name = ((((1900 + $dt[5]) * 100 + 1 + $dt[4]) * 100 + $dt[3]) * 100 + $dt[2]) * 100 + $dt[1]; mkdir "/var/save/$subfolder_name" or die "$subfolder_name: $!"; system("mv /var/files/info /var/save/$subfolder_name"); system("mv $new /var/save/$subfolder_name"); exit;