Imported Upstream version 3.2.0
[debian/amanda] / application-src / amraw.pl
1 #!@PERL@ 
2 # Copyright (c) 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::Amraw;
26 use base qw(Amanda::Application);
27 use IPC::Open3;
28 use Sys::Hostname;
29 use Symbol;
30 use IO::Handle;
31 use Amanda::Constants;
32 use Amanda::Debug qw( :logging );
33 use Amanda::Util;
34
35 sub new {
36     my $class = shift;
37     my ($config, $host, $disk, $device, $level, $index, $message, $collection, $record, $calcsize, $include_list, $exclude_list, $directory) = @_;
38     my $self = $class->SUPER::new($config);
39
40     $self->{config}           = $config;
41     $self->{host}             = $host;
42     if (defined $disk) {
43         $self->{disk}         = $disk;
44     } else {
45         $self->{disk}         = $device;
46     }
47     if (defined $device) {
48         $self->{device}       = $device;
49     } else {
50         $self->{device}       = $disk;
51     }
52     $self->{level}            = [ @{$level} ];
53     $self->{index}            = $index;
54     $self->{message}          = $message;
55     $self->{collection}       = $collection;
56     $self->{record}           = $record;
57     $self->{calcsize}         = $calcsize;
58     $self->{exclude_list}     = [ @{$exclude_list} ];
59     $self->{include_list}     = [ @{$include_list} ];
60     $self->{directory}        = $directory;
61
62     return $self;
63 }
64
65 sub command_support {
66     my $self = shift;
67
68     print "CONFIG YES\n";
69     print "HOST YES\n";
70     print "DISK YES\n";
71     print "MAX-LEVEL 0\n";
72     print "INDEX-LINE YES\n";
73     print "INDEX-XML NO\n";
74     print "MESSAGE-LINE YES\n";
75     print "MESSAGE-XML NO\n";
76     print "RECORD YES\n";
77     print "COLLECTION NO\n";
78     print "MULTI-ESTIMATE NO\n";
79     print "CALCSIZE NO\n";
80     print "CLIENT-ESTIMATE YES\n";
81 }
82
83 sub command_selfcheck {
84     my $self = shift;
85
86     print "OK " . $self->{disk} . "\n";
87     print "OK " . $self->{device} . "\n";
88
89     if (! -r $self->{device}) {
90         $self->print_to_server("$self->{device} can't be read",
91                                $Amanda::Script_App::ERROR);
92     }
93
94     if ($#{$self->{include_list}} >= 0) {
95         $self->print_to_server("include-list not supported for backup",
96                                $Amanda::Script_App::ERROR);
97     }
98     if ($#{$self->{exclude_list}} >= 0) {
99         $self->print_to_server("exclude-list not supported for backup",
100                                $Amanda::Script_App::ERROR);
101     }
102     if ($self->{directory}) {
103         $self->print_to_server("directory PROPERTY not supported for backup",
104                                $Amanda::Script_App::ERROR);
105     }
106
107     #check statefile
108     #check amdevice
109 }
110
111 sub command_estimate {
112     my $self = shift;
113
114     my $level = $self->{level}[0];
115
116     if ($level != 0) {
117         $self->print_to_server("amraw can only do level 0 backup",
118                                $Amanda::Script_App::ERROR);
119     }
120
121     if ($#{$self->{include_list}} >= 0) {
122         $self->print_to_server("include-list not supported for backup",
123                                $Amanda::Script_App::ERROR);
124     }
125     if ($#{$self->{include_list}} >= 0) {
126         $self->print_to_server("exclude-list not supported for backup",
127                                $Amanda::Script_App::ERROR);
128     }
129     if ($self->{directory}) {
130         $self->print_to_server("directory PROPERTY not supported for backup",
131                                $Amanda::Script_App::ERROR);
132     }
133
134     my $fd = POSIX::open($self->{device}, &POSIX::O_RDONLY);
135     my $size = 0;
136     my $s;
137     my $buffer;
138     while (($s = POSIX::read($fd, $buffer, 32768)) > 0) {
139         $size += $s;
140     }
141     POSIX::close($fd);
142     output_size($level, $size);
143 }
144
145 sub output_size {
146    my($level) = shift;
147    my($size) = shift;
148    if($size == -1) {
149       print "$level -1 -1\n";
150    }
151    else {
152       my($ksize) = int $size / (1024);
153       $ksize=32 if ($ksize<32);
154       print "$level $ksize 1\n";
155    }
156 }
157
158 sub command_backup {
159     my $self = shift;
160
161     my $level = $self->{level}[0];
162
163     if (defined($self->{index})) {
164         $self->{'index_out'} = IO::Handle->new_from_fd(4, 'w');
165         $self->{'index_out'} or confess("Could not open index fd");
166     }
167
168     if ($level != 0) {
169         $self->print_to_server("amraw can only do level 0 backup",
170                                $Amanda::Script_App::ERROR);
171     }
172
173     if ($#{$self->{include_list}} >= 0) {
174         $self->print_to_server("include-list not supported for backup",
175                                $Amanda::Script_App::ERROR);
176     }
177     if ($#{$self->{include_list}} >= 0) {
178         $self->print_to_server("exclude-list not supported for backup",
179                                $Amanda::Script_App::ERROR);
180     }
181     if ($self->{directory}) {
182         $self->print_to_server("directory PROPERTY not supported for backup",
183                                $Amanda::Script_App::ERROR);
184     }
185
186     my $fd = POSIX::open($self->{device}, &POSIX::O_RDONLY);
187     my $size = 0;
188     my $s;
189     my $buffer;
190     my $out = 1;
191     while (($s = POSIX::read($fd, $buffer, 32768)) > 0) {
192         Amanda::Util::full_write($out, $buffer, $s);
193         $size += $s;
194     }
195     POSIX::close($fd);
196     close($out);
197     if (defined($self->{index})) {
198         $self->{'index_out'}->print("/\n");
199         $self->{'index_out'}->close;
200     }
201     if ($size >= 0) {
202         my $ksize = $size / 1024;
203         if ($ksize < 32) {
204             $ksize = 32;
205         }
206         print {$self->{mesgout}} "sendbackup: size $ksize\n";
207         print {$self->{mesgout}} "sendbackup: end\n";
208     }
209
210     exit 0;
211 }
212
213 sub command_restore {
214     my $self = shift;
215     my @cmd = ();
216
217     my $device = $self->{device};
218     if (defined $self->{directory}) {
219         $device = $self->{directory};
220     } else {
221         chdir(Amanda::Util::get_original_cwd());
222     }
223
224     # include-list and exclude-list are ignored, the complete dle is restored.
225
226     $device = "amraw-restored" if !defined $device;
227
228     my $fd = POSIX::open($device, &POSIX::O_CREAT | &POSIX::O_RDWR, 0600 );
229     if ($fd == -1) {
230         $self->print_to_server_and_die("Can't open '$device': $!",
231                                        $Amanda::Script_App::ERROR);
232     }
233     my $size = 0;
234     my $s;
235     my $buffer;
236     my $in = 0;
237     while (($s = POSIX::read($in, $buffer, 32768)) > 0) {
238         Amanda::Util::full_write($fd, $buffer, $s);
239         $size += $s;
240     }
241     POSIX::close($fd);
242     close($in);
243     exit(0);
244 }
245
246 sub command_validate {
247     my $self = shift;
248
249     $self->default_validate();
250 }
251
252 package main;
253
254 sub usage {
255     print <<EOF;
256 Usage: amraw <command> --config=<config> --host=<host> --disk=<disk> --device=<device> --level=<level> --index=<yes|no> --message=<text> --collection=<no> --record=<yes|no> --calcsize.
257 EOF
258     exit(1);
259 }
260
261 my $opt_version;
262 my $opt_config;
263 my $opt_host;
264 my $opt_disk;
265 my $opt_device;
266 my @opt_level;
267 my $opt_index;
268 my $opt_message;
269 my $opt_collection;
270 my $opt_record;
271 my $opt_calcsize;
272 my @opt_include_list;
273 my @opt_exclude_list;
274 my $opt_directory;
275
276 Getopt::Long::Configure(qw{bundling});
277 GetOptions(
278     'version'            => \$opt_version,
279     'config=s'           => \$opt_config,
280     'host=s'             => \$opt_host,
281     'disk=s'             => \$opt_disk,
282     'device=s'           => \$opt_device,
283     'level=s'            => \@opt_level,
284     'index=s'            => \$opt_index,
285     'message=s'          => \$opt_message,
286     'collection=s'       => \$opt_collection,
287     'record'             => \$opt_record,
288     'calcsize'           => \$opt_calcsize,
289     'include-list=s'     => \@opt_include_list,
290     'exclude-list=s'     => \@opt_exclude_list,
291     'directory'          => \$opt_directory,
292 ) or usage();
293
294 if (defined $opt_version) {
295     print "amraw-" . $Amanda::Constants::VERSION , "\n";
296     exit(0);
297 }
298
299 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);
300
301 $application->do($ARGV[0]);