5502657dc5325f19f525a0853c9d2f87c9158dda
[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 qw( quote_string );
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     $self->print_to_server("disk " . quote_string($self->{disk}),
87                            $Amanda::Script_App::GOOD)
88                 if defined $self->{disk};
89
90     $self->print_to_server("amraw version " . $Amanda::Constants::VERSION,
91                            $Amanda::Script_App::GOOD);
92
93     $self->print_to_server(quote_string($self->{device}),
94                            $Amanda::Script_App::GOOD)
95                 if defined $self->{device};
96
97     if (! -r $self->{device}) {
98         $self->print_to_server("$self->{device} can't be read",
99                                $Amanda::Script_App::ERROR);
100     }
101
102     if ($#{$self->{include_list}} >= 0) {
103         $self->print_to_server("include-list not supported for backup",
104                                $Amanda::Script_App::ERROR);
105     }
106     if ($#{$self->{exclude_list}} >= 0) {
107         $self->print_to_server("exclude-list not supported for backup",
108                                $Amanda::Script_App::ERROR);
109     }
110     if ($self->{directory}) {
111         $self->print_to_server("directory PROPERTY not supported for backup",
112                                $Amanda::Script_App::ERROR);
113     }
114
115     #check statefile
116     #check amdevice
117 }
118
119 sub command_estimate {
120     my $self = shift;
121
122     my $level = $self->{level}[0];
123
124     if ($level != 0) {
125         $self->print_to_server("amraw can only do level 0 backup",
126                                $Amanda::Script_App::ERROR);
127     }
128
129     if ($#{$self->{include_list}} >= 0) {
130         $self->print_to_server("include-list not supported for backup",
131                                $Amanda::Script_App::ERROR);
132     }
133     if ($#{$self->{include_list}} >= 0) {
134         $self->print_to_server("exclude-list not supported for backup",
135                                $Amanda::Script_App::ERROR);
136     }
137     if ($self->{directory}) {
138         $self->print_to_server("directory PROPERTY not supported for backup",
139                                $Amanda::Script_App::ERROR);
140     }
141
142     my $fd = POSIX::open($self->{device}, &POSIX::O_RDONLY);
143     my $size = 0;
144     my $s;
145     my $buffer;
146     while (($s = POSIX::read($fd, $buffer, 32768)) > 0) {
147         $size += $s;
148     }
149     POSIX::close($fd);
150     output_size($level, $size);
151 }
152
153 sub output_size {
154    my($level) = shift;
155    my($size) = shift;
156    if($size == -1) {
157       print "$level -1 -1\n";
158    }
159    else {
160       my($ksize) = int $size / (1024);
161       $ksize=32 if ($ksize<32);
162       print "$level $ksize 1\n";
163    }
164 }
165
166 sub command_backup {
167     my $self = shift;
168
169     my $level = $self->{level}[0];
170
171     if (defined($self->{index})) {
172         $self->{'index_out'} = IO::Handle->new_from_fd(4, 'w');
173         $self->{'index_out'} or confess("Could not open index fd");
174     }
175
176     if ($level != 0) {
177         $self->print_to_server("amraw can only do level 0 backup",
178                                $Amanda::Script_App::ERROR);
179     }
180
181     if ($#{$self->{include_list}} >= 0) {
182         $self->print_to_server("include-list not supported for backup",
183                                $Amanda::Script_App::ERROR);
184     }
185     if ($#{$self->{include_list}} >= 0) {
186         $self->print_to_server("exclude-list not supported for backup",
187                                $Amanda::Script_App::ERROR);
188     }
189     if ($self->{directory}) {
190         $self->print_to_server("directory PROPERTY not supported for backup",
191                                $Amanda::Script_App::ERROR);
192     }
193
194     my $fd = POSIX::open($self->{device}, &POSIX::O_RDONLY);
195     my $size = 0;
196     my $s;
197     my $buffer;
198     my $out = 1;
199     while (($s = POSIX::read($fd, $buffer, 32768)) > 0) {
200         Amanda::Util::full_write($out, $buffer, $s);
201         $size += $s;
202     }
203     POSIX::close($fd);
204     close($out);
205     if (defined($self->{index})) {
206         $self->{'index_out'}->print("/\n");
207         $self->{'index_out'}->close;
208     }
209     if ($size >= 0) {
210         my $ksize = $size / 1024;
211         if ($ksize < 32) {
212             $ksize = 32;
213         }
214         print {$self->{mesgout}} "sendbackup: size $ksize\n";
215         print {$self->{mesgout}} "sendbackup: end\n";
216     }
217
218     exit 0;
219 }
220
221 sub command_restore {
222     my $self = shift;
223     my @cmd = ();
224
225     my $device = $self->{device};
226     if (defined $self->{directory}) {
227         $device = $self->{directory};
228     } else {
229         chdir(Amanda::Util::get_original_cwd());
230     }
231
232     # include-list and exclude-list are ignored, the complete dle is restored.
233
234     $device = "amraw-restored" if !defined $device;
235
236     my $fd = POSIX::open($device, &POSIX::O_CREAT | &POSIX::O_RDWR, 0600 );
237     if ($fd == -1) {
238         $self->print_to_server_and_die("Can't open '$device': $!",
239                                        $Amanda::Script_App::ERROR);
240     }
241     my $size = 0;
242     my $s;
243     my $buffer;
244     my $in = 0;
245     while (($s = POSIX::read($in, $buffer, 32768)) > 0) {
246         Amanda::Util::full_write($fd, $buffer, $s);
247         $size += $s;
248     }
249     POSIX::close($fd);
250     close($in);
251     exit(0);
252 }
253
254 sub command_validate {
255     my $self = shift;
256
257     $self->default_validate();
258 }
259
260 package main;
261
262 sub usage {
263     print <<EOF;
264 Usage: amraw <command> --config=<config> --host=<host> --disk=<disk> --device=<device> --level=<level> --index=<yes|no> --message=<text> --collection=<no> --record=<yes|no> --calcsize.
265 EOF
266     exit(1);
267 }
268
269 my $opt_version;
270 my $opt_config;
271 my $opt_host;
272 my $opt_disk;
273 my $opt_device;
274 my @opt_level;
275 my $opt_index;
276 my $opt_message;
277 my $opt_collection;
278 my $opt_record;
279 my $opt_calcsize;
280 my @opt_include_list;
281 my @opt_exclude_list;
282 my $opt_directory;
283
284 Getopt::Long::Configure(qw{bundling});
285 GetOptions(
286     'version'            => \$opt_version,
287     'config=s'           => \$opt_config,
288     'host=s'             => \$opt_host,
289     'disk=s'             => \$opt_disk,
290     'device=s'           => \$opt_device,
291     'level=s'            => \@opt_level,
292     'index=s'            => \$opt_index,
293     'message=s'          => \$opt_message,
294     'collection=s'       => \$opt_collection,
295     'record'             => \$opt_record,
296     'calcsize'           => \$opt_calcsize,
297     'include-list=s'     => \@opt_include_list,
298     'exclude-list=s'     => \@opt_exclude_list,
299     'directory'          => \$opt_directory,
300 ) or usage();
301
302 if (defined $opt_version) {
303     print "amraw-" . $Amanda::Constants::VERSION , "\n";
304     exit(0);
305 }
306
307 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);
308
309 $application->do($ARGV[0]);