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