#!/usr/local/bin/perl -w # Fetch the latest production data use Getopt::Long; use Pod::Usage; use Net::FTP; use strict; # Parse command line arguments and assign corresponding variables GetOptions ( 'd|date=s' => \( my $date = undef ), 'v|verbose' => \( my $VERBOSE = 0 ), 'r|root=s' => \( my $root = '.' ), ); unless ( defined $date ) { pod2usage( -exitval => 1, -output => \*STDERR ); } # Create the local directories -d "$root/data" or mkdir("$root/data"); # Establish an FTP connection my $ftp = Net::FTP->new("prod", Debug => 0) or die "Cannot connect to the production box: $@"; $ftp->login("user","passwd") or die "Cannot login ", $ftp->message; &FetchRemoteFiles($ftp, "/opt/data", $date, "$root/data"); $ftp->quit; exit(0); # Fetch files under given directory sub FetchRemoteFiles { my ($ftp, $dir, $date, $localdir) = @_; $ftp->cwd($dir) or die "Cannot change working directory ", $ftp->message; my @remote_files = $ftp->ls("*.txt"); my $latest_date; if (uc $date eq "LATEST"){ $latest_date = @{[reverse sort map {/(\d{8})/;$1} @remote_files]}[0]; } else { $latest_date = $date; } print "Fetching data for $latest_date...\n" if $VERBOSE; foreach my $file (@remote_files){ if ($file =~ /$latest_date/){ print ">> $file\n" if $VERBOSE; $ftp->get($file, "$localdir/$file") or die "Can not fetch file ", $ftp->message; } } print "Ok!\n" if $VERBOSE; } sub Today() { my ($day, $month, $year) = (localtime)[3,4,5]; return sprintf "%04d%02d%02d", $year + 1900, $month+1, $day; } __END__ =pod =head1 NAME fetchdata.pl =head1 SYNOPSIS fetchdata.pl [options] =head1 ARGUMENTS All arguments except date to this script are optional. =over 4 =item B<-d|--date [YYYYMMDD|latest]> Specify the date of data to fetch from production. =item B<-r|--root [directory]> Specify the root directory to store the data files. Under the directory there are two sub directories, aus and asia. =item B<-v|--verbose> Let the script print out more debug information. =back =cut