View | Details | Raw Unified | Return to bug 36766
Collapse All | Expand All

(-)a/misc/cronjobs/sftp_file.pl (-1 / +218 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
use C4::Context;
20
use Net::SFTP::Foreign;
21
use C4::Log qw( cronlogaction );
22
use Koha::Email;
23
use Getopt::Long qw( GetOptions );
24
use Pod::Usage qw( pod2usage );
25
use Carp qw( carp );
26
27
=head1 NAME
28
29
sftp_file.pl - SFTP a file to a remote server
30
31
=head1 SYNOPSIS
32
33
oclc.pl [ -h | -m ] [ -v ] method host user pass upload_dir file
34
35
 Options:
36
   -h --help       brief help message
37
   -m --man        full documentation, same as --help --verbose
38
   -v --verbose    verbose output
39
   --host          SFTP host to upload to
40
   --user          SFTP user
41
   --pass          SFTP password
42
   --upload_dir    Directory on SFTP host to upload to
43
   --file          File to SFTP to host
44
   --email         Email address to receive confirmation of SFTP upload success or failure
45
46
=head1 OPTIONS
47
48
=over
49
50
=item B<--help>
51
52
Print brief help and exit.
53
54
=item B<--man>
55
56
Print full documentation and exit.
57
58
=item B<--verbose>
59
60
Increased verbosity, reports successes and errors.
61
62
=item B<--host>
63
64
Remote server host to SFTP file to.
65
66
=item B<--user>
67
68
Username credential to authenticate to remote host.
69
70
=item B<--pass>
71
72
Password credential to authenticate to remote host.
73
74
=item B<--upload_dir>
75
76
Directory on remote host to SFTP file to.
77
78
=item B<--file>
79
80
File to SFTP to remote host.
81
82
=item B<--email>
83
84
Email address to receive the confirmation if the SFTP upload was a success or failure.
85
86
=back
87
88
=head1 DESCRIPTION
89
90
This script is designed to SFTP files.
91
92
=head1 DESCRIPTION
93
94
This script is designed to SFTP files to a remote server.
95
96
=head1 USAGE EXAMPLES
97
98
B<sftp_file.pl --host test.com --user test --pass 123cne --upload_dir uploads --file /tmp/test.mrc>
99
100
In this example the script will upload /tmp/test.mrc on the Koha server to the test.com remote host using the username:password of test:123cne. The file will be SFTP to the uploads directory.
101
102
=head1 TO DO
103
104
=back
105
106
=cut
107
108
# These variables can be set by command line options,
109
# initially set to default values.
110
111
my $help                 = 0;
112
my $man                  = 0;
113
my $verbose              = 0;
114
my $host                 = undef;
115
my $user                 = undef;
116
my $pass                 = undef;
117
my $upload_dir           = undef;
118
my $file                 = 0;
119
my $email                = 0;
120
my $admin_address        = undef;
121
my $status_email         = undef;
122
my $status_email_message = undef;
123
my $sftp_status          = undef;
124
125
my $command_line_options = join(" ",@ARGV);
126
127
GetOptions(
128
    'help|?'         => \$help,
129
    'man'            => \$man,
130
    'verbose'        => \$verbose,
131
    'host=s'         => \$host,
132
    'user=s'         => \$user,
133
    'pass=s'         => \$pass,
134
    'upload_dir=s'   => \$upload_dir,
135
    'file=s'         => \$file,
136
    'email=s'        => \$email,
137
) or pod2usage(2);
138
pod2usage( -verbose => 2 ) if ($man);
139
pod2usage( -verbose => 2 ) if ($help and $verbose);
140
pod2usage(1) if $help;
141
142
cronlogaction({ info => $command_line_options });
143
144
# Check we have all the SFTP details we need
145
if ( !$user || !$pass || !$host || !$upload_dir ) {
146
    pod2usage(q|Please provide details for sftp username, password, upload directory, and host|);
147
}
148
149
# Prepare the SFTP connection
150
my $sftp = Net::SFTP::Foreign->new(
151
    host           => $host,
152
    user           => $user,
153
    password       => $pass,
154
    timeout        => 10,
155
    stderr_discard => 1,
156
);
157
$sftp->die_on_error( "Cannot ssh to $host" . $sftp->error );
158
159
# Change to remote directory
160
$sftp->setcwd( $upload_dir )
161
    or return warn "Cannot change remote dir : $sftp->error\n";
162
163
# If --mail parameter is defined then prepare sending an email confirming the success 
164
# or failure of the SFTP
165
if ( $email ) {
166
    if ( C4::Context->preference('KohaAdminEmailAddress') ) {
167
        $admin_address = C4::Context->preference('KohaAdminEmailAddress');
168
    }
169
170
    if ( !Koha::Email->is_valid($email) ) {
171
        return warn "The email address you defined in the --email parameter is invalid\n";
172
    }
173
174
    $status_email = Koha::Email->new(
175
        {
176
            to        => $email,
177
            from      => $admin_address,
178
            subject   => '[OCLC] ' . C4::Context->config('database') . ' ' . $sftp_status,
179
        }
180
    );
181
}
182
183
# Do the SFTP upload
184
open my $fh, '<', $file;
185
if (
186
    $sftp->put(
187
        $fh, $file,
188
            callback => sub { my ( $sftp, $data, $offset, $size ) = @_; warn "$offset of $size bytes transferred\n"; }
189
        )
190
) {
191
    $sftp_status = 'successful';
192
    # Send success email
193
    if ( $email ) {
194
        $status_email_message = "OCLC upload was successful";
195
    }
196
    close $fh;
197
} else {
198
    $sftp_status = 'failed';
199
    # Send failure email
200
    if ( $email ) {
201
        $status_email_message = "OCLC upload failed\nOCLC error: " . $sftp->error;
202
    }
203
}
204
205
# Send email confirming the success or failure of the SFTP
206
if ( $email ) {
207
    $status_email->text_body($status_email_message);
208
    my $smtp_server = Koha::SMTP::Servers->get_default;
209
    $email->transport( $smtp_server->transport );
210
    try {
211
        $email->send_or_die;
212
    }
213
    catch {
214
        carp "Mail not sent: $_";
215
    };
216
}
217
218
cronlogaction({ action => 'End', info => "COMPLETED" });

Return to bug 36766