Imported Upstream version 3.3.3
[debian/amanda] / application-src / amraw.pl
1 #!@PERL@ 
2 # Copyright (c) 2009-2012 Zmanda, Inc.  All Rights Reserved.
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 # for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20
21 use lib '@amperldir@';
22 use strict;
23 use warnings;
24 use Getopt::Long;
25
26 package Amanda::Application::Amraw;
27 use base qw(Amanda::Application);
28 use IPC::Open3;
29 use Sys::Hostname;
30 use Symbol;
31 use IO::Handle;
32 use Amanda::Constants;
33 use Amanda::Debug qw( :logging );
34 use Amanda::Util qw( quote_string );
35
36 sub new {
37     my $class = shift;
38     my ($config, $host, $disk, $device, $level, $index, $message, $collection, $record, $calcsize, $include_list, $exclude_list, $directory) = @_;
39     my $self = $class->SUPER::new($config);
40
41     $self->{config}           = $config;
42     $self->{host}             = $host;
43     if (defined $disk) {
44         $self->{disk}         = $disk;
45     } else {
46         $self->{disk}         = $device;
47     }
48     if (defined $device) {
49         $self->{device}       = $device;
50     } else {
51         $self->{device}       = $disk;
52     }
53     $self->{level}            = [ @{$level} ];
54     $self->{index}            = $index;
55     $self->{message}          = $message;
56     $self->{collection}       = $collection;
57     $self->{record}           = $record;
58     $self->{calcsize}         = $calcsize;
59     $self->{exclude_list}     = [ @{$exclude_list} ];
60     $self->{include_list}     = [ @{$include_list} ];
61     $self->{directory}        = $directory;
62
63     return $self;
64 }
65
66 sub command_support {
67     my $self = shift;
68
69     print "CONFIG YES\n";
70     print "HOST YES\n";
71     print "DISK YES\n";
72     print "MAX-LEVEL 0\n";
73     print "INDEX-LINE YES\n";
74     print "INDEX-XML NO\n";
75     print "MESSAGE-LINE YES\n";
76     print "MESSAGE-XML NO\n";
77     print "RECORD YES\n";
78     print "COLLECTION NO\n";
79     print "MULTI-ESTIMATE NO\n";
80     print "CALCSIZE NO\n";
81     print "CLIENT-ESTIMATE YES\n";
82 }
83
84 sub command_selfcheck {
85     my $self = shift;
86
87     $self->print_to_server("disk " . quote_string($self->{disk}),
88                            $Amanda::Script_App::GOOD)
89                 if defined $self->{disk};
90
91     $self->print_to_server("amraw version " . $Amanda::Constants::VERSION,
92                            $Amanda::Script_App::GOOD);
93
94     $self->print_to_server(quote_string($self->{device}),
95                            $Amanda::Script_App::GOOD)
96                 if defined $self->{device};
97
98     if (! -r $self->{device}) {
99         $self->print_to_server("$self->{device} can't be read",
100                                $Amanda::Script_App::ERROR);
101     }
102
103     if ($#{$self->{include_list}} >= 0) {
104         $self->print_to_server("include-list not supported for backup",
105                                $Amanda::Script_App::ERROR);
106     }
107     if ($#{$self->{exclude_list}} >= 0) {
108         $self->print_to_server("exclude-list not supported for backup",
109                                $Amanda::Script_App::ERROR);
110     }
111     if ($self->{directory}) {
112         $self->print_to_server("directory PROPERTY not supported for backup",
113                                $Amanda::Script_App::ERROR);
114     }
115
116     #check statefile
117     #check amdevice
118 }
119
120 sub command_estimate {
121     my $self = shift;
122
123     my $level = $self->{level}[0];
124
125     if ($level != 0) {
126         $self->print_to_server("amraw can only do level 0 backup",
127                                $Amanda::Script_App::ERROR);
128     }
129
130     if ($#{$self->{include_list}} >= 0) {
131         $self->print_to_server("include-list not supported for backup",
132                                $Amanda::Script_App::ERROR);
133     }
134     if ($#{$self->{include_list}} >= 0) {
135         $self->print_to_server("exclude-list not supported for backup",
136                                $Amanda::Script_App::ERROR);
137     }
138     if ($self->{directory}) {
139         $self->print_to_server("directory PROPERTY not supported for backup",
140                                $Amanda::Script_App::ERROR);
141     }
142
143     my $fd = POSIX::open($self->{device}, &POSIX::O_RDONLY);
144     my $size = 0;
145     my $s;
146     my $buffer;
147     while (($s = POSIX::read($fd, $buffer, 32768)) > 0) {
148         $size += $s;
149     }
150     POSIX::close($fd);
151     output_size($level, $size);
152 }
153
154 sub output_size {
155    my($level) = shift;
156    my($size) = shift;
157    if($size == -1) {
158       print "$level -1 -1\n";
159    }
160    else {
161       my($ksize) = int $size / (1024);
162       $ksize=32 if ($ksize<32);
163       print "$level $ksize 1\n";
164    }
165 }
166
167 sub command_backup {
168     my $self = shift;
169
170     my $level = $self->{level}[0];
171
172     if (defined($self->{index})) {
173         $self->{'index_out'} = IO::Handle->new_from_fd(4, 'w');
174         $self->{'index_out'} or confess("Could not open index fd");
175     }
176
177     if ($level != 0) {
178         $self->print_to_server("amraw can only do level 0 backup",
179                                $Amanda::Script_App::ERROR);
180     }
181
182     if ($#{$self->{include_list}} >= 0) {
183         $self->print_to_server("include-list not supported for backup",
184                                $Amanda::Script_App::ERROR);
185     }
186     if ($#{$self->{include_list}} >= 0) {
187         $self->print_to_server("exclude-list not supported for backup",
188                                $Amanda::Script_App::ERROR);
189     }
190     if ($self->{directory}) {
191         $self->print_to_server("directory PROPERTY not supported for backup",
192                                $Amanda::Script_App::ERROR);
193     }
194
195     my $fd = POSIX::open($self->{device}, &POSIX::O_RDONLY);
196     my $size = 0;
197     my $s;
198     my $buffer;
199     my $out = 1;
200     while (($s = POSIX::read($fd, $buffer, 32768)) > 0) {
201         Amanda::Util::full_write($out, $buffer, $s);
202         $size += $s;
203     }
204     POSIX::close($fd);
205     close($out);
206     if (defined($self->{index})) {
207         $self->{'index_out'}->print("/\n");
208         $self->{'index_out'}->close;
209     }
210     if ($size >= 0) {
211         my $ksize = $size / 1024;
212         if ($ksize < 32) {
213             $ksize = 32;
214         }
215         print {$self->{mesgout}} "sendbackup: size $ksize\n";
216         print {$self->{mesgout}} "sendbackup: end\n";
217     }
218
219     exit 0;
220 }
221
222 sub command_restore {
223     my $self = shift;
224     my @cmd = ();
225
226     my $device = $self->{device};
227     if (defined $self->{directory}) {
228         $device = $self->{directory};
229     } else {
230         chdir(Amanda::Util::get_original_cwd());
231     }
232
233     # include-list and exclude-list are ignored, the complete dle is restored.
234
235     $device = "amraw-restored" if !defined $device;
236
237     my $fd = POSIX::open($device, &POSIX::O_CREAT | &POSIX::O_RDWR, 0600 );
238     if ($fd == -1) {
239         $self->print_to_server_and_die("Can't open '$device': $!",
240                                        $Amanda::Script_App::ERROR);
241     }
242     my $size = 0;
243     my $s;
244     my $buffer;
245     my $in = 0;
246     while (($s = POSIX::read($in, $buffer, 32768)) > 0) {
247         Amanda::Util::full_write($fd, $buffer, $s);
248         $size += $s;
249     }
250     POSIX::close($fd);
251     close($in);
252     exit(0);
253 }
254
255 sub command_validate {
256     my $self = shift;
257
258     $self->default_validate();
259 }
260
261 package main;
262
263 sub usage {
264     print <<EOF;
265 Usage: amraw <command> --config=<config> --host=<host> --disk=<disk> --device=<device> --level=<level> --index=<yes|no> --message=<text> --collection=<no> --record=<yes|no> --calcsize.
266 EOF
267     exit(1);
268 }
269
270 my $opt_version;
271 my $opt_config;
272 my $opt_host;
273 my $opt_disk;
274 my $opt_device;
275 my @opt_level;
276 my $opt_index;
277 my $opt_message;
278 my $opt_collection;
279 my $opt_record;
280 my $opt_calcsize;
281 my @opt_include_list;
282 my @opt_exclude_list;
283 my $opt_directory;
284
285 Getopt::Long::Configure(qw{bundling});
286 GetOptions(
287     'version'            => \$opt_version,
288     'config=s'           => \$opt_config,
289     'host=s'             => \$opt_host,
290     'disk=s'             => \$opt_disk,
291     'device=s'           => \$opt_device,
292     'level=s'            => \@opt_level,
293     'index=s'            => \$opt_index,
294     'message=s'          => \$opt_message,
295     'collection=s'       => \$opt_collection,
296     'record'             => \$opt_record,
297     'calcsize'           => \$opt_calcsize,
298     'include-list=s'     => \@opt_include_list,
299     'exclude-list=s'     => \@opt_exclude_list,
300     'directory'          => \$opt_directory,
301 ) or usage();
302
303 if (defined $opt_version) {
304     print "amraw-" . $Amanda::Constants::VERSION , "\n";
305     exit(0);
306 }
307
308 my $application = Amanda::Application::Amraw->new($opt_config, $opt_host, $opt_disk, $opt_device, \@opt_level, $opt_index, $opt_message, $opt_collection, $opt_record, $opt_calcsize, \@opt_include_list, \@opt_exclude_list, $opt_directory);
309
310 $application->do($ARGV[0]);