2 # Copyright (c) 2008,2009 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@';
24 package Amanda::Application::Amzfs_sendrecv;
25 use base qw(Amanda::Application Amanda::Application::Zfs);
31 use Amanda::Constants;
32 use Amanda::Config qw( :init :getconf config_dir_relative );
33 use Amanda::Debug qw( :logging );
35 use Amanda::Util qw( :constants );
39 my ($config, $host, $disk, $device, $level, $index, $message, $collection, $record, $df_path, $zfs_path, $pfexec_path, $pfexec, $keep_snapshot, $exclude_list, $include_list, $directory) = @_;
40 my $self = $class->SUPER::new($config);
42 $self->{config} = $config;
43 $self->{host} = $host;
45 $self->{disk} = $disk;
47 $self->{disk} = $device;
49 if (defined $device) {
50 $self->{device} = $device;
52 $self->{device} = $disk;
54 $self->{level} = [ @{$level} ];
55 $self->{index} = $index;
56 $self->{message} = $message;
57 $self->{collection} = $collection;
58 $self->{record} = $record;
59 $self->{df_path} = $df_path;
60 $self->{zfs_path} = $zfs_path;
61 $self->{pfexec_path} = $pfexec_path;
62 $self->{pfexec} = $pfexec;
63 $self->{keep_snapshot} = $keep_snapshot;
64 $self->{pfexec_cmd} = undef;
65 $self->{exclude_list} = [ @{$exclude_list} ];
66 $self->{include_list} = [ @{$include_list} ];
67 $self->{directory} = $directory;
69 if ($self->{keep_snapshot} =~ /^YES$/i) {
70 $self->{keep_snapshot} = "YES";
71 if (!defined $self->{record}) {
72 $self->{keep_snapshot} = "NO";
79 sub check_for_backup_failure {
82 $self->zfs_destroy_snapshot();
91 print "MAX-LEVEL 9\n";
92 print "INDEX-LINE NO\n";
93 print "INDEX-XML NO\n";
94 print "MESSAGE-LINE YES\n";
95 print "MESSAGE-XML NO\n";
97 print "COLLECTION NO\n";
98 print "CLIENT-ESTIMATE YES\n";
101 sub command_selfcheck {
104 $self->zfs_set_value();
106 if (!defined $self->{device}) {
110 if ($self->{error_status} == $Amanda::Script_App::GOOD) {
111 $self->zfs_create_snapshot();
112 $self->zfs_destroy_snapshot();
113 print "OK " . $self->{disk} . "\n";
114 print "OK " . $self->{device} . "\n";
117 if ($#{$self->{include_list}} >= 0) {
118 $self->print_to_server("include-list not supported for backup",
119 $Amanda::Script_App::ERROR);
121 if ($#{$self->{exclude_list}} >= 0) {
122 $self->print_to_server("exclude-list not supported for backup",
123 $Amanda::Script_App::ERROR);
127 sub command_estimate() {
131 if ($#{$self->{include_list}} >= 0) {
132 $self->print_to_server("include-list not supported for backup",
133 $Amanda::Script_App::ERROR);
135 if ($#{$self->{exclude_list}} >= 0) {
136 $self->print_to_server("exclude-list not supported for backup",
137 $Amanda::Script_App::ERROR);
140 $self->zfs_set_value();
141 $self->zfs_create_snapshot();
143 while (defined ($level = shift @{$self->{level}})) {
144 debug "Estimate of level $level";
145 my $size = $self->estimate_snapshot($level);
146 output_size($level, $size);
149 $self->zfs_destroy_snapshot();
158 print "$level -1 -1\n";
162 my($ksize) = int $size / (1024);
163 $ksize=32 if ($ksize<32);
164 print "$level $ksize 1\n";
172 open($mesgout_fd, '>&=3') ||
173 $self->print_to_server_and_die("Can't open mesgout_fd: $!",
174 $Amanda::Script_App::ERROR);
175 $self->{mesgout} = $mesgout_fd;
177 if ($#{$self->{include_list}} >= 0) {
178 $self->print_to_server("include-list not supported for backup",
179 $Amanda::Script_App::ERROR);
181 if ($#{$self->{exclude_list}} >= 0) {
182 $self->print_to_server("exclude-list not supported for backup",
183 $Amanda::Script_App::ERROR);
186 $self->zfs_set_value();
187 $self->zfs_create_snapshot();
190 my $level = $self->{level}[0];
192 debug "Backup of level $level";
194 $cmd = "$self->{pfexec_cmd} $self->{zfs_path} send $self->{filesystem}\@$self->{snapshot} | $Amanda::Paths::amlibexecdir/teecount";
196 my $refsnapshotname = $self->zfs_find_snapshot_level($level-1);
197 debug "Referenced snapshot name: $refsnapshotname|";
198 if ($refsnapshotname ne "") {
199 $cmd = "$self->{pfexec_cmd} $self->{zfs_path} send -i $refsnapshotname $self->{filesystem}\@$self->{snapshot} | $Amanda::Paths::amlibexecdir/teecount";
201 $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);
205 debug "running (backup): $cmd";
206 my($wtr, $err, $pid);
208 $err = Symbol::gensym;
209 $pid = open3($wtr, '>&STDOUT', $err, $cmd);
215 if (defined $errmsg) {
216 $self->print_to_server_and_die($errmsg, $Amanda::Script_App::ERROR);
218 $self->print_to_server_and_die("cannot backup snapshot '$self->{filesystem}\@$self->{snapshot}': unknown reason", $Amanda::Script_App::ERROR);
224 my($ksize) = int ($size/1024);
225 $ksize=32 if ($ksize<32);
227 print $mesgout_fd "sendbackup: size $ksize\n";
228 print $mesgout_fd "sendbackup: end\n";
230 # destroy all snapshot of this level and higher
231 $self->zfs_purge_snapshot($level, 9);
233 if ($self->{keep_snapshot} eq 'YES') {
234 $self->zfs_rename_snapshot($level);
236 $self->zfs_destroy_snapshot();
242 sub estimate_snapshot
247 debug "\$filesystem = $self->{filesystem}";
248 debug "\$snapshot = $self->{snapshot}";
249 debug "\$level = $level";
253 $cmd = "$self->{pfexec_cmd} $self->{zfs_path} get -Hp -o value referenced $self->{filesystem}\@$self->{snapshot}";
255 my $refsnapshotname = $self->zfs_find_snapshot_level($level-1);
256 debug "Referenced snapshot name: $refsnapshotname|";
257 if ($refsnapshotname ne "") {
258 $cmd = "$self->{pfexec_cmd} $self->{zfs_path} send -i $refsnapshotname $self->{filesystem}\@$self->{snapshot} | /usr/bin/wc -c";
263 debug "running (estimate): $cmd";
264 my($wtr, $rdr, $err, $pid);
265 $err = Symbol::gensym;
266 $pid = open3($wtr, $rdr, $err, $cmd);
269 my ($errmsg) = <$err>;
274 if (defined $msg && defined $errmsg) {
275 $self->print_to_server_and_die("$msg, $errmsg", $Amanda::Script_App::ERROR);
276 } elsif (defined $msg) {
277 $self->print_to_server_and_die($msg, $Amanda::Script_App::ERROR);
278 } elsif (defined $errmsg) {
279 $self->print_to_server_and_die($errmsg, $Amanda::Script_App::ERROR);
281 $self->print_to_server_and_die("cannot estimate snapshot '$self->{snapshot}\@$self->{snapshot}': unknown reason", $Amanda::Script_App::ERROR);
285 my $compratio = $self->get_compratio();
298 $cmd = "$self->{pfexec_cmd} $self->{zfs_path} get -Hp -o value compressratio $self->{filesystem}\@$self->{snapshot}";
299 debug "running (get-compression): $cmd";
300 my($wtr, $rdr, $err, $pid);
301 $err = Symbol::gensym;
302 $pid = open3($wtr, $rdr, $err, $cmd);
305 my ($errmsg) = <$err>;
310 if (defined $msg && defined $errmsg) {
311 $self->print_to_server_and_die("$msg, $errmsg", $Amanda::Script_App::ERROR);
312 } elsif (defined $msg) {
313 $self->print_to_server_and_die($msg, $Amanda::Script_App::ERROR);
314 } elsif (defined $errmsg) {
315 $self->print_to_server_and_die($errmsg, $Amanda::Script_App::ERROR);
317 $self->print_to_server_and_die("cannot read compression ratio '$self->{snapshot}\@$self->{snapshot}': unknown reason", $Amanda::Script_App::ERROR);
323 sub command_index_from_output {
326 sub command_index_from_image {
329 #sub command_restore {
335 sub command_print_command {
342 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>.
357 my $zfs_path = 'zfs';
358 my $pfexec_path = 'pfexec';
360 my $opt_keep_snapshot = "YES";
361 my @opt_exclude_list;
362 my @opt_include_list;
365 Getopt::Long::Configure(qw{bundling});
367 'config=s' => \$opt_config,
368 'host=s' => \$opt_host,
369 'disk=s' => \$opt_disk,
370 'device=s' => \$opt_device,
371 'level=s' => \@opt_level,
372 'index=s' => \$opt_index,
373 'message=s' => \$opt_message,
374 'collection=s' => \$opt_collection,
375 'record' => \$opt_record,
376 'df-path=s' => \$df_path,
377 'zfs-path=s' => \$zfs_path,
378 'pfexec-path=s' => \$pfexec_path,
379 'pfexec=s' => \$pfexec,
380 'keep-snapshot=s' => \$opt_keep_snapshot,
381 'exclude-list=s' => \@opt_exclude_list,
382 'include-list=s' => \@opt_include_list,
383 'directory=s' => \$opt_directory,
386 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);
388 $application->do($ARGV[0]);