6 use Net::SFTP::Foreign::Compat;
8 use FS::UID qw(adminsuidsetup datasrc);
15 use vars qw( $opt_m $opt_p $opt_r $opt_e $opt_d $opt_v $opt_P $opt_a $opt_c $opt_g $opt_s $opt_b );
16 getopts('c:m:p:r:e:d:v:P:agsb');
19 #$opt_e = ".$opt_e" unless $opt_e =~ /^\./;
24 die "invalid cdrtypenum" if $opt_c && $opt_c !~ /^\d+$/;
28 my $user = shift or die &usage;
31 # %%%FREESIDE_CACHE%%%
32 my $cachedir = '%%%FREESIDE_CACHE%%%/cache.'. datasrc. '/cdrs';
33 mkdir $cachedir unless -d $cachedir;
35 my $format = shift or die &usage;
37 use vars qw( $servername );
38 $servername = shift or die &usage;
44 warn "Retrieving directory listing\n" if $opt_v;
46 $opt_m = 'sftp' if !defined($opt_m);
52 $options{'Port'} = $opt_P if $opt_P;
53 $options{'Debug'} = $opt_v if $opt_v;
54 $options{'Passive'} = $opt_a if $opt_a;
58 $ls = [ grep { /^$opt_p.*\.$opt_e$/i } $ls_ftp->ls ];
60 elsif($opt_m eq 'sftp') {
61 $options{'port'} = $opt_P if $opt_P;
62 $options{'debug'} = $opt_v if $opt_v;
66 $ls_sftp->setcwd($opt_r) or die "can't chdir to $opt_r\n"
69 $ls = $ls_sftp->ls('.', wanted => qr/^$opt_p.*\.$opt_e$/i,
73 die "Method '$opt_m' not supported; must be ftp or sftp\n";
80 foreach my $filename ( @$ls ) {
82 warn "Downloading $filename\n" if $opt_v;
87 $ftp->get($filename, "$cachedir/$filename")
88 or die "Can't get $filename: ". $ftp->message . "\n";
92 $sftp->get($filename, "$cachedir/$filename")
93 or die "Can't get $filename: ". $sftp->error . "\n";
96 warn "Processing $filename\n" if $opt_v;
98 my $ungziped = $filename;
99 $ungziped =~ s/\.gz$//;
101 if(system('gunzip', "$cachedir/$filename") != 0) {
102 warn "gunzip of '$cachedir/$filename' failed" if $opt_v;
103 unlink "$cachedir/$filename";
108 my $file_timestamp = $filename.'-'.time2str('%Y-%m-%d', time);
110 my $import_options = {
111 'file' => "$cachedir/$ungziped",
113 'batch_namevalue' => $file_timestamp,
116 $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
118 my $error = FS::cdr::batch_import($import_options);
123 warn "$ungziped: $error\n";
125 unlink "$cachedir/$filename";
126 unlink "$cachedir/$ungziped" if $opt_g;
133 if ( $opt_m eq 'ftp') {
135 $ftp->rename($filename, "$opt_d/$file_timestamp")
137 unlink "$cachedir/$filename";
138 unlink "$cachedir/$ungziped" if $opt_g;
139 die "Can't move $filename to $opt_d: ".$ftp->message . "\n";
143 $sftp->rename($filename, "$opt_d/$file_timestamp")
145 unlink "$cachedir/$filename";
146 unlink "$cachedir/$ungziped" if $opt_g;
147 die "can't move $filename to $opt_d: ". $sftp->error . "\n";
154 unlink "$cachedir/$filename";
155 unlink "$cachedir/$ungziped" if $opt_g;
164 "Usage: \n cdr.import user format servername\n";
167 use vars qw( $sftp $ftp );
170 return $ftp if $ftp && $ftp->pwd;
172 my ($hostname, $userpass) = reverse split('@', $servername);
173 my ($ftp_user, $ftp_pass) = split(':', $userpass);
175 my $ftp = Net::FTP->new($hostname, %options)
176 or die "FTP connection to '$hostname' failed.";
177 $ftp->login($ftp_user, $ftp_pass) or die "FTP login failed: ".$ftp->message;
178 $ftp->cwd($opt_r) or die "can't chdir to $opt_r\n" if $opt_r;
179 $ftp->binary or die "can't set BINARY mode: ". $ftp->message if $opt_b;
186 return $sftp if $sftp && $sftp->cwd;
188 my %sftp = ( host => $servername );
190 $sftp = Net::SFTP::Foreign->new(%sftp);
191 $sftp->error and die "SFTP connection failed: ". $sftp->error;
198 freeside-cdr-sftp_and_import - Download CDR files from a remote server via SFTP
202 cdr.sftp_and_import [ -m method ] [ -p prefix ] [ -e extension ]
203 [ -r remotefolder ] [ -d donefolder ] [ -v level ] [ -P port ]
204 [ -a ] [ -g ] [ -s ] [ -c cdrtypenum ] user format [sftpuser@]servername
208 Command line tool to download CDR files from a remote server via SFTP
209 or FTP and then import them into the database.
211 -m: transfer method (sftp or ftp), defaults to sftp
213 -p: file prefix, if specified
215 -e: file extension, defaults to .csv
217 -r: if specified, changes into this remote folder before starting
219 -d: if specified, moves files to the specified folder when done
221 -P: if specified, sets the port to use
223 -a: use ftp passive mode
225 -b: use ftp binary mode
227 -v: set verbosity level; this script only has one level, but it will
228 be passed as the 'debug' argument to the transport method
230 -c: cdrtypenum to set, defaults to none
234 -s: Warn and skip files which could not be imported rather than abort
236 user: freeside username
238 format: CDR format name
240 [sftpuser@]servername: remote server
241 (or ftpuser:ftppass@servername)