Brackup/Target/Ftp.pm untested

filter: Statementstmt 0.0% 0 / 166 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 321 CC 13 SCAR 16.7
CC
13
Cov
0%
_ls
12.0
_recurse
12.0
chunks
12.0
CRAP 5.3
1 package Brackup::Target::Ftp;
2 use strict;
3 use warnings;
4 use base 'Brackup::Target::Filebased';
5 use File::Basename;
6 use Net::FTP;
7  
8 sub new {
9 my ($class, $confsec) = @_;
10 my $self = $class->SUPER::new($confsec);
11  
12 $self->{ftp_host} = $confsec->value("ftp_host") or die 'No "ftp_host"';
13 $self->{ftp_user} = $confsec->value("ftp_user") or die 'No "ftp_user"';
14 $self->{ftp_password} = $confsec->value("ftp_password") or
15 die 'No "ftp_password"';
16 $self->{path} = $confsec->value("path") or
17 die 'No path specified';
18  
19 $self->_common_new;
20  
21 return $self;
22 }
23  
24 sub new_from_backup_header {
25 my ($class, $header, $confsec) = @_;
26 my $self = bless {}, $class;
27  
28 $self->{ftp_host} = $ENV{FTP_HOST} || $header->{'FtpHost'};
29 $self->{ftp_user} = $ENV{FTP_USER} || $header->{'FtpUser'};
30 $self->{ftp_password} = $ENV{FTP_PASSWORD} ||
31 $confsec->value('ftp_password') or
32 die "FTP_PASSWORD missing in environment";
33 $self->{path} = $header->{'BackupPath'} or
34 die "No BackupPath specified in the backup metafile.\n";
35  
36 $self->_common_new;
37  
38 return $self;
39 }
40  
41 sub _common_new {
42 my ($self) = @_;
43 $self->{retry_wait} = int($ENV{FTP_RETRY_WAIT} || 10);
44 $self->_connect();
45 }
46  
47 sub backup_header {
48 my ($self) = @_;
49 return {
50 "BackupPath" => $self->{path},
51 "FtpHost" => $self->{ftp_host},
52 "FtpUser" => $self->{ftp_user},
53 };
54 }
55  
56 sub nocolons {
57 # We never know what operating system is at the other end, thus never use
58 # colons.
59 return 1;
60 }
61  
62 sub _connect {
63 my ($self) = @_;
64  
65 $self->{ftp} = Net::FTP->new($self->{ftp_host}) or die $@;
66 $self->{ftp}->login($self->{ftp_user}, $self->{ftp_password});
67 $self->{ftp}->binary();
68 }
69  
70 sub _autoretry {
71 my ($self, $code) = @_;
72 my $result = $code->();
73  
74 if (!defined($result) && !$self->{ftp}->connected) {
75 warn "Error in FTP: " . $self->{ftp}->message . "\n";
76 sleep $self->{retry_wait};
77 warn "Trying to reconnect ...\n";
78 $self->_connect();
79 $result = $code->();
80 }
81  
82 return $result;
83 }
84  
85 sub _ls {
86 my ($self, $path) = @_;
87 my $result = $self->_autoretry(sub {
88 return $self->{ftp}->ls($path);
89 });
90 unless (defined($result)) {
91 die "Listing failed for $path: " . $self->{ftp}->message;
92 }
93 return wantarray ? @$result : $result;
94 }
95  
96 sub size {
97 my ($self, $path) = @_;
98 my $size = $self->_autoretry(sub {
99 return $self->{ftp}->size($path);
100 });
101 unless (defined($size)) {
102 die "Getting size for $path failed: " . $self->{ftp}->message;
103 }
104 return $size;
105 }
106  
107 sub _mdtm {
108 my ($self, $path) = @_;
109 my $mtime = $self->_autoretry(sub {
110 return $self->{ftp}->mdtm($path);
111 });
112 unless (defined $mtime) {
113 die "Getting mtime of $_ failed: " . $self->{ftp}->message;
114 }
115 return $mtime;
116 }
117  
118 sub _put_fh {
119 my ($self, $path, $fh) = @_;
120  
121 # Ugly-hack: monkey-patch IO::InnerFile to provide BINMODE for Net::FTP::put
122 *{IO::InnerFile::BINMODE} = sub { $_[0]->binmode }
123 if $fh->isa('IO::InnerFile');
124  
125 # Make sure directory exists.
126 my $dir = dirname($path);
127 $self->_autoretry(sub {
128 return $self->{ftp}->mkdir($dir, 1)
129 }) or die "Creating directory $dir failed: " . $self->{ftp}->message;
130  
131 $self->_autoretry(sub {
132 $self->{ftp}->put($fh, $path);
133 }) or die "Writing file $path failed: " . $self->{ftp}->message;
134 }
135  
136 sub _put_chunk {
137 my ($self, $path, $content) = @_;
138 open(my $fh, '<', \$content) or die $!;
139 $self->_put_fh($path, $fh);
140 }
141  
142 sub _get {
143 my ($self, $path) = @_;
144 my $content;
145  
146 $self->_autoretry(sub {
147 open(my $fh, '>', \$content) or die $!;
148 binmode($fh);
149 my $result = $self->{ftp}->get($path, $fh);
150 close($fh) or die "Failed to close";
151 return $result;
152 }) or die "Reading file $path failed: " . $self->{ftp}->message;
153  
154 return \$content;
155 }
156  
157 sub _delete {
158 my ($self, $path) = @_;
159 $self->_autoretry(sub {
160 return $self->{ftp}->delete($path);
161 }) or die "Removing file $path failed: " . $self->{ftp}->message;
162 }
163  
164 sub _recurse {
165 my ($self, $path, $maxdepth, $match) = @_;
166 return if $maxdepth < 0;
167 foreach ($self->_ls($path)) {
168 if ($match->($_)) {
169 $self->_recurse($_, $maxdepth - 1, $match);
170 }
171 }
172 }
173  
174 sub chunkpath {
175 my ($self, $dig) = @_;
176 return $self->{path} . '/' . $self->SUPER::chunkpath($dig);
177 }
178  
179 sub metapath {
180 my ($self, $name) = @_;
181 return $self->{path} . '/' . $self->SUPER::metapath($name);
182 }
183  
184 sub load_chunk {
185 my ($self, $dig) = @_;
186 return $self->_get($self->chunkpath($dig));
187 }
188  
189 sub store_chunk {
190 my ($self, $chunk) = @_;
191 my $dig = $chunk->backup_digest;
192 my $path = $self->chunkpath($dig);
193  
194 $self->_put_fh($path, $chunk->chunkref);
195  
196 my $actual_size = $self->size($path);
197 my $expected_size = $chunk->backup_length;
198 unless ($actual_size == $expected_size) {
199 die "Chunk $path incompletely written to disk: size is " .
200 "$actual_size, expecting $expected_size\n";
201 }
202  
203 return 1;
204 }
205  
206 sub delete_chunk {
207 my ($self, $dig) = @_;
208 $self->_delete($self->chunkpath($dig));
209 }
210  
211 # returns a list of names of all chunks
212 sub chunks {
213 my ($self) = @_;
214 my @chunks;
215  
216 $self->_recurse($self->{path}, 2, sub {
217 my ($path) = @_;
218 my $filename = basename($path);
219  
220 if ($path =~ m/\.chunk$/) {
221 $filename =~ s/\.chunk$//;
222 $filename =~ s/\./:/g if $self->nocolons;
223 push @chunks, $filename;
224 return 0;
225 }
226  
227 return $filename ne 'backups';
228 });
229  
230 return @chunks;
231 }
232  
233 sub store_backup_meta {
234 my ($self, $name, $fh) = @_;
235 $self->_put_fh($self->metapath("$name.brackup"), $fh);
236 return 1;
237 }
238  
239 sub backups {
240 my ($self) = @_;
241 my $list = $self->_ls($self->metapath());
242  
243 my @ret = ();
244 foreach (@$list) {
245 my $fn = basename($_);
246 next unless $fn =~ m/\.brackup$/;
247  
248 (my $bn = $fn) =~ s/\.brackup$//;
249  
250 my $path = $self->metapath($fn);
251 my $size = $self->size($path);
252 my $mtime = $self->_mdtm($path);
253  
254 push @ret, Brackup::TargetBackupStatInfo->new($self, $bn,
255 time => $mtime,
256 size => $size);
257 }
258  
259 return @ret;
260 }
261  
262 # downloads the given backup name to the current directory (with
263 # *.brackup extension) or to the specified location
264 sub get_backup {
265 my ($self, $name, $output_file) = @_;
266 my $path = $self->metapath("$name.brackup");
267  
268 $output_file ||= "$name.brackup";
269  
270 $self->_autoretry(sub {
271 open(my $out, '>', $output_file)
272 or die "Failed to open $output_file: $!\n";
273 my $result = $self->{ftp}->get($path, $out);
274 close($out) or die $!;
275 return $result;
276 }) or die "Reading file $path failed: " . $self->{ftp}->message;
277  
278 return 1;
279 }
280  
281 sub delete_backup {
282 my ($self, $name) = @_;
283 $self->_delete($self->metapath("$name.brackup"));
284 return 1;
285 }
286  
287 1;
288  
289  
290 =head1 NAME
291  
292 Brackup::Target::Ftp - backup to an FTP server
293  
294 =head1 DESCRIPTION
295  
296 Back up to an FTP server.
297  
298 =head1 EXAMPLE
299  
300 In your ~/.brackup.conf file:
301  
302 [TARGET:nfs_in_garage]
303 type = Ftp
304 path = .
305 ftp_host = ftp.domain.tld
306 ftp_user = user
307 ftp_password = password
308  
309 =head1 CONFIG OPTIONS
310  
311 =over
312  
313 =item B<type>
314  
315 Must be "B<Ftp>".
316  
317 =item B<path>
318  
319 Server-side path, can be ".".
320  
321 =item B<ftp_host>
322  
323 FTP server host, optionally with port (host:port).
324  
325 =item B<ftp_user>
326  
327 Username to use.
328  
329 =item B<ftp_password>
330  
331 Password to use.
332  
333 =back
334  
335 =head1 SEE ALSO
336  
337 L<Brackup::Target>