29 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
|
|
|
3 |
use warnings;
|
|
|
4 |
use strict;
|
|
|
5 |
use Net::FTP;
|
|
|
6 |
|
|
|
7 |
# find our location and use it for searching for libraries
|
|
|
8 |
BEGIN {
|
|
|
9 |
use FindBin;
|
|
|
10 |
use File::Spec;
|
|
|
11 |
use lib File::Spec->catdir($FindBin::Bin);
|
|
|
12 |
}
|
|
|
13 |
|
|
|
14 |
require 'rsbackup.pm';
|
|
|
15 |
|
|
|
16 |
my $MY_PATH;
|
|
|
17 |
my $TEST_CONDITIONS;
|
|
|
18 |
my $testConfigurationFile;
|
|
|
19 |
my $transports;
|
|
|
20 |
|
|
|
21 |
sub loadConfigurationFile {
|
|
|
22 |
use File::Spec::Functions qw(rel2abs);
|
|
|
23 |
use File::Basename;
|
|
|
24 |
use Cwd;
|
|
|
25 |
use Cwd qw(realpath);
|
|
|
26 |
# calculate the name of the configuration file, as basename.conf
|
|
|
27 |
# and get the path to the program at the same time.
|
|
|
28 |
my $filename;
|
|
|
29 |
my $suffix;
|
|
|
30 |
($filename, $MY_PATH, $suffix) = fileparse(realpath($0),('.pl'));
|
|
|
31 |
my $configuration_file = $filename . '.conf';
|
|
|
32 |
my @configurationFiles; # store the names of configuration files found, in correct order.
|
|
|
33 |
# implementing a queue, using push to place things into the queue, and shift to bring them out in the same order
|
|
|
34 |
# Note: This is sloppy as, if we are running from the app directory, the conf file is run twice
|
|
|
35 |
# /etc has lowest priority, so it is run first
|
|
|
36 |
if ( $TEST_CONDITIONS == 3 ) { # we are using an alternate configuration, generally for development
|
|
|
37 |
push @configurationFiles, "../../../rsbackup.conf";
|
|
|
38 |
} elsif ( $testConfigurationFile ) {
|
|
|
39 |
print "Loading configuration file from command line, $testConfigurationFile\n";
|
|
|
40 |
push @configurationFiles, $testConfigurationFile;
|
|
|
41 |
} else {
|
|
|
42 |
push @configurationFiles, "/etc/$configuration_file" if ( -e "/etc/$configuration_file" );
|
|
|
43 |
# now, try for the "real" configuration file in /etc/$filename
|
|
|
44 |
push @configurationFiles, "/etc/$filename/$configuration_file" if ( -e "/etc/$filename/$configuration_file" );
|
|
|
45 |
# application directory is next higher priority
|
|
|
46 |
push @configurationFiles, $MY_PATH . $configuration_file if ( -e "$MY_PATH/$configuration_file" );
|
|
|
47 |
# current working directory has highest priority
|
|
|
48 |
push @configurationFiles, getcwd . "/$configuration_file" if ( -e getcwd . "/$configuration_file" );
|
|
|
49 |
return '' unless @configurationFiles; # require at least one configuration file
|
|
|
50 |
# now, see if the control file exists and, if it does, add it at the tail end of the queue (giving it priority)
|
|
|
51 |
push @configurationFiles, '/etc/rsbackup/rsbackup_control.conf' if ( -e '/etc/rsbackup/rsbackup_control.conf' );
|
|
|
52 |
}
|
|
|
53 |
# Now, read the contents into a string, in the correct order, so they can be returned to the calling routine
|
|
|
54 |
my $confFileContents;
|
|
|
55 |
my $lastConfig = ''; # total hack to work around the fact that we load multiple copies of the same file
|
|
|
56 |
while ($filename = shift @configurationFiles) {
|
|
|
57 |
next if $lastConfig eq $filename;
|
|
|
58 |
$lastConfig = $filename;
|
|
|
59 |
print "Processing Configuration File [$filename]\n" if $TEST_CONDITIONS;
|
|
|
60 |
open CONFFILE, "<$filename" or die "Can not open configuration file $filename";
|
|
|
61 |
$confFileContents .= join( '', <CONFFILE> );
|
|
|
62 |
close CONFFILE;
|
|
|
63 |
}
|
|
|
64 |
return $confFileContents;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
sub makeTempFile {
|
|
|
68 |
my $content = shift;
|
|
|
69 |
use File::Temp qw(tempfile);
|
|
|
70 |
my $tmp_fh = new File::Temp( UNLINK => 1 );
|
|
|
71 |
print $tmp_fh $content;
|
|
|
72 |
return $tmp_fh->filename;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
|
|
|
76 |
sub doit {
|
|
|
77 |
my ( $parameters, $message ) = @_;
|
|
|
78 |
|
|
|
79 |
my $filename = '';
|
|
|
80 |
|
|
|
81 |
if ( $message =~ m/^filename:(.*)$/ ) {
|
|
|
82 |
$filename = $1;
|
|
|
83 |
$filename = &deblank( $filename );
|
|
|
84 |
} else {
|
|
|
85 |
$filename = &saveToTempFile( $message );
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
my $ftp = Net::FTP->new(
|
|
|
89 |
$parameters->{ 'server' },
|
|
|
90 |
Debug => 0,
|
|
|
91 |
Port => $parameters->{ 'port' },
|
|
|
92 |
SSL => 1,
|
|
|
93 |
Timeout => $parameters->{ 'timeout' },
|
|
|
94 |
Passive => $parameters->{ 'Passive Mode' }
|
|
|
95 |
) or die "Cannot connect to some.host.name: $@";
|
|
|
96 |
$ftp->login( $parameters->{ 'Username' },$parameters->{ 'Password' } ) or die "Cannot login ", $ftp->message;
|
|
|
97 |
if ( $$transports{ 'directory' } ) {
|
|
|
98 |
$ftp->cwd( $transports->{'directory'} ) or die "Cannot change working directory ", $ftp->message;
|
|
|
99 |
}
|
|
|
100 |
$ftp->binary(); # or $ftp->ascii(), or $ftp->type( 'binary' ); or $type = $ftp->type();
|
|
|
101 |
$ftp->put( $filename ) or die "get failed ", $ftp->message;
|
|
|
102 |
$ftp->quit;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
my $configuration = &loadConfigurationFile();
|
|
|
108 |
die "No Configuration file found" unless $configuration;
|
|
|
109 |
# load configuration file into memory and die if it is invalid
|
|
|
110 |
eval( $configuration ) or die "Error in configuration file. Execute perl -c conffile for all copies";
|
|
|
111 |
|