Blame | Last modification | View Log | Download | RSS feed
#!/usr/bin/env perl
use warnings;
use strict;
use Net::FTP;
# find our location and use it for searching for libraries
BEGIN {
use FindBin;
use File::Spec;
use lib File::Spec->catdir($FindBin::Bin);
}
require 'rsbackup.pm';
my $MY_PATH;
my $TEST_CONDITIONS;
my $testConfigurationFile;
my $transports;
sub loadConfigurationFile {
use File::Spec::Functions qw(rel2abs);
use File::Basename;
use Cwd;
use Cwd qw(realpath);
# calculate the name of the configuration file, as basename.conf
# and get the path to the program at the same time.
my $filename;
my $suffix;
($filename, $MY_PATH, $suffix) = fileparse(realpath($0),('.pl'));
my $configuration_file = $filename . '.conf';
my @configurationFiles; # store the names of configuration files found, in correct order.
# implementing a queue, using push to place things into the queue, and shift to bring them out in the same order
# Note: This is sloppy as, if we are running from the app directory, the conf file is run twice
# /etc has lowest priority, so it is run first
if ( $TEST_CONDITIONS == 3 ) { # we are using an alternate configuration, generally for development
push @configurationFiles, "../../../rsbackup.conf";
} elsif ( $testConfigurationFile ) {
print "Loading configuration file from command line, $testConfigurationFile\n";
push @configurationFiles, $testConfigurationFile;
} else {
push @configurationFiles, "/etc/$configuration_file" if ( -e "/etc/$configuration_file" );
# now, try for the "real" configuration file in /etc/$filename
push @configurationFiles, "/etc/$filename/$configuration_file" if ( -e "/etc/$filename/$configuration_file" );
# application directory is next higher priority
push @configurationFiles, $MY_PATH . $configuration_file if ( -e "$MY_PATH/$configuration_file" );
# current working directory has highest priority
push @configurationFiles, getcwd . "/$configuration_file" if ( -e getcwd . "/$configuration_file" );
return '' unless @configurationFiles; # require at least one configuration file
# now, see if the control file exists and, if it does, add it at the tail end of the queue (giving it priority)
push @configurationFiles, '/etc/rsbackup/rsbackup_control.conf' if ( -e '/etc/rsbackup/rsbackup_control.conf' );
}
# Now, read the contents into a string, in the correct order, so they can be returned to the calling routine
my $confFileContents;
my $lastConfig = ''; # total hack to work around the fact that we load multiple copies of the same file
while ($filename = shift @configurationFiles) {
next if $lastConfig eq $filename;
$lastConfig = $filename;
print "Processing Configuration File [$filename]\n" if $TEST_CONDITIONS;
open CONFFILE, "<$filename" or die "Can not open configuration file $filename";
$confFileContents .= join( '', <CONFFILE> );
close CONFFILE;
}
return $confFileContents;
}
sub makeTempFile {
my $content = shift;
use File::Temp qw(tempfile);
my $tmp_fh = new File::Temp( UNLINK => 1 );
print $tmp_fh $content;
return $tmp_fh->filename;
}
sub doit {
my ( $parameters, $message ) = @_;
my $filename = '';
if ( $message =~ m/^filename:(.*)$/ ) {
$filename = $1;
$filename = &deblank( $filename );
} else {
$filename = &saveToTempFile( $message );
}
my $ftp = Net::FTP->new(
$parameters->{ 'server' },
Debug => 0,
Port => $parameters->{ 'port' },
SSL => 1,
Timeout => $parameters->{ 'timeout' },
Passive => $parameters->{ 'Passive Mode' }
) or die "Cannot connect to some.host.name: $@";
$ftp->login( $parameters->{ 'Username' },$parameters->{ 'Password' } ) or die "Cannot login ", $ftp->message;
if ( $$transports{ 'directory' } ) {
$ftp->cwd( $transports->{'directory'} ) or die "Cannot change working directory ", $ftp->message;
}
$ftp->binary(); # or $ftp->ascii(), or $ftp->type( 'binary' ); or $type = $ftp->type();
$ftp->put( $filename ) or die "get failed ", $ftp->message;
$ftp->quit;
}
my $configuration = &loadConfigurationFile();
die "No Configuration file found" unless $configuration;
# load configuration file into memory and die if it is invalid
eval( $configuration ) or die "Error in configuration file. Execute perl -c conffile for all copies";