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

(-)a/misc/cronjobs/sftp_file.pl (-1 / +226 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
sftp_file.pl [ -h | -m ] [ -v ] host user pass upload_dir port file email
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
   --port          SFTP upload port to use - falls back to port 22
44
   --file          File to SFTP to host
45
   --email         Email address to receive confirmation of SFTP upload success or failure
46
47
=head1 OPTIONS
48
49
=over
50
51
=item B<--help>
52
53
Print brief help and exit.
54
55
=item B<--man>
56
57
Print full documentation and exit.
58
59
=item B<--verbose>
60
61
Increased verbosity, reports successes and errors.
62
63
=item B<--host>
64
65
Remote server host to SFTP file to.
66
67
=item B<--user>
68
69
Username credential to authenticate to remote host.
70
71
=item B<--pass>
72
73
Password credential to authenticate to remote host.
74
75
=item B<--upload_dir>
76
77
Directory on remote host to SFTP file to.
78
79
=item B<--port>
80
81
Remote host port to use for SFTP upload. Koha will fallback to port 22 if this parameter is not defined.
82
83
=item B<--file>
84
85
File to SFTP to remote host.
86
87
=item B<--email>
88
89
Email address to receive the confirmation if the SFTP upload was a success or failure.
90
91
=back
92
93
=head1 DESCRIPTION
94
95
This script is designed to SFTP files.
96
97
=head1 DESCRIPTION
98
99
This script is designed to SFTP files to a remote server.
100
101
=head1 USAGE EXAMPLES
102
103
B<sftp_file.pl --host test.com --user test --pass 123cne --upload_dir uploads --file /tmp/test.mrc>
104
105
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.
106
107
=head1 TO DO
108
109
=back
110
111
=cut
112
113
# These variables can be set by command line options,
114
# initially set to default values.
115
116
my $help                 = 0;
117
my $man                  = 0;
118
my $verbose              = 0;
119
my $host                 = undef;
120
my $user                 = undef;
121
my $pass                 = undef;
122
my $upload_dir           = undef;
123
my $port                 = 22;
124
my $file                 = 0;
125
my $email                = 0;
126
my $admin_address        = undef;
127
my $status_email         = undef;
128
my $status_email_message = undef;
129
my $sftp_status          = undef;
130
131
my $command_line_options = join(" ",@ARGV);
132
133
GetOptions(
134
    'help|?'         => \$help,
135
    'man'            => \$man,
136
    'verbose'        => \$verbose,
137
    'host=s'         => \$host,
138
    'user=s'         => \$user,
139
    'pass=s'         => \$pass,
140
    'upload_dir=s'   => \$upload_dir,
141
    'port=s'         => \$port,
142
    'file=s'         => \$file,
143
    'email=s'        => \$email,
144
) or pod2usage(2);
145
pod2usage( -verbose => 2 ) if ($man);
146
pod2usage( -verbose => 2 ) if ($help and $verbose);
147
pod2usage(1) if $help;
148
149
cronlogaction({ info => $command_line_options });
150
151
# Check we have all the SFTP details we need
152
if ( !$user || !$pass || !$host || !$upload_dir ) {
153
    pod2usage(q|Please provide details for sftp username, password, upload directory, and host|);
154
}
155
156
# Prepare the SFTP connection
157
my $sftp = Net::SFTP::Foreign->new(
158
    host           => $host,
159
    user           => $user,
160
    password       => $pass,
161
    port           => $port,
162
    timeout        => 10,
163
    stderr_discard => 1,
164
);
165
$sftp->die_on_error( "Cannot ssh to $host" . $sftp->error );
166
167
# Change to remote directory
168
$sftp->setcwd( $upload_dir )
169
    or return warn "Cannot change remote dir : $sftp->error\n";
170
171
# If the --email parameter is defined then prepare sending an email confirming the success
172
# or failure of the SFTP
173
if ( $email ) {
174
    if ( C4::Context->preference('KohaAdminEmailAddress') ) {
175
        $admin_address = C4::Context->preference('KohaAdminEmailAddress');
176
    }
177
178
    if ( !Koha::Email->is_valid($email) ) {
179
        return warn "The email address you defined in the --email parameter is invalid\n";
180
    }
181
182
    $status_email = Koha::Email->new(
183
        {
184
            to        => $email,
185
            from      => $admin_address,
186
            subject   => '[OCLC] ' . C4::Context->config('database') . ' ' . $sftp_status,
187
        }
188
    );
189
}
190
191
# Do the SFTP upload
192
open my $fh, '<', $file;
193
if (
194
    $sftp->put(
195
        $fh, $file,
196
            callback => sub { my ( $sftp, $data, $offset, $size ) = @_; warn "$offset of $size bytes transferred\n"; }
197
        )
198
) {
199
    $sftp_status = 'successful';
200
    # Send success email
201
    if ( $email ) {
202
        $status_email_message = "OCLC upload was successful";
203
    }
204
    close $fh;
205
} else {
206
    $sftp_status = 'failed';
207
    # Send failure email
208
    if ( $email ) {
209
        $status_email_message = "OCLC upload failed\nOCLC error: " . $sftp->error;
210
    }
211
}
212
213
# Send email confirming the success or failure of the SFTP
214
if ( $email ) {
215
    $status_email->text_body($status_email_message);
216
    my $smtp_server = Koha::SMTP::Servers->get_default;
217
    $email->transport( $smtp_server->transport );
218
    try {
219
        $email->send_or_die;
220
    }
221
    catch {
222
        carp "Mail not sent: $_";
223
    };
224
}
225
226
cronlogaction({ action => 'End', info => "COMPLETED" });

Return to bug 36766