| 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::Amrandom; |
| 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, $size, $size_level_1, $min_size, $max_size, $block_size, $min_block_size, $max_block_size) = @_; |
| 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 |
$self->{size} = $size; |
| 64 |
$self->{size_level_1} = $size_level_1; |
| 65 |
$self->{min_size} = $min_size || 1; |
| 66 |
$self->{max_size} = $max_size || 131072; |
| 67 |
$self->{block_size} = $block_size; |
| 68 |
$self->{min_block_size} = $min_block_size || 1; |
| 69 |
$self->{max_block_size} = $max_block_size || 32768; |
| 70 |
|
| 71 |
if (!defined $self->{size}) { |
| 72 |
$self->{'size'} = $self->{min_size} + int(rand($self->{max_size}- $self->{min_size})); |
| 73 |
} |
| 74 |
if (!defined $self->{size_level_1}) { |
| 75 |
$self->{'size_level_1'} = $self->{min_size} + int(rand($self->{max_size}- $self->{min_size})); |
| 76 |
} |
| 77 |
debug("size: $self->{size}"); |
| 78 |
return $self; |
| 79 |
} |
| 80 |
|
| 81 |
sub command_support { |
| 82 |
my $self = shift; |
| 83 |
|
| 84 |
print "CONFIG YES\n"; |
| 85 |
print "HOST YES\n"; |
| 86 |
print "DISK YES\n"; |
| 87 |
print "MAX-LEVEL 1\n"; |
| 88 |
print "INDEX-LINE YES\n"; |
| 89 |
print "INDEX-XML NO\n"; |
| 90 |
print "MESSAGE-LINE YES\n"; |
| 91 |
print "MESSAGE-XML NO\n"; |
| 92 |
print "RECORD YES\n"; |
| 93 |
print "COLLECTION NO\n"; |
| 94 |
print "MULTI-ESTIMATE NO\n"; |
| 95 |
print "CALCSIZE NO\n"; |
| 96 |
print "CLIENT-ESTIMATE YES\n"; |
| 97 |
} |
| 98 |
|
| 99 |
sub command_selfcheck { |
| 100 |
my $self = shift; |
| 101 |
|
| 102 |
$self->print_to_server("disk " . quote_string($self->{disk}), |
| 103 |
$Amanda::Script_App::GOOD) |
| 104 |
if defined $self->{disk}; |
| 105 |
|
| 106 |
$self->print_to_server("amrandom version " . $Amanda::Constants::VERSION, |
| 107 |
$Amanda::Script_App::GOOD); |
| 108 |
|
| 109 |
$self->print_to_server(quote_string($self->{device}), |
| 110 |
$Amanda::Script_App::GOOD) |
| 111 |
if defined $self->{device}; |
| 112 |
|
| 113 |
# if (! -r $self->{device}) { |
| 114 |
# $self->print_to_server("$self->{device} can't be read", |
| 115 |
# $Amanda::Script_App::ERROR); |
| 116 |
# } |
| 117 |
|
| 118 |
if ($#{$self->{include_list}} >= 0) { |
| 119 |
$self->print_to_server("include-list not supported for backup", |
| 120 |
$Amanda::Script_App::ERROR); |
| 121 |
} |
| 122 |
if ($#{$self->{exclude_list}} >= 0) { |
| 123 |
$self->print_to_server("exclude-list not supported for backup", |
| 124 |
$Amanda::Script_App::ERROR); |
| 125 |
} |
| 126 |
if ($self->{target}) { |
| 127 |
$self->print_to_server("target PROPERTY not supported for backup", |
| 128 |
$Amanda::Script_App::ERROR); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
sub command_estimate { |
| 133 |
my $self = shift; |
| 134 |
|
| 135 |
my $level = $self->{level}[0]; |
| 136 |
|
| 137 |
if ($level != 0 && $level != 1) { |
| 138 |
$self->print_to_server("amrandom can only do level 0 and 1 backup", |
| 139 |
$Amanda::Script_App::ERROR); |
| 140 |
} |
| 141 |
|
| 142 |
if ($#{$self->{include_list}} >= 0) { |
| 143 |
$self->print_to_server("include-list not supported for backup", |
| 144 |
$Amanda::Script_App::ERROR); |
| 145 |
} |
| 146 |
if ($#{$self->{include_list}} >= 0) { |
| 147 |
$self->print_to_server("exclude-list not supported for backup", |
| 148 |
$Amanda::Script_App::ERROR); |
| 149 |
} |
| 150 |
if ($self->{target}) { |
| 151 |
$self->print_to_server("target PROPERTY not supported for backup", |
| 152 |
$Amanda::Script_App::ERROR); |
| 153 |
} |
| 154 |
|
| 155 |
if ($level == 0) { |
| 156 |
output_size($level, $self->{size}); |
| 157 |
} else { |
| 158 |
output_size($level, $self->{size_level_1}); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
sub output_size { |
| 163 |
my($level) = shift; |
| 164 |
my($size) = shift; |
| 165 |
if($size == -1) { |
| 166 |
print "$level -1 -1\n"; |
| 167 |
} |
| 168 |
else { |
| 169 |
my($ksize) = int $size / (1024); |
| 170 |
$ksize=32 if ($ksize<32); |
| 171 |
print "$level $ksize 1\n"; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
sub command_backup { |
| 176 |
my $self = shift; |
| 177 |
|
| 178 |
my $level = $self->{level}[0]; |
| 179 |
|
| 180 |
if (defined($self->{index})) { |
| 181 |
$self->{'index_out'} = IO::Handle->new_from_fd(4, 'w'); |
| 182 |
$self->{'index_out'} or confess("Could not open index fd"); |
| 183 |
} |
| 184 |
|
| 185 |
if ($level != 0 && $level != 1) { |
| 186 |
$self->print_to_server("amrandom can only do level 0 and 1 backup", |
| 187 |
$Amanda::Script_App::ERROR); |
| 188 |
} |
| 189 |
|
| 190 |
if ($#{$self->{include_list}} >= 0) { |
| 191 |
$self->print_to_server("include-list not supported for backup", |
| 192 |
$Amanda::Script_App::ERROR); |
| 193 |
} |
| 194 |
if ($#{$self->{include_list}} >= 0) { |
| 195 |
$self->print_to_server("exclude-list not supported for backup", |
| 196 |
$Amanda::Script_App::ERROR); |
| 197 |
} |
| 198 |
if ($self->{target}) { |
| 199 |
$self->print_to_server("target PROPERTY not supported for backup", |
| 200 |
$Amanda::Script_App::ERROR); |
| 201 |
} |
| 202 |
|
| 203 |
my $buffer="A"; |
| 204 |
my $buf_size = 1024*1024; |
| 205 |
if (defined $self->{blocksize} && $self->{blocksize} < $buf_size) { |
| 206 |
$buf_size = $self->{blocksize}; |
| 207 |
} |
| 208 |
for (my $i=0; $i<$buf_size; $i++) { |
| 209 |
$buffer .= chr(int(rand(256))); |
| 210 |
} |
| 211 |
|
| 212 |
my $size; |
| 213 |
if ($level == 0) { |
| 214 |
$size = $self->{size}; |
| 215 |
} else { |
| 216 |
$size = $self->{size_level_1}; |
| 217 |
} |
| 218 |
|
| 219 |
my $out = fileno(STDOUT); |
| 220 |
while ($size > 0) { |
| 221 |
my $block_size = $self->{block_size} || |
| 222 |
$self->{min_block_size} + int(rand($self->{max_block_size} - $self->{min_block_size})); |
| 223 |
|
| 224 |
if ($block_size > $size) { |
| 225 |
$block_size = $size; |
| 226 |
} |
| 227 |
debug("writing $block_size bytes"); |
| 228 |
my $n = POSIX::write($out, $buffer, $block_size); |
| 229 |
if (!defined $n) { |
| 230 |
debug("Bad write: $!"); |
| 231 |
} elsif ($n ne $block_size) { |
| 232 |
debug("Bad write $n != $block_size"); |
| 233 |
} |
| 234 |
$size -= $block_size |
| 235 |
} |
| 236 |
POSIX::close($out); |
| 237 |
if (defined($self->{index})) { |
| 238 |
$self->{'index_out'}->print("/\n"); |
| 239 |
$self->{'index_out'}->close; |
| 240 |
} |
| 241 |
|
| 242 |
if ($level == 0) { |
| 243 |
$size = $self->{size}; |
| 244 |
} else { |
| 245 |
$size = $self->{size_level_1}; |
| 246 |
} |
| 247 |
|
| 248 |
if ($size >= 0) { |
| 249 |
my $ksize = $size / 1024; |
| 250 |
if ($ksize < 1) { |
| 251 |
$ksize = 1; |
| 252 |
} |
| 253 |
print {$self->{mesgout}} "sendbackup: size $ksize\n"; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
sub command_restore { |
| 258 |
my $self = shift; |
| 259 |
my @cmd = (); |
| 260 |
|
| 261 |
my $device = $self->{device}; |
| 262 |
if (defined $self->{target}) { |
| 263 |
$device = $self->{target}; |
| 264 |
} else { |
| 265 |
chdir(Amanda::Util::get_original_cwd()); |
| 266 |
} |
| 267 |
|
| 268 |
# include-list and exclude-list are ignored, the complete dle is restored. |
| 269 |
|
| 270 |
$device = "amrandom-restored" if !defined $device; |
| 271 |
debug("Restoring to $device"); |
| 272 |
|
| 273 |
my $fd = POSIX::open($device, &POSIX::O_CREAT | &POSIX::O_RDWR, 0600 ); |
| 274 |
if (!defined $fd) { |
| 275 |
$self->print_to_server_and_die("Can't open '$device': $!", |
| 276 |
$Amanda::Script_App::ERROR); |
| 277 |
} |
| 278 |
my $size = 0; |
| 279 |
my $s; |
| 280 |
my $buffer; |
| 281 |
my $in = fileno(STDIN); |
| 282 |
while (($s = POSIX::read($in, $buffer, 32768)) > 0) { |
| 283 |
Amanda::Util::full_write($fd, $buffer, $s); |
| 284 |
$size += $s; |
| 285 |
} |
| 286 |
POSIX::close($fd); |
| 287 |
POSIX::close($in); |
| 288 |
} |
| 289 |
|
| 290 |
sub command_validate { |
| 291 |
my $self = shift; |
| 292 |
|
| 293 |
$self->default_validate(); |
| 294 |
} |
| 295 |
|
| 296 |
sub command_index { |
| 297 |
my $self = shift; |
| 298 |
my $buffer; |
| 299 |
|
| 300 |
print "/\n"; |
| 301 |
|
| 302 |
do { |
| 303 |
sysread STDIN, $buffer, 1048576; |
| 304 |
} while (defined $buffer and length($buffer) > 0); |
| 305 |
} |
| 306 |
|
| 307 |
package main; |
| 308 |
|
| 309 |
sub usage { |
| 310 |
print <<EOF; |
| 311 |
Usage: amrandom <command> --config=<config> --host=<host> --disk=<disk> --device=<device> --level=<level> --index=<yes|no> --message=<text> --collection=<no> --record=<yes|no> --calcsize. |
| 312 |
EOF |
| 313 |
exit(1); |
| 314 |
} |
| 315 |
|
| 316 |
my $opt_version; |
| 317 |
my $opt_config; |
| 318 |
my $opt_host; |
| 319 |
my $opt_disk; |
| 320 |
my $opt_device; |
| 321 |
my @opt_level; |
| 322 |
my $opt_index; |
| 323 |
my $opt_message; |
| 324 |
my $opt_collection; |
| 325 |
my $opt_record; |
| 326 |
my $opt_calcsize; |
| 327 |
my @opt_include_list; |
| 328 |
my @opt_exclude_list; |
| 329 |
my $opt_target; |
| 330 |
my $opt_size; |
| 331 |
my $opt_size_level_1; |
| 332 |
my $opt_min_size; |
| 333 |
my $opt_max_size; |
| 334 |
my $opt_block_size; |
| 335 |
my $opt_min_block_size; |
| 336 |
my $opt_max_block_size; |
| 337 |
|
| 338 |
my @orig_argv = @ARGV; |
| 339 |
|
| 340 |
Getopt::Long::Configure(qw{bundling}); |
| 341 |
GetOptions( |
| 342 |
'version' => \$opt_version, |
| 343 |
'config=s' => \$opt_config, |
| 344 |
'host=s' => \$opt_host, |
| 345 |
'disk=s' => \$opt_disk, |
| 346 |
'device=s' => \$opt_device, |
| 347 |
'level=s' => \@opt_level, |
| 348 |
'index=s' => \$opt_index, |
| 349 |
'message=s' => \$opt_message, |
| 350 |
'collection=s' => \$opt_collection, |
| 351 |
'record' => \$opt_record, |
| 352 |
'calcsize' => \$opt_calcsize, |
| 353 |
'include-list=s' => \@opt_include_list, |
| 354 |
'exclude-list=s' => \@opt_exclude_list, |
| 355 |
'target|directory=s' => \$opt_target, |
| 356 |
'size=s' => \$opt_size, |
| 357 |
'size-level-1=s' => \$opt_size_level_1, |
| 358 |
'min-size=s' => \$opt_min_size, |
| 359 |
'max-size=s' => \$opt_max_size, |
| 360 |
'block-size=s' => \$opt_block_size, |
| 361 |
'min-block-size=s' => \$opt_min_block_size, |
| 362 |
'max-block-size=s' => \$opt_max_block_size, |
| 363 |
) or usage(); |
| 364 |
|
| 365 |
if (defined $opt_version) { |
| 366 |
print "amrandom-" . $Amanda::Constants::VERSION , "\n"; |
| 367 |
exit(0); |
| 368 |
} |
| 369 |
|
| 370 |
my $application = Amanda::Application::Amrandom->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, $opt_size, $opt_size_level_1, $opt_min_size, $opt_max_size, $opt_block_size, $opt_min_block_size, $opt_max_block_size); |
| 371 |
|
| 372 |
Amanda::Debug::debug("Arguments: " . join(' ', @orig_argv)); |
| 373 |
|
| 374 |
$application->do($ARGV[0]); |
| 375 |
# NOTREACHED |