Debian Codegraph @

application-src/amzfs-sendrecv.pl

raw
1 #!@PERL@
2 # Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved.
3 # Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #
19 # Contact information: Carbonite Inc., 756 N Pastoria Ave
20 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
21
22 use lib '@amperldir@';
23 use strict;
24 use warnings;
25 use Getopt::Long;
26
27 package Amanda::Application::Amzfs_sendrecv;
28 use base qw(Amanda::Application Amanda::Application::Zfs);
29 use File::Copy;
30 use File::Path;
31 use IPC::Open3;
32 use Sys::Hostname;
33 use Symbol;
34 use Amanda::Constants;
35 use Amanda::Config qw( :init :getconf config_dir_relative );
36 use Amanda::Debug qw( :logging );
37 use Amanda::Paths;
38 use Amanda::Util qw( :constants quote_string );
39
40 sub new {
41 my $class = shift;
42 my ($config, $host, $disk, $device, $level, $index, $message, $collection, $record, $df_path, $zfs_path, $pfexec_path, $pfexec, $keep_snapshot, $exclude_list, $include_list, $target) = @_;
43 my $self = $class->SUPER::new($config);
44
45 $self->{config} = $config;
46 $self->{host} = $host;
47 if (defined $disk) {
48 $self->{disk} = $disk;
49 } else {
50 $self->{disk} = $device;
51 }
52 if (defined $device) {
53 $self->{device} = $device;
54 } else {
55 $self->{device} = $disk;
56 }
57 $self->{level} = [ @{$level} ];
58 $self->{index} = $index;
59 $self->{message} = $message;
60 $self->{collection} = $collection;
61 $self->{record} = $record;
62 $self->{df_path} = $df_path;
63 $self->{zfs_path} = $zfs_path;
64 $self->{pfexec_path} = $pfexec_path;
65 $self->{pfexec} = $pfexec;
66 $self->{keep_snapshot} = $keep_snapshot;
67 $self->{pfexec_cmd} = undef;
68 $self->{exclude_list} = [ @{$exclude_list} ];
69 $self->{include_list} = [ @{$include_list} ];
70 $self->{target} = $target;
71
72 if ($self->{keep_snapshot} =~ /^YES$/i) {
73 $self->{keep_snapshot} = "YES";
74 if (!defined $self->{record}) {
75 $self->{keep_snapshot} = "NO";
76 }
77 }
78
79 return $self;
80 }
81
82 sub check_for_backup_failure {
83 my $self = shift;
84
85 $self->zfs_destroy_snapshot();
86 }
87
88 sub command_support {
89 my $self = shift;
90
91 print "CONFIG YES\n";
92 print "HOST YES\n";
93 print "DISK YES\n";
94 print "MAX-LEVEL 9\n";
95 print "INDEX-LINE YES\n";
96 print "INDEX-XML NO\n";
97 print "MESSAGE-LINE YES\n";
98 print "MESSAGE-XML NO\n";
99 print "RECORD YES\n";
100 print "COLLECTION NO\n";
101 print "CLIENT-ESTIMATE YES\n";
102 }
103
104 sub command_selfcheck {
105 my $self = shift;
106
107 $self->print_to_server("disk " . quote_string($self->{disk}),
108 $Amanda::Script_App::GOOD);
109
110 $self->print_to_server("amzfs-sendrecv version " . $Amanda::Constants::VERSION,
111 $Amanda::Script_App::GOOD);
112 $self->zfs_set_value();
113
114 if (!defined $self->{device}) {
115 return;
116 }
117
118 if ($self->{error_status} == $Amanda::Script_App::GOOD) {
119 $self->zfs_create_snapshot();
120 $self->zfs_destroy_snapshot();
121 print "OK " . $self->{device} . "\n";
122 }
123
124 if ($#{$self->{include_list}} >= 0) {
125 $self->print_to_server("include-list not supported for backup",
126 $Amanda::Script_App::ERROR);
127 }
128 if ($#{$self->{exclude_list}} >= 0) {
129 $self->print_to_server("exclude-list not supported for backup",
130 $Amanda::Script_App::ERROR);
131 }
132 }
133
134 sub command_estimate() {
135 my $self = shift;
136 my $level = 0;
137
138 if ($#{$self->{include_list}} >= 0) {
139 $self->print_to_server("include-list not supported for backup",
140 $Amanda::Script_App::ERROR);
141 }
142 if ($#{$self->{exclude_list}} >= 0) {
143 $self->print_to_server("exclude-list not supported for backup",
144 $Amanda::Script_App::ERROR);
145 }
146
147 $self->zfs_set_value();
148 $self->zfs_create_snapshot();
149
150 while (defined ($level = shift @{$self->{level}})) {
151 debug "Estimate of level $level";
152 my $size = $self->estimate_snapshot($level);
153 output_size($level, $size);
154 }
155
156 $self->zfs_destroy_snapshot();
157 }
158
159 sub output_size {
160 my($level) = shift;
161 my($size) = shift;
162 if($size == -1) {
163 print "$level -1 -1\n";
164 #exit 2;
165 }
166 else {
167 my($ksize) = int $size / (1024);
168 $ksize=32 if ($ksize<32);
169 print "$level $ksize 1\n";
170 }
171 }
172
173 sub command_backup {
174 my $self = shift;
175
176 if ($#{$self->{include_list}} >= 0) {
177 $self->print_to_server("include-list not supported for backup",
178 $Amanda::Script_App::ERROR);
179 }
180 if ($#{$self->{exclude_list}} >= 0) {
181 $self->print_to_server("exclude-list not supported for backup",
182 $Amanda::Script_App::ERROR);
183 }
184
185 $self->zfs_set_value();
186 $self->zfs_create_snapshot();
187
188 my $size = -1;
189 my $level = $self->{level}[0];
190 my $cmd;
191 debug "Backup of level $level";
192 if ($level == 0) {
193 $cmd = "$self->{pfexec_cmd} $self->{zfs_path} send $self->{filesystem}\@$self->{snapshot} | $Amanda::Paths::amlibexecdir/teecount";
194 } else {
195 my $refsnapshotname = $self->zfs_find_snapshot_level($level-1);
196 debug "Referenced snapshot name: $refsnapshotname|";
197 if ($refsnapshotname ne "") {
198 $cmd = "$self->{pfexec_cmd} $self->{zfs_path} send -i $self->{filesystem}\@$refsnapshotname $self->{filesystem}\@$self->{snapshot} | $Amanda::Paths::amlibexecdir/teecount";
199 } else {
200 $self->print_to_server_and_die("cannot backup snapshot '$self->{filesystem}\@$self->{snapshot}': reference snapshot doesn't exists for level $level", $Amanda::Script_App::ERROR);
201 }
202 }
203
204 debug "running (backup): $cmd";
205 my($wtr, $err, $pid);
206 my($errmsg);
207 $err = Symbol::gensym;
208 $pid = open3($wtr, '>&STDOUT', $err, $cmd);
209 close $wtr;
210
211 if (defined($self->{index})) {
212 my $indexout;
213 open($indexout, '>&=4') ||
214 $self->print_to_server_and_die("Can't open indexout: $!",
215 $Amanda::Script_App::ERROR);
216 print $indexout "/\n";
217 close($indexout);
218 }
219
220 $errmsg = <$err>;
221 waitpid $pid, 0;
222 close $err;
223 if ($? != 0) {
224 if (defined $errmsg) {
225 $self->print_to_server_and_die($errmsg, $Amanda::Script_App::ERROR);
226 } else {
227 $self->print_to_server_and_die("cannot backup snapshot '$self->{filesystem}\@$self->{snapshot}': unknown reason", $Amanda::Script_App::ERROR);
228 }
229 }
230 $size = $errmsg;
231 debug "Dump done";
232
233 my($ksize) = int ($size/1024);
234 $ksize=32 if ($ksize<32);
235
236 print {$self->{mesgout}} "sendbackup: size $ksize\n";
237
238 # destroy all snapshot of this level and higher
239 $self->zfs_purge_snapshot($level, 9);
240
241 if ($self->{keep_snapshot} eq 'YES') {
242 $self->zfs_rename_snapshot($level);
243 } else {
244 $self->zfs_destroy_snapshot();
245 }
246 }
247
248 sub estimate_snapshot
249 {
250 my $self = shift;
251 my $level = shift;
252
253 debug "\$filesystem = $self->{filesystem}";
254 debug "\$snapshot = $self->{snapshot}";
255 debug "\$level = $level";
256
257 my $cmd;
258 if ($level == 0) {
259 $cmd = "$self->{pfexec_cmd} $self->{zfs_path} get -Hp -o value referenced $self->{filesystem}\@$self->{snapshot}";
260 } else {
261 my $refsnapshotname = $self->zfs_find_snapshot_level($level-1);
262 debug "Referenced snapshot name: $refsnapshotname|";
263 if ($refsnapshotname ne "") {
264 $cmd = "$self->{pfexec_cmd} $self->{zfs_path} send -i $refsnapshotname $self->{filesystem}\@$self->{snapshot} | /usr/bin/wc -c";
265 } else {
266 return "-1";
267 }
268 }
269 debug "running (estimate): $cmd";
270 my($wtr, $rdr, $err, $pid);
271 $err = Symbol::gensym;
272 $pid = open3($wtr, $rdr, $err, $cmd);
273 close $wtr;
274 my ($msg) = <$rdr>;
275 my ($errmsg) = <$err>;
276 waitpid $pid, 0;
277 close $rdr;
278 close $err;
279 if ($? != 0) {
280 if (defined $msg && defined $errmsg) {
281 $self->print_to_server_and_die("$msg, $errmsg", $Amanda::Script_App::ERROR);
282 } elsif (defined $msg) {
283 $self->print_to_server_and_die($msg, $Amanda::Script_App::ERROR);
284 } elsif (defined $errmsg) {
285 $self->print_to_server_and_die($errmsg, $Amanda::Script_App::ERROR);
286 } else {
287 $self->print_to_server_and_die("cannot estimate snapshot '$self->{snapshot}\@$self->{snapshot}': unknown reason", $Amanda::Script_App::ERROR);
288 }
289 }
290 if ($level == 0) {
291 my $compratio = $self->get_compratio();
292 $compratio =~ s/x$//;
293 $msg *= $compratio;
294 }
295
296 return $msg;
297 }
298
299 sub get_compratio
300 {
301 my $self = shift;
302
303 my $cmd;
304 $cmd = "$self->{pfexec_cmd} $self->{zfs_path} get -Hp -o value compressratio $self->{filesystem}\@$self->{snapshot}";
305 debug "running (get-compression): $cmd";
306 my($wtr, $rdr, $err, $pid);
307 $err = Symbol::gensym;
308 $pid = open3($wtr, $rdr, $err, $cmd);
309 close $wtr;
310 my ($msg) = <$rdr>;
311 chomp($msg) if defined $msg;
312 my ($errmsg) = <$err>;
313 chomp($errmsg) if defined $errmsg;
314 waitpid $pid, 0;
315 close $rdr;
316 close $err;
317 if ($? != 0) {
318 if (defined $msg && defined $errmsg) {
319 $self->print_to_server_and_die("$msg, $errmsg", $Amanda::Script_App::ERROR);
320 } elsif (defined $msg) {
321 $self->print_to_server_and_die($msg, $Amanda::Script_App::ERROR);
322 } elsif (defined $errmsg) {
323 $self->print_to_server_and_die($errmsg, $Amanda::Script_App::ERROR);
324 } else {
325 $self->print_to_server_and_die("cannot read compression ratio '$self->{snapshot}\@$self->{snapshot}': unknown reason", $Amanda::Script_App::ERROR);
326 }
327 }
328 return $msg
329 }
330
331 sub command_index {
332 my $self = shift;
333 my $buffer;
334
335 print "/\n";
336
337 do {
338 sysread STDIN, $buffer, 1048576;
339 } while (defined $buffer and length($buffer) > 0);
340
341 }
342
343 sub command_index_from_image {
344 }
345
346 sub command_restore {
347 my $self = shift;
348
349 my $current_snapshot;
350 my $level = $self->{level}[0];
351 my $device = $self->{device};
352 if (defined $device) {
353 $device =~ s,^/,,;
354 $current_snapshot = $self->zfs_build_snapshotname($device);
355 $self->{'snapshot'} = $self->zfs_build_snapshotname($device, $level);
356 }
357
358 my $directory = $device;
359 $directory = $self->{target} if defined $self->{target};
360 $directory =~ s,^/,,;
361
362 my @cmd = ();
363
364 if ($self->{pfexec_cmd}) {
365 push @cmd, $self->{pfexec_cmd};
366 }
367 push @cmd, $self->{zfs_path};
368 push @cmd, "recv";
369 push @cmd, $directory;
370
371 debug("cmd:" . join(" ", @cmd));
372 system @cmd;
373
374 my $snapshotname;
375 my $newsnapname;
376 if (defined $device) {
377 $snapshotname = "$directory\@$current_snapshot";
378 $newsnapname = "$directory\@$self->{'snapshot'}";
379 } else {
380 # find snapshot name
381 @cmd = ();
382 if ($self->{pfexec_cmd}) {
383 push @cmd, $self->{pfexec_cmd};
384 }
385 push @cmd, $self->{zfs_path};
386 push @cmd, "list";
387 push @cmd, "-r";
388 push @cmd, "-t";
389 push @cmd, "snapshot";
390 push @cmd, $directory;
391 debug("cmd:" . join(" ", @cmd));
392
393 my($wtr, $rdr, $err, $pid);
394 my($msg, $errmsg);
395 $err = Symbol::gensym;
396 $pid = open3($wtr, $rdr, $err, @cmd);
397 close $wtr;
398 while ($msg = <$rdr>) {
399 next if $msg =~ /^NAME/;
400 my ($name, $used, $avail) = split(/ +/, $msg);
401 if ($name =~ /-current$/) {
402 $snapshotname = $name;
403 last;
404 }
405 }
406 $errmsg = <$err>;
407 waitpid $pid, 0;
408 close $rdr;
409 close $err;
410
411 if (defined $snapshotname and defined($level)) {
412 $newsnapname = $snapshotname;
413 $newsnapname =~ s/current$/$level/;
414 } else {
415 # destroy the snapshot
416 # restoring next level will fail.
417 @cmd = ();
418 if ($self->{pfexec_cmd}) {
419 push @cmd, $self->{pfexec_cmd};
420 }
421 push @cmd, $self->{zfs_path};
422 push @cmd, "destroy";
423 push @cmd, $snapshotname;
424
425 debug("cmd:" . join(" ", @cmd));
426 system @cmd;
427 }
428 }
429
430 if (defined $newsnapname) {
431 # rename -current snapshot to -level
432 @cmd = ();
433 if ($self->{pfexec_cmd}) {
434 push @cmd, $self->{pfexec_cmd};
435 }
436 push @cmd, $self->{zfs_path};
437 push @cmd, "rename";
438 push @cmd, $snapshotname;
439 push @cmd, $newsnapname;
440
441 debug("cmd:" . join(" ", @cmd));
442 system @cmd;
443 }
444 }
445
446 sub command_print_command {
447 }
448
449 package main;
450
451 sub usage {
452 print <<EOF;
453 Usage: amzfs-sendrecv <command> --config=<config> --host=<host> --disk=<disk> --device=<device> --level=<level> --index=<yes|no> --message=<text> --collection=<no> --record=<yes|no> --df-path=<path/to/df> --zfs-path=<path/to/zfs> --pfexec-path=<path/to/pfexec> --pfexec=<yes|no> --keep-snapshot=<yes|no>.
454 EOF
455 exit(1);
456 }
457
458 my $opt_version;
459 my $opt_config;
460 my $opt_host;
461 my $opt_disk;
462 my $opt_device;
463 my @opt_level;
464 my $opt_index;
465 my $opt_message;
466 my $opt_collection;
467 my $opt_record;
468 my $df_path = 'df';
469 my $zfs_path = 'zfs';
470 my $pfexec_path = 'pfexec';
471 my $pfexec = "NO";
472 my $opt_keep_snapshot = "YES";
473 my @opt_exclude_list;
474 my @opt_include_list;
475 my $opt_target;
476
477 my @orig_argv = @ARGV;
478
479 Getopt::Long::Configure(qw{bundling});
480 GetOptions(
481 'version' => \$opt_version,
482 'config=s' => \$opt_config,
483 'host=s' => \$opt_host,
484 'disk=s' => \$opt_disk,
485 'device=s' => \$opt_device,
486 'level=s' => \@opt_level,
487 'index=s' => \$opt_index,
488 'message=s' => \$opt_message,
489 'collection=s' => \$opt_collection,
490 'record' => \$opt_record,
491 'df-path=s' => \$df_path,
492 'zfs-path=s' => \$zfs_path,
493 'pfexec-path=s' => \$pfexec_path,
494 'pfexec=s' => \$pfexec,
495 'keep-snapshot=s' => \$opt_keep_snapshot,
496 'exclude-list=s' => \@opt_exclude_list,
497 'include-list=s' => \@opt_include_list,
498 'target|directory=s' => \$opt_target,
499 ) or usage();
500
501 if (defined $opt_version) {
502 print "amzfs-sendrecv-" . $Amanda::Constants::VERSION , "\n";
503 exit(0);
504 }
505
506 my $application = Amanda::Application::Amzfs_sendrecv->new($opt_config, $opt_host, $opt_disk, $opt_device, \@opt_level, $opt_index, $opt_message, $opt_collection, $opt_record, $df_path, $zfs_path, $pfexec_path, $pfexec, $opt_keep_snapshot, \@opt_exclude_list, \@opt_include_list, $opt_target);
507
508 Amanda::Debug::debug("Arguments: " . join(' ', @orig_argv));
509
510 $application->do($ARGV[0]);
511 # NOTREACHED