Debian Codegraph @

application-src/amraw.pl

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