2 # Copyright (c) 2008, 2009, 2010 Zmanda, Inc. All Rights Reserved.
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.
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
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
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 use lib '@amperldir@';
25 package Amanda::Application::Amzfs_sendrecv;
26 use base qw(Amanda::Application Amanda::Application::Zfs);
32 use Amanda::Constants;
33 use Amanda::Config qw( :init :getconf config_dir_relative );
34 use Amanda::Debug qw( :logging );
36 use Amanda::Util qw( :constants );
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);
43 $self->{config} = $config;
44 $self->{host} = $host;
46 $self->{disk} = $disk;
48 $self->{disk} = $device;
50 if (defined $device) {
51 $self->{device} = $device;
53 $self->{device} = $disk;
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;
70 if ($self->{keep_snapshot} =~ /^YES$/i) {
71 $self->{keep_snapshot} = "YES";
72 if (!defined $self->{record}) {
73 $self->{keep_snapshot} = "NO";
80 sub check_for_backup_failure {
83 $self->zfs_destroy_snapshot();
92 print "MAX-LEVEL 9\n";
93 print "INDEX-LINE NO\n";
94 print "INDEX-XML NO\n";
95 print "MESSAGE-LINE YES\n";
96 print "MESSAGE-XML NO\n";
98 print "COLLECTION NO\n";
99 print "CLIENT-ESTIMATE YES\n";
102 sub command_selfcheck {
105 $self->zfs_set_value();
107 if (!defined $self->{device}) {
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";
118 if ($#{$self->{include_list}} >= 0) {
119 $self->print_to_server("include-list not supported for backup",
120 $Amanda::Script_App::ERROR);
122 if ($#{$self->{exclude_list}} >= 0) {
123 $self->print_to_server("exclude-list not supported for backup",
124 $Amanda::Script_App::ERROR);
128 sub command_estimate() {
132 if ($#{$self->{include_list}} >= 0) {
133 $self->print_to_server("include-list not supported for backup",
134 $Amanda::Script_App::ERROR);
136 if ($#{$self->{exclude_list}} >= 0) {
137 $self->print_to_server("exclude-list not supported for backup",
138 $Amanda::Script_App::ERROR);
141 $self->zfs_set_value();
142 $self->zfs_create_snapshot();
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);
150 $self->zfs_destroy_snapshot();
159 print "$level -1 -1\n";
163 my($ksize) = int $size / (1024);
164 $ksize=32 if ($ksize<32);
165 print "$level $ksize 1\n";
172 if ($#{$self->{include_list}} >= 0) {
173 $self->print_to_server("include-list not supported for backup",
174 $Amanda::Script_App::ERROR);
176 if ($#{$self->{exclude_list}} >= 0) {
177 $self->print_to_server("exclude-list not supported for backup",
178 $Amanda::Script_App::ERROR);
181 $self->zfs_set_value();
182 $self->zfs_create_snapshot();
185 my $level = $self->{level}[0];
187 debug "Backup of level $level";
189 $cmd = "$self->{pfexec_cmd} $self->{zfs_path} send $self->{filesystem}\@$self->{snapshot} | $Amanda::Paths::amlibexecdir/teecount";
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 $refsnapshotname $self->{filesystem}\@$self->{snapshot} | $Amanda::Paths::amlibexecdir/teecount";
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);
200 debug "running (backup): $cmd";
201 my($wtr, $err, $pid);
203 $err = Symbol::gensym;
204 $pid = open3($wtr, '>&STDOUT', $err, $cmd);
210 if (defined $errmsg) {
211 $self->print_to_server_and_die($errmsg, $Amanda::Script_App::ERROR);
213 $self->print_to_server_and_die("cannot backup snapshot '$self->{filesystem}\@$self->{snapshot}': unknown reason", $Amanda::Script_App::ERROR);
219 my($ksize) = int ($size/1024);
220 $ksize=32 if ($ksize<32);
222 print {$self->{mesgout}} "sendbackup: size $ksize\n";
223 print {$self->{mesgout}} "sendbackup: end\n";
225 # destroy all snapshot of this level and higher
226 $self->zfs_purge_snapshot($level, 9);
228 if ($self->{keep_snapshot} eq 'YES') {
229 $self->zfs_rename_snapshot($level);
231 $self->zfs_destroy_snapshot();
237 sub estimate_snapshot
242 debug "\$filesystem = $self->{filesystem}";
243 debug "\$snapshot = $self->{snapshot}";
244 debug "\$level = $level";
248 $cmd = "$self->{pfexec_cmd} $self->{zfs_path} get -Hp -o value referenced $self->{filesystem}\@$self->{snapshot}";
250 my $refsnapshotname = $self->zfs_find_snapshot_level($level-1);
251 debug "Referenced snapshot name: $refsnapshotname|";
252 if ($refsnapshotname ne "") {
253 $cmd = "$self->{pfexec_cmd} $self->{zfs_path} send -i $refsnapshotname $self->{filesystem}\@$self->{snapshot} | /usr/bin/wc -c";
258 debug "running (estimate): $cmd";
259 my($wtr, $rdr, $err, $pid);
260 $err = Symbol::gensym;
261 $pid = open3($wtr, $rdr, $err, $cmd);
264 my ($errmsg) = <$err>;
269 if (defined $msg && defined $errmsg) {
270 $self->print_to_server_and_die("$msg, $errmsg", $Amanda::Script_App::ERROR);
271 } elsif (defined $msg) {
272 $self->print_to_server_and_die($msg, $Amanda::Script_App::ERROR);
273 } elsif (defined $errmsg) {
274 $self->print_to_server_and_die($errmsg, $Amanda::Script_App::ERROR);
276 $self->print_to_server_and_die("cannot estimate snapshot '$self->{snapshot}\@$self->{snapshot}': unknown reason", $Amanda::Script_App::ERROR);
280 my $compratio = $self->get_compratio();
293 $cmd = "$self->{pfexec_cmd} $self->{zfs_path} get -Hp -o value compressratio $self->{filesystem}\@$self->{snapshot}";
294 debug "running (get-compression): $cmd";
295 my($wtr, $rdr, $err, $pid);
296 $err = Symbol::gensym;
297 $pid = open3($wtr, $rdr, $err, $cmd);
300 my ($errmsg) = <$err>;
305 if (defined $msg && defined $errmsg) {
306 $self->print_to_server_and_die("$msg, $errmsg", $Amanda::Script_App::ERROR);
307 } elsif (defined $msg) {
308 $self->print_to_server_and_die($msg, $Amanda::Script_App::ERROR);
309 } elsif (defined $errmsg) {
310 $self->print_to_server_and_die($errmsg, $Amanda::Script_App::ERROR);
312 $self->print_to_server_and_die("cannot read compression ratio '$self->{snapshot}\@$self->{snapshot}': unknown reason", $Amanda::Script_App::ERROR);
318 sub command_index_from_output {
321 sub command_index_from_image {
324 #sub command_restore {
330 sub command_print_command {
337 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>.
352 my $zfs_path = 'zfs';
353 my $pfexec_path = 'pfexec';
355 my $opt_keep_snapshot = "YES";
356 my @opt_exclude_list;
357 my @opt_include_list;
360 Getopt::Long::Configure(qw{bundling});
362 'config=s' => \$opt_config,
363 'host=s' => \$opt_host,
364 'disk=s' => \$opt_disk,
365 'device=s' => \$opt_device,
366 'level=s' => \@opt_level,
367 'index=s' => \$opt_index,
368 'message=s' => \$opt_message,
369 'collection=s' => \$opt_collection,
370 'record' => \$opt_record,
371 'df-path=s' => \$df_path,
372 'zfs-path=s' => \$zfs_path,
373 'pfexec-path=s' => \$pfexec_path,
374 'pfexec=s' => \$pfexec,
375 'keep-snapshot=s' => \$opt_keep_snapshot,
376 'exclude-list=s' => \@opt_exclude_list,
377 'include-list=s' => \@opt_include_list,
378 'directory=s' => \$opt_directory,
381 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);
383 $application->do($ARGV[0]);