Wednesday, February 14, 2007

Module Net::FTP To Download File

Code below use to download a file from other server and work out on linux command line. I use cronjob to run this script to download daily log of our apache web server.

#!/usr/local/bin/perl

use Net::FTP;

$ftpSite = "www.muntenari.com";
$ftpUsername = "username";
$ftpPassword = "password";
$ftpDirectory = "/var/log/httpd";
$logFilesDirectory = "/localserver/home/";
$ftpFilename = "data.tar.gz";

unless (-d $logFilesDirectory) {
print STDERR "Creating directory " . $logFilesDirectory . "\n";
mkdir $logFilesDirectory || die "Cannot create " . $! . "\n";
}

$ftp = Net::FTP->new($ftpSite) || die "Can't connect to $ftpSite : $@\n";
$ftp->login($ftpUsername, $ftpPassword) || die "Couldn't login to $ftpSite \n";
$ftp->cwd($ftpDirectory) || die "Couldn't change directory\n";
$ftp->binary() || die "Couldn't change to binary\n";

print STDERR "copying $ftpFilename from $ftpSite .....\n";
$noGetFile = 0;
$ftp->get($ftpFilename,"$logFilesDirectory/$ftpFilename") || ($noGetFile = 1);

if ($noGetFile == 1) {
print "Couldn't get $ftpFilename\n";
}
else {
print "success\n";
}

1;

No comments: