[freeside-commits] freeside/FS/bin freeside-cdr-sftp_and_import, NONE, 1.1
Ivan,,,
ivan at wavetail.420.am
Sat May 9 16:55:00 PDT 2009
Update of /home/cvs/cvsroot/freeside/FS/bin
In directory wavetail.420.am:/tmp/cvs-serv4765/FS/bin
Added Files:
freeside-cdr-sftp_and_import
Log Message:
move cdr-sftp_and_import script to FS/bin, add -p option, RT#4081
--- NEW FILE: freeside-cdr-sftp_and_import ---
#!/usr/bin/perl
use strict;
use Getopt::Std;
use Net::SFTP::Foreign;
use FS::UID qw(adminsuidsetup datasrc);
use FS::cdr;
###
# parse command line
###
use vars qw( $opt_p $opt_e $opt_d $opt_v );
getopts('p:e:d:v');
$opt_e ||= 'csv';
#$opt_e = ".$opt_e" unless $opt_e =~ /^\./;
$opt_e =~ s/^\.//;
$opt_p ||= '';
my $user = shift or die &usage;
adminsuidsetup $user;
# %%%FREESIDE_CACHE%%%
my $cachedir = '/usr/local/etc/freeside/cache.'. datasrc. '/cdrs';
mkdir $cachedir unless -d $cachedir;
my $format = shift or die &usage;
use vars qw( $servername );
$servername = shift or die &usage;
###
# get the file list
###
warn "Retreiving directory listing\n" if $opt_v;
my $ls_sftp = sftp();
my $ls = $ls_sftp->ls('.', wanted => qr/^$opt_p.*\.$opt_e$/i );
###
# import each file
###
foreach my $file ( @$ls ) {
my $filename = $file->{filename};
warn "Downloading $filename\n" if $opt_v;
#get the file
my $get_sftp = sftp();
$get_sftp->get($filename, "$cachedir/$filename")
or die "Can't get $filename: ". $get_sftp->error;
warn "Processing $filename\n" if $opt_v;
my $error = FS::cdr::batch_import( {
'file' => "$cachedir/$filename",
'format' => $format,
'params' => { 'cdrbatch' => $filename, },
'empty_ok' => 1,
} );
die $error if $error;
if ( $opt_d ) {
my $mv_sftp = sftp();
$mv_sftp->rename($filename, "$opt_d/$filename")
or die "can't move $filename to $opt_d: ". $mv_sftp->error;
}
unlink "$cachedir/$filename";
}
###
# subs
###
sub usage {
"Usage: \n cdr.import user format servername\n";
}
use vars qw( $sftp );
sub sftp {
#reuse connections
return $sftp if $sftp && $sftp->cwd;
my %sftp = ( host => $servername );
#XXX remove these
$sftp{port} = 10022;
#$sftp{more} = '-v';
$sftp = Net::SFTP::Foreign->new(%sftp);
$sftp->error and die "SFTP connection failed: ". $sftp->error;
$sftp;
}
=head1 NAME
cdr.sftp_and_import - Download CDR files from a remote server via SFTP
=head1 SYNOPSIS
cdr.sftp_and_import [ -p prefix ] [ -e extension ] [ -d donefolder ] [ -v ] user format [sftpuser@]servername
=head1 DESCRIPTION
Command line tool to download CDR files from a remote server via SFTP and then
import them into the database.
-p: file prefix, if specified
-e: file extension, defaults to .csv
-d: if specified, moves files to the specified folder when done
-v: verbose
user: freeside username
format: CDR format name
[sftpuser@]servername: remote server
=head1 BUGS
Only supports SFTP right now.
=head1 SEE ALSO
L<FS::cdr>
=cut
1;
More information about the freeside-commits
mailing list