5 use Net::SFTP::Foreign::Compat;
7 use FS::UID qw(adminsuidsetup datasrc);
14 use vars qw( $opt_m $opt_p $opt_r $opt_e $opt_d $opt_v $opt_P $opt_a $opt_c $opt_g );
15 getopts('c:m:p:r:e:d:v:P:ag');
18 #$opt_e = ".$opt_e" unless $opt_e =~ /^\./;
23 die "invalid cdrtypenum" if $opt_c && $opt_c !~ /^\d+$/;
27 my $user = shift or die &usage;
30 # %%%FREESIDE_CACHE%%%
31 my $cachedir = '%%%FREESIDE_CACHE%%%/cache.'. datasrc. '/cdrs';
32 mkdir $cachedir unless -d $cachedir;
34 my $format = shift or die &usage;
36 use vars qw( $servername );
37 $servername = shift or die &usage;
43 warn "Retrieving directory listing\n" if $opt_v;
45 $opt_m = 'sftp' if !defined($opt_m);
51 $options{'Port'} = $opt_P if $opt_P;
52 $options{'Debug'} = $opt_v if $opt_v;
53 $options{'Passive'} = $opt_a if $opt_a;
57 $ls = [ grep { /^$opt_p.*\.$opt_e$/i } $ls_ftp->ls ];
59 elsif($opt_m eq 'sftp') {
60 $options{'port'} = $opt_P if $opt_P;
61 $options{'debug'} = $opt_v if $opt_v;
65 $ls_sftp->setcwd($opt_r) or die "can't chdir to $opt_r\n"
68 $ls = $ls_sftp->ls('.', wanted => qr/^$opt_p.*\.$opt_e$/i,
72 die "Method '$opt_m' not supported; must be ftp or sftp\n";
79 foreach my $filename ( @$ls ) {
81 warn "Downloading $filename\n" if $opt_v;
86 $ftp->get($filename, "$cachedir/$filename")
87 or die "Can't get $filename: ". $ftp->message . "\n";
91 $sftp->get($filename, "$cachedir/$filename")
92 or die "Can't get $filename: ". $sftp->error . "\n";
95 warn "Processing $filename\n" if $opt_v;
97 my $ungziped = $filename;
98 $ungziped =~ s/\.gz$//;
100 if(system('gunzip', "$cachedir/$filename") != 0) {
101 warn "gunzip of '$cachedir/$filename' failed" if $opt_v;
102 unlink "$cachedir/$filename";
107 my $import_options = {
108 'file' => "$cachedir/$ungziped",
110 'batch_namevalue' => $filename,
113 $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
115 my $error = FS::cdr::batch_import($import_options);
116 die $error if $error;
119 if($opt_m eq 'ftp') {
121 $ftp->rename($filename, "$opt_d/$filename")
122 or die "Can't move $filename to $opt_d: ".$ftp->message . "\n";
126 $sftp->rename($filename, "$opt_d/$filename")
127 or die "can't move $filename to $opt_d: ". $sftp->error . "\n";
131 unlink "$cachedir/$filename";
132 unlink "$cachedir/$ungziped" if $opt_g;
141 "Usage: \n cdr.import user format servername\n";
144 use vars qw( $sftp $ftp );
147 return $ftp if $ftp && $ftp->pwd;
149 my ($hostname, $user) = reverse split('@', $servername);
150 my ($user, $pass) = split(':', $user);
152 my $ftp = Net::FTP->new($hostname, %options)
153 or die "FTP connection to '$hostname' failed.";
154 $ftp->login($user, $pass) or die "FTP login failed: ".$ftp->message;
155 $ftp->cwd($opt_r) or die "can't chdir to $opt_r\n" if $opt_r;
162 return $sftp if $sftp && $sftp->cwd;
164 my %sftp = ( host => $servername );
166 $sftp = Net::SFTP::Foreign->new(%sftp);
167 $sftp->error and die "SFTP connection failed: ". $sftp->error;
174 freeside-cdr-sftp_and_import - Download CDR files from a remote server via SFTP
178 cdr.sftp_and_import [ -m method ] [ -p prefix ] [ -e extension ]
179 [ -r remotefolder ] [ -d donefolder ] [ -v level ] [ -P port ]
180 [ -a ] [ -c cdrtypenum ] user format [sftpuser@]servername
184 Command line tool to download CDR files from a remote server via SFTP
185 or FTP and then import them into the database.
187 -m: transfer method (sftp or ftp), defaults to sftp
189 -p: file prefix, if specified
191 -e: file extension, defaults to .csv
193 -r: if specified, changes into this remote folder before starting
195 -d: if specified, moves files to the specified folder when done
197 -P: if specified, sets the port to use
199 -a: use ftp passive mode
201 -v: set verbosity level; this script only has one level, but it will
202 be passed as the 'debug' argument to the transport method
204 -c: cdrtypenum to set, defaults to none
206 user: freeside username
208 format: CDR format name
210 [sftpuser@]servername: remote server
211 (or ftpuser:ftppass@servername)