5 use Net::SFTP::Foreign;
6 use FS::UID qw(adminsuidsetup datasrc);
13 use vars qw( $opt_p $opt_d $opt_v );
19 my $user = shift or die &usage;
22 # %%%FREESIDE_CACHE%%%
23 my $cachedir = '/usr/local/etc/freeside/cache.'. datasrc. '/cdrs';
24 mkdir $cachedir unless -d $cachedir;
26 #my $format = shift or die &usage;
27 my $format = 'transnexus';
29 use vars qw( $servername );
30 $servername = shift or die &usage;
32 my $DIR = '/home/ossadmin/OSS/nexoss/CDR_ARCHIVE_BY_ACCOUNT';
38 warn "Retreiving directory listing\n" if $opt_v;
42 my $lsdir = $ls_sftp->ls($DIR);
45 # import each file in each dir
48 foreach my $dir ( @$lsdir ) {
50 my $dirname = $dir->{filename};
51 warn "Scanning dir $dirname\n" if $opt_v;
53 #my $ls = $ls_sftp->ls("$DIR/$dirname", wanted => qr/^$opt_p.*-CDRs$/i );
54 my $ls = $ls_sftp->ls("$DIR/$dirname", wanted => qr/^$opt_p.*Customer-CDRs$/i );
56 foreach my $file ( @$ls ) {
58 my $filename = $file->{filename};
59 warn "Downloading $filename\n" if $opt_v;
62 my $get_sftp = sftp();
63 $get_sftp->get("$DIR/$dirname/$filename", "$cachedir/$filename")
64 or die "Can't get $filename: ". $get_sftp->error;
66 warn "Processing $filename\n" if $opt_v;
68 my $error = FS::cdr::batch_import( {
69 'file' => "$cachedir/$filename",
71 'batch_namevalue' => $filename,
78 $mv_sftp->mkdir("$DIR/$dirname/$opt_d");
79 $mv_sftp->rename( "$DIR/$dirname/$filename",
80 "$DIR/$dirname/$opt_d/$filename" )
81 or die "can't move $filename to $opt_d: ". $mv_sftp->error;
84 unlink "$cachedir/$filename";
95 "Usage: \n cdr-transnexus.import [ -v ] user [sftpuser@]servername\n";
103 return $sftp if $sftp && $sftp->cwd;
105 my %sftp = ( host => $servername );
107 $sftp = Net::SFTP::Foreign->new(%sftp);
108 $sftp->error and die "SFTP connection failed: ". $sftp->error;
115 cdr.sftp_and_import - Download CDR files from a remote server via SFTP
119 cdr-transnexus.import [ -v ] user [sftpuser@]servername
123 Command line tool to download CDR files from a remote server via SFTP and then
124 import them into the database.
128 user: freeside username
130 [sftpuser@]servername: remote server
134 Hacked up copy of freeside-cdr-sftp_and_import