| 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::ampgsql; |
| 28 |
use base qw(Amanda::Application); |
| 29 |
use Carp; |
| 30 |
use File::Copy; |
| 31 |
use File::Path; |
| 32 |
use IO::Dir; |
| 33 |
use IO::File; |
| 34 |
use IPC::Open3; |
| 35 |
use POSIX; |
| 36 |
use POSIX qw( ceil ); |
| 37 |
use Sys::Hostname; |
| 38 |
use Symbol; |
| 39 |
use Amanda::Constants; |
| 40 |
use Amanda::Config qw( :init :getconf config_dir_relative string_to_boolean ); |
| 41 |
use Amanda::Debug qw( :logging ); |
| 42 |
use Amanda::Paths; |
| 43 |
use Amanda::Util qw( :constants :encoding quote_string ); |
| 44 |
use Amanda::MainLoop qw( :GIOCondition ); |
| 45 |
|
| 46 |
my $_DATA_DIR_TAR = "data_dir.tar"; |
| 47 |
my $_ARCHIVE_DIR_TAR = "archive_dir.tar"; |
| 48 |
my $_WAL_FILE_PAT = qr/\w{24}/; |
| 49 |
|
| 50 |
my $_DATA_DIR_RESTORE = "data"; |
| 51 |
my $_ARCHIVE_DIR_RESTORE = "archive"; |
| 52 |
|
| 53 |
sub new { |
| 54 |
my $class = shift @_; |
| 55 |
my $args = shift @_; |
| 56 |
my $self = $class->SUPER::new($args->{'config'}); |
| 57 |
$self->{'args'} = $args; |
| 58 |
$self->{'label-prefix'} = 'amanda'; |
| 59 |
$self->{'runtar'} = "$Amanda::Paths::amlibexecdir/runtar"; |
| 60 |
|
| 61 |
# default arguments (application properties) |
| 62 |
$self->{'args'}->{'statedir'} ||= $Amanda::Paths::GNUTAR_LISTED_INCREMENTAL_DIR; |
| 63 |
$self->{'args'}->{'tmpdir'} ||= $AMANDA_TMPDIR; |
| 64 |
# XXX: when using runtar, this is not actually honored. |
| 65 |
# So, this only works for restore at the moment |
| 66 |
$self->{'args'}->{'gnutar-path'} ||= $Amanda::Constants::GNUTAR; |
| 67 |
|
| 68 |
if (!defined $self->{'args'}->{'disk'}) { |
| 69 |
$self->{'args'}->{'disk'} = $self->{'args'}->{'device'}; |
| 70 |
} |
| 71 |
if (!defined $self->{'args'}->{'device'}) { |
| 72 |
$self->{'args'}->{'device'} = $self->{'args'}->{'disk'}; |
| 73 |
} |
| 74 |
# default properties |
| 75 |
$self->{'props'} = { |
| 76 |
'pg-cleanupwal' => 'yes', |
| 77 |
'pg-max-wal-wait' => 60, |
| 78 |
'pg-full-wal' => 'incr', |
| 79 |
'pg-incremental' => 'no', |
| 80 |
'pg-remove-full-wal' => 'yes', |
| 81 |
'pg-remove-incremental-wal' => 'no', |
| 82 |
}; |
| 83 |
|
| 84 |
my @PROP_NAMES = qw(pg-host pg-port pg-db pg-user pg-password pg-passfile |
| 85 |
psql-path pg-datadir pg-archivedir pg-cleanupwal |
| 86 |
pg-max-wal-wait pg-full-wal |
| 87 |
pg-remove-full-wal pg-remove-incremental-wal |
| 88 |
pg-incremental ); |
| 89 |
|
| 90 |
# config is loaded by Amanda::Application (and Amanda::Script_App) |
| 91 |
my $conf_props = getconf($CNF_PROPERTY); |
| 92 |
# check for properties like 'pg-host' |
| 93 |
foreach my $pname (@PROP_NAMES) { |
| 94 |
if ($conf_props->{$pname}) { |
| 95 |
debug("More than one value for $pname. Using the first.") |
| 96 |
if scalar(@{$conf_props->{$pname}->{'values'}}) > 1; |
| 97 |
$self->{'props'}->{$pname} = $conf_props->{$pname}->{'values'}->[0]; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
# check for properties like 'foo-pg-host' where the diskname is 'foo' |
| 102 |
if ($self->{'args'}->{'disk'}) { |
| 103 |
foreach my $pname (@PROP_NAMES) { |
| 104 |
my $tmp = "$self->{'args'}->{'disk'}-$pname"; |
| 105 |
if ($conf_props->{$tmp}) { |
| 106 |
debug("More than one value for $tmp. Using the first.") |
| 107 |
if scalar(@{$conf_props->{$tmp}->{'values'}}) > 1; |
| 108 |
$self->{'props'}->{$pname} = $conf_props->{$tmp}->{'values'}->[0]; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
# overwrite with dumptype properties if they are set. |
| 114 |
foreach my $pname (@PROP_NAMES) { |
| 115 |
my $pdumpname = $pname; |
| 116 |
$pdumpname =~ s/^pg-//g; |
| 117 |
$self->{'props'}->{$pname} = $self->{'args'}->{$pdumpname} |
| 118 |
if defined $self->{'args'}->{$pdumpname}; |
| 119 |
debug("prop $pname set from dumpname $pdumpname: $self->{'args'}->{$pdumpname}") |
| 120 |
if defined $self->{'args'}->{$pdumpname}; |
| 121 |
} |
| 122 |
|
| 123 |
unless ($self->{'props'}->{'psql-path'}) { |
| 124 |
foreach my $pre (split(/:/, $ENV{PATH})) { |
| 125 |
my $psql = "$pre/psql"; |
| 126 |
if (-x $psql) { |
| 127 |
$self->{'props'}{'psql-path'} = $psql; |
| 128 |
last; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
foreach my $aname (keys %{$self->{'args'}}) { |
| 134 |
if (defined($self->{'args'}->{$aname})) { |
| 135 |
debug("app property: $aname $self->{'args'}->{$aname}"); |
| 136 |
} else { |
| 137 |
debug("app property: $aname (undef)"); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
foreach my $pname (keys %{$self->{'props'}}) { |
| 142 |
if (defined($self->{'props'}->{$pname})) { |
| 143 |
debug("client property: $pname $self->{'props'}->{$pname}"); |
| 144 |
} else { |
| 145 |
debug("client property: $pname (undef)"); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
$self->{'pg-version'} = $self->_get_pg_version(); |
| 150 |
|
| 151 |
if (!exists $self->{'props'}->{'pg-datadir'}) { |
| 152 |
$self->{'props'}->{'pg-datadir'} = $self->{'args'}->{'device'}; |
| 153 |
} |
| 154 |
|
| 155 |
$self->{'props'}->{'pg-full-wal'} ||= 'incr'; |
| 156 |
$self->{'props'}->{'pg-full-wal'} = lc($self->{'props'}->{'pg-full-wal'}); |
| 157 |
$self->{'args'}->{'verbose'} ||= "no"; |
| 158 |
$self->{'args'}->{'verbose'} = string_to_boolean($self->{'args'}->{'verbose'}); |
| 159 |
|
| 160 |
return $self; |
| 161 |
} |
| 162 |
|
| 163 |
sub command_support { |
| 164 |
my $self = shift; |
| 165 |
|
| 166 |
print <<EOF; |
| 167 |
CONFIG YES |
| 168 |
HOST YES |
| 169 |
DISK YES |
| 170 |
MAX-LEVEL 9 |
| 171 |
INDEX-LINE YES |
| 172 |
INDEX-XML NO |
| 173 |
MESSAGE-LINE YES |
| 174 |
MESSAGE-XML NO |
| 175 |
RECORD YES |
| 176 |
COLLECTION NO |
| 177 |
CLIENT-ESTIMATE YES |
| 178 |
MULTI-ESTIMATE NO |
| 179 |
CALCSIZE NO |
| 180 |
EXCLUDE-FILE YES |
| 181 |
EXCLUDE-LIST YES |
| 182 |
EOF |
| 183 |
} |
| 184 |
|
| 185 |
sub _check { |
| 186 |
my ($desc, $succ_suf, $err_suf, $check, @check_args) = @_; |
| 187 |
my $ret = $check->(@check_args); |
| 188 |
my $msg = $ret? "OK $desc $succ_suf" : "ERROR $desc $err_suf"; |
| 189 |
debug($msg); |
| 190 |
print "$msg\n"; |
| 191 |
$ret; |
| 192 |
} |
| 193 |
|
| 194 |
sub _check_parent_dirs { |
| 195 |
my ($dir) = @_; |
| 196 |
my $ok = 1; |
| 197 |
my $is_abs = substr($dir, 0, 1) eq "/"; |
| 198 |
_check("$dir is an absolute path?", "Yes", "No. It should start with '/'", |
| 199 |
sub {$is_abs}); |
| 200 |
|
| 201 |
my @parts = split('/', $dir); |
| 202 |
pop @parts; # don't test the last part |
| 203 |
my $partial_path = ''; |
| 204 |
for my $path_part (@parts) { |
| 205 |
$partial_path .= $path_part . (($partial_path || $is_abs)? '/' : ''); |
| 206 |
$ok &&= |
| 207 |
_check("$partial_path is executable?", "Yes", "No", |
| 208 |
sub {-x $_[0]}, $partial_path); |
| 209 |
$ok &&= |
| 210 |
_check("$partial_path is a directory?", "Yes", "No", |
| 211 |
sub {-d $_[0]}, $partial_path); |
| 212 |
} |
| 213 |
$ok; |
| 214 |
} |
| 215 |
|
| 216 |
sub _ok_passfile_perms { |
| 217 |
my $passfile = shift @_; |
| 218 |
# libpq uses stat, so we use stat |
| 219 |
my @fstat = stat($passfile); |
| 220 |
return 0 unless @fstat; |
| 221 |
return 0 if 077 & $fstat[2]; |
| 222 |
return -r $passfile; |
| 223 |
} |
| 224 |
|
| 225 |
sub _run_psql_command { |
| 226 |
my ($self, $cmd) = @_; |
| 227 |
|
| 228 |
# n.b. deprecated, passfile recommended for better security |
| 229 |
my $orig_pgpassword = $ENV{'PGPASSWORD'}; |
| 230 |
$ENV{'PGPASSWORD'} = $self->{'props'}->{'pg-password'} if $self->{'props'}->{'pg-password'}; |
| 231 |
# n.b. supported in 8.1+ |
| 232 |
my $orig_pgpassfile = $ENV{'PGPASSFILE'}; |
| 233 |
$ENV{'PGPASSFILE'} = $self->{'props'}->{'pg-passfile'} if $self->{'props'}->{'pg-passfile'}; |
| 234 |
|
| 235 |
my @cmd = ($self->{'props'}->{'psql-path'}); |
| 236 |
push @cmd, "-X"; |
| 237 |
push @cmd, "-h", $self->{'props'}->{'pg-host'} if ($self->{'props'}->{'pg-host'}); |
| 238 |
push @cmd, "-p", $self->{'props'}->{'pg-port'} if ($self->{'props'}->{'pg-port'}); |
| 239 |
push @cmd, "-U", $self->{'props'}->{'pg-user'} if ($self->{'props'}->{'pg-user'}); |
| 240 |
|
| 241 |
if (!($cmd =~ /pg_xlogfile_name_offset/) && |
| 242 |
!($cmd =~ /pg_walfile_name_offset/) && |
| 243 |
!$self->{'args'}->{'verbose'}) { |
| 244 |
push @cmd, '--quiet', '--output', '/dev/null'; |
| 245 |
} |
| 246 |
push @cmd, '--command', $cmd; |
| 247 |
push @cmd,$self->{'props'}->{'pg-db'} if exists $self->{'props'}->{'pg-db'}; |
| 248 |
debug("running " . join(" ", @cmd)); |
| 249 |
|
| 250 |
my ($wtr, $rdr); |
| 251 |
my $err = Symbol::gensym; |
| 252 |
my $pid = open3($wtr, $rdr, $err, @cmd); |
| 253 |
close($wtr); |
| 254 |
|
| 255 |
my $file_to_close = 2; |
| 256 |
my $psql_stdout_src = Amanda::MainLoop::fd_source($rdr, |
| 257 |
$G_IO_IN|$G_IO_HUP|$G_IO_ERR); |
| 258 |
my $psql_stderr_src = Amanda::MainLoop::fd_source($err, |
| 259 |
$G_IO_IN|$G_IO_HUP|$G_IO_ERR); |
| 260 |
$psql_stdout_src->set_callback(sub { |
| 261 |
my $line = <$rdr>; |
| 262 |
if (!defined $line) { |
| 263 |
$file_to_close--; |
| 264 |
$psql_stdout_src->remove(); |
| 265 |
Amanda::MainLoop::quit() if $file_to_close == 0; |
| 266 |
return; |
| 267 |
} |
| 268 |
chomp $line; |
| 269 |
return if $line =~ /^\s*$/; |
| 270 |
debug("psql stdout: $line"); |
| 271 |
if ($cmd =~ /pg_xlogfile_name_offset/ || |
| 272 |
$cmd =~ /pg_walfile_name_offset/) { |
| 273 |
return if $line =~ /file_name/; |
| 274 |
return if $line =~ /------/; |
| 275 |
return if $line =~ /\(1 row\)/; |
| 276 |
if ($line =~ /^ ($_WAL_FILE_PAT)/) { |
| 277 |
$self->{'switch_xlog_filename'} = $1; |
| 278 |
return; |
| 279 |
} |
| 280 |
} |
| 281 |
if ($line =~ /NOTICE: pg_stop_backup complete, all required WAL segments have been archived/) { |
| 282 |
} else { |
| 283 |
$self->print_to_server("psql stdout: $line", |
| 284 |
$Amanda::Script_App::GOOD); |
| 285 |
} |
| 286 |
}); |
| 287 |
$psql_stderr_src->set_callback(sub { |
| 288 |
my $line = <$err>; |
| 289 |
if (!defined $line) { |
| 290 |
$file_to_close--; |
| 291 |
$psql_stderr_src->remove(); |
| 292 |
Amanda::MainLoop::quit() if $file_to_close == 0; |
| 293 |
return; |
| 294 |
} |
| 295 |
chomp $line; |
| 296 |
debug("psql stderr: $line"); |
| 297 |
if ($line =~ /NOTICE: pg_stop_backup complete, all required WAL segments have been archived/) { |
| 298 |
} elsif ($line =~ /could not connect to server/) { |
| 299 |
$self->print_to_server("psql stderr: $line", |
| 300 |
$Amanda::Script_App::ERROR); |
| 301 |
} else { |
| 302 |
$self->print_to_server("psql stderr: $line", |
| 303 |
$Amanda::Script_App::GOOD); |
| 304 |
} |
| 305 |
}); |
| 306 |
|
| 307 |
Amanda::MainLoop::run(); |
| 308 |
close($rdr); |
| 309 |
close($err); |
| 310 |
|
| 311 |
waitpid $pid, 0; |
| 312 |
my $status = $?; |
| 313 |
|
| 314 |
$ENV{'PGPASSWORD'} = $orig_pgpassword || ''; |
| 315 |
$ENV{'PGPASSFILE'} = $orig_pgpassfile || ''; |
| 316 |
|
| 317 |
return 0 == ($status >> 8) |
| 318 |
} |
| 319 |
|
| 320 |
sub command_selfcheck { |
| 321 |
my $self = shift; |
| 322 |
|
| 323 |
# set up to handle errors correctly |
| 324 |
$self->{'die_cb'} = sub { |
| 325 |
my ($msg) = @_; |
| 326 |
debug("$msg"); |
| 327 |
print "$msg\n"; |
| 328 |
exit(1); |
| 329 |
}; |
| 330 |
|
| 331 |
$self->print_to_server("disk " . quote_string($self->{args}->{disk}), $Amanda::Script_App::GOOD); |
| 332 |
|
| 333 |
$self->print_to_server("ampgsql version " . $Amanda::Constants::VERSION, |
| 334 |
$Amanda::Script_App::GOOD); |
| 335 |
|
| 336 |
if ($self->{'props'}->{'pg-full-wal'} ne "no" && |
| 337 |
$self->{'props'}->{'pg-full-wal'} ne "full" && |
| 338 |
$self->{'props'}->{'pg-full-wal'} ne "incr") { |
| 339 |
$self->print_to_server("FULL-WAL property must be set to 'NO', 'FULL' or 'INCR'", |
| 340 |
$Amanda::Script_App::ERROR); |
| 341 |
} |
| 342 |
for my $k (keys %{$self->{'args'}}) { |
| 343 |
print "OK application property: $k = $self->{'args'}->{$k}\n"; |
| 344 |
} |
| 345 |
|
| 346 |
_check("GNUTAR-PATH $self->{'args'}->{'gnutar-path'}", |
| 347 |
"is executable", "is NOT executable", |
| 348 |
sub {-x $_[0]}, $self->{'args'}->{'gnutar-path'}); |
| 349 |
_check("GNUTAR-PATH $self->{'args'}->{'gnutar-path'}", |
| 350 |
"is not a directory (okay)", "is a directory (it shouldn't be)", |
| 351 |
sub {!(-d $_[0])}, $self->{'args'}->{'gnutar-path'}); |
| 352 |
_check_parent_dirs($self->{'args'}->{'gnutar-path'}); |
| 353 |
|
| 354 |
_check("GNUTAR $Amanda::Constants::GNUTAR", |
| 355 |
"is executable", "is NOT executable", |
| 356 |
sub {-x $_[0]}, $Amanda::Constants::GNUTAR); |
| 357 |
_check("GNUTAR $Amanda::Constants::GNUTAR", |
| 358 |
"is not a directory (okay)", "is a directory (it shouldn't be)", |
| 359 |
sub {!(-d $_[0])}, $Amanda::Constants::GNUTAR); |
| 360 |
_check_parent_dirs($Amanda::Constants::GNUTAR); |
| 361 |
|
| 362 |
_check("TMPDIR '$self->{'args'}->{'tmpdir'}'", |
| 363 |
"is an acessible directory", "is NOT an acessible directory", |
| 364 |
sub {$_[0] && -d $_[0] && -r $_[0] && -w $_[0] && -x $_[0]}, |
| 365 |
$self->{'args'}->{'tmpdir'}); |
| 366 |
|
| 367 |
if (exists $self->{'props'}->{'pg-datadir'}) { |
| 368 |
_check("PG-DATADIR property is", |
| 369 |
"same as diskdevice", "differrent than diskdevice", |
| 370 |
sub { $_[0] eq $_[1] }, |
| 371 |
$self->{'props'}->{'pg-datadir'}, $self->{'args'}->{'device'}); |
| 372 |
} else { |
| 373 |
$self->{'props'}->{'pg-datadir'} = $self->{'args'}->{'device'}; |
| 374 |
} |
| 375 |
|
| 376 |
_check("PG-DATADIR property", "is set", "is NOT set", |
| 377 |
sub { $_[0] }, $self->{'props'}->{'pg-datadir'}); |
| 378 |
# note that the backup user need not be able ot read this dir |
| 379 |
|
| 380 |
_check("STATEDIR '$self->{'args'}->{'statedir'}'", |
| 381 |
"is an acessible directory", "is NOT an acessible directory", |
| 382 |
sub {$_[0] && -d $_[0] && -r $_[0] && -w $_[0] && -x $_[0]}, |
| 383 |
$self->{'args'}->{'statedir'}); |
| 384 |
_check_parent_dirs($self->{'args'}->{'statedir'}); |
| 385 |
|
| 386 |
if ($self->{'args'}->{'device'}) { |
| 387 |
my $try_connect = 1; |
| 388 |
|
| 389 |
for my $k (keys %{$self->{'props'}}) { |
| 390 |
print "OK client property: $k = $self->{'props'}->{$k}\n"; |
| 391 |
} |
| 392 |
|
| 393 |
if (_check("PG-ARCHIVEDIR property", "is set", "is NOT set", |
| 394 |
sub { $_[0] }, $self->{'props'}->{'pg-archivedir'})) { |
| 395 |
_check("PG-ARCHIVEDIR $self->{'props'}->{'pg-archivedir'}", |
| 396 |
"is a directory", "is NOT a directory", |
| 397 |
sub {-d $_[0]}, $self->{'props'}->{'pg-archivedir'}); |
| 398 |
_check("PG-ARCHIVEDIR $self->{'props'}->{'pg-archivedir'}", |
| 399 |
"is readable", "is NOT readable", |
| 400 |
sub {-r $_[0]}, $self->{'props'}->{'pg-archivedir'}); |
| 401 |
_check("PG-ARCHIVEDIR $self->{'props'}->{'pg-archivedir'}", |
| 402 |
"is executable", "is NOT executable", |
| 403 |
sub {-x $_[0]}, $self->{'props'}->{'pg-archivedir'}); |
| 404 |
_check_parent_dirs($self->{'props'}->{'pg-archivedir'}); |
| 405 |
} |
| 406 |
|
| 407 |
if (defined $self->{'props'}->{'pg-datadir'} and |
| 408 |
defined $self->{'props'}->{'pg-archivedir'}) { |
| 409 |
_check("ARCHIVEDIR", "is not inside DATADIR", "is inside DATADIR", |
| 410 |
sub {$self->{'props'}->{'pg-datadir'} ne substr($self->{'props'}->{'pg-archivedir'}, 0, length($self->{'props'}->{'pg-datadir'}))}); |
| 411 |
} |
| 412 |
|
| 413 |
$try_connect &&= |
| 414 |
_check("Are both PG-PASSFILE and PG-PASSWORD set?", |
| 415 |
"No (okay)", |
| 416 |
"Yes. Please set only one or the other", |
| 417 |
sub {!($self->{'props'}->{'pg-passfile'} and |
| 418 |
$self->{'props'}->{'pg-password'})}); |
| 419 |
|
| 420 |
if ($self->{'props'}->{'pg-passfile'}) { |
| 421 |
$try_connect &&= |
| 422 |
_check("PG-PASSFILE $self->{'props'}->{'pg-passfile'}", |
| 423 |
"has correct permissions", "does not have correct permissions", |
| 424 |
\&_ok_passfile_perms, $self->{'props'}->{'pg-passfile'}); |
| 425 |
$try_connect &&= |
| 426 |
_check_parent_dirs($self->{'props'}->{'pg-passfile'}); |
| 427 |
} |
| 428 |
|
| 429 |
if (_check("PSQL-PATH property", "is set", "is NOT set and psql is not in \$PATH", |
| 430 |
sub { $_[0] }, $self->{'props'}->{'psql-path'})) { |
| 431 |
$try_connect &&= |
| 432 |
_check("PSQL-PATH $self->{'props'}->{'psql-path'}", |
| 433 |
"is executable", "is NOT executable", |
| 434 |
sub {-x $_[0]}, $self->{'props'}->{'psql-path'}); |
| 435 |
$try_connect &&= |
| 436 |
_check("PSQL-PATH $self->{'props'}->{'psql-path'}", |
| 437 |
"is not a directory (okay)", "is a directory (it shouldn't be)", |
| 438 |
sub {!(-d $_[0])}, $self->{'props'}->{'psql-path'}); |
| 439 |
$try_connect &&= |
| 440 |
_check_parent_dirs($self->{'props'}->{'psql-path'}); |
| 441 |
} else { |
| 442 |
$try_connect = 0; |
| 443 |
} |
| 444 |
|
| 445 |
if ($try_connect) { |
| 446 |
my @pv = `$self->{'props'}->{'psql-path'} --version`; |
| 447 |
if ($? >> 8 == 0) { |
| 448 |
$pv[0] =~ /^[^0-9]*([0-9.]*)[^0-9]*$/; |
| 449 |
my $pv = $1; |
| 450 |
$self->print_to_server("ampgsql psql-version $pv", |
| 451 |
$Amanda::Script_App::GOOD); |
| 452 |
} else { |
| 453 |
$self->print_to_server( |
| 454 |
"[Can't get " . $self->{'props'}->{'psql-path'} . " version]\n", |
| 455 |
$Amanda::Script_App::ERROR); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
if ($try_connect) { |
| 460 |
$try_connect &&= |
| 461 |
_check("Connecting to database server", "succeeded", "failed", |
| 462 |
\&_run_psql_command, $self, ''); |
| 463 |
} |
| 464 |
|
| 465 |
{ |
| 466 |
my @gv = `$self->{'args'}->{'gnutar-path'} --version`; |
| 467 |
if ($? >> 8 == 0) { |
| 468 |
$gv[0] =~ /^[^0-9]*([0-9.]*)[^0-9]*$/; |
| 469 |
my $gv = $1; |
| 470 |
$self->print_to_server("ampgsql gtar-version $gv", |
| 471 |
$Amanda::Script_App::GOOD); |
| 472 |
} else { |
| 473 |
$self->print_to_server( |
| 474 |
"[Can't get " . $self->{'props'}->{'gnutar-path'} . " version]\n", |
| 475 |
$Amanda::Script_App::ERROR); |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
sub _filename_prefix { |
| 482 |
my $self = shift; |
| 483 |
|
| 484 |
my @parts = ("ampgsql", hexencode($self->{'args'}->{'host'}), hexencode($self->{'args'}->{'disk'})); |
| 485 |
return $self->{'args'}->{'statedir'} . '/' . join("-", @parts) |
| 486 |
} |
| 487 |
|
| 488 |
sub _state_filename { |
| 489 |
my ($self, $level) = @_; |
| 490 |
|
| 491 |
my $prefix = $self->_filename_prefix(); |
| 492 |
my $statefile = $prefix . "-$level"; |
| 493 |
debug("statefile: $statefile"); |
| 494 |
return $statefile; |
| 495 |
} |
| 496 |
|
| 497 |
sub _last_backup_filename { |
| 498 |
my $self = shift; |
| 499 |
|
| 500 |
my $prefix = $self->_filename_prefix(); |
| 501 |
my $lastfile = $prefix . "-last_backup"; |
| 502 |
debug("lastfile $lastfile"); |
| 503 |
return $lastfile |
| 504 |
} |
| 505 |
|
| 506 |
sub _get_last_backup { |
| 507 |
my $self = shift; |
| 508 |
|
| 509 |
my $h = new IO::File($self->_last_backup_filename(), "r"); |
| 510 |
$h or return undef; |
| 511 |
|
| 512 |
my $line = <$h>; |
| 513 |
$line =~ /LEVEL: (\d*)/; |
| 514 |
my $level = $1; |
| 515 |
$h->close(); |
| 516 |
|
| 517 |
return $level; |
| 518 |
} |
| 519 |
|
| 520 |
sub _write_last_backup { |
| 521 |
my $self = shift; |
| 522 |
my $level = shift; |
| 523 |
|
| 524 |
my $h = new IO::File($self->_last_backup_filename(), "w"); |
| 525 |
$h or return undef; |
| 526 |
|
| 527 |
debug("writing last file"); |
| 528 |
$h->print("LEVEL: $level\n"); |
| 529 |
$h->close; |
| 530 |
} |
| 531 |
|
| 532 |
sub _write_state_file { |
| 533 |
my ($self, $end_wal) = @_; |
| 534 |
|
| 535 |
my $h = new IO::File(_state_filename($self, $self->{'args'}->{'level'}), "w"); |
| 536 |
$h or return undef; |
| 537 |
|
| 538 |
debug("writing state file"); |
| 539 |
$h->print("VERSION: 0\n"); |
| 540 |
$h->print("LAST WAL FILE: $end_wal\n"); |
| 541 |
$h->close(); |
| 542 |
1; |
| 543 |
} |
| 544 |
|
| 545 |
sub _get_prev_state { |
| 546 |
my $self = shift @_; |
| 547 |
my $initial_level = shift; |
| 548 |
$initial_level = $self->{'args'}->{'level'} - 1 if !defined $initial_level; |
| 549 |
|
| 550 |
my $end_wal; |
| 551 |
for (my $level = $initial_level; $level >= 0; $level--) { |
| 552 |
my $fn = _state_filename($self, $level); |
| 553 |
debug("reading state file: $fn"); |
| 554 |
my $h = new IO::File($fn, "r"); |
| 555 |
next unless $h; |
| 556 |
while (my $l = <$h>) { |
| 557 |
chomp $l; |
| 558 |
debug(" $l"); |
| 559 |
if ($l =~ /^VERSION: (\d+)/) { |
| 560 |
unless (0 == $1) { |
| 561 |
$end_wal = undef; |
| 562 |
last; |
| 563 |
} |
| 564 |
} elsif ($l =~ /^LAST WAL FILE: ($_WAL_FILE_PAT)/) { |
| 565 |
$end_wal = $1; |
| 566 |
} |
| 567 |
} |
| 568 |
$h->close(); |
| 569 |
last if $end_wal; |
| 570 |
} |
| 571 |
$end_wal; |
| 572 |
} |
| 573 |
|
| 574 |
sub _make_dummy_dir_base { |
| 575 |
my ($self) = @_; |
| 576 |
|
| 577 |
my $dummydir = "$self->{'args'}->{'tmpdir'}/ampgsql-dummy-$$"; |
| 578 |
mkpath("$dummydir/$_ARCHIVE_DIR_RESTORE"); |
| 579 |
|
| 580 |
return $dummydir; |
| 581 |
} |
| 582 |
|
| 583 |
sub _make_dummy_dir { |
| 584 |
my ($self) = @_; |
| 585 |
|
| 586 |
my $dummydir = "$self->{'args'}->{'tmpdir'}/ampgsql-dummy-$$"; |
| 587 |
mkpath($dummydir); |
| 588 |
open(my $fh, ">$dummydir/empty-incremental"); |
| 589 |
close($fh); |
| 590 |
|
| 591 |
return $dummydir; |
| 592 |
} |
| 593 |
|
| 594 |
sub _run_tar_totals { |
| 595 |
my ($self, @other_args) = @_; |
| 596 |
|
| 597 |
my @cmd; |
| 598 |
@cmd = ($self->{'runtar'}, $self->{'args'}->{'config'}, |
| 599 |
$Amanda::Constants::GNUTAR, '--create', '--totals', |
| 600 |
'--blocking-factor', '64', |
| 601 |
@other_args); |
| 602 |
debug("running: " . join(" ", @cmd)); |
| 603 |
|
| 604 |
local (*TAR_IN, *TAR_OUT, *TAR_ERR); |
| 605 |
open TAR_OUT, ">&", $self->{'out_h'}; |
| 606 |
my $pid; |
| 607 |
eval { $pid = open3(\*TAR_IN, ">&TAR_OUT", \*TAR_ERR, @cmd); 1;} or |
| 608 |
$self->{'die_cb'}->("failed to run tar. error was $@"); |
| 609 |
close(TAR_IN); |
| 610 |
|
| 611 |
# read stderr |
| 612 |
my $size; |
| 613 |
while (my $l = <TAR_ERR>) { |
| 614 |
if ($l =~ /^Total bytes written: (\d+)/) { |
| 615 |
$size = $1; |
| 616 |
} elsif ($l =~ /file changed as we read it/ || |
| 617 |
$l =~ /File removed before we read it/ || |
| 618 |
$l =~ /File shrank by \d* bytes; padding with zeros/) { |
| 619 |
chomp $l; |
| 620 |
$self->print_to_server($l, $Amanda::Script_App::GOOD); |
| 621 |
debug("TAR_ERR: $l"); |
| 622 |
} else { |
| 623 |
chomp $l; |
| 624 |
$self->print_to_server($l, $Amanda::Script_App::ERROR); |
| 625 |
debug("TAR_ERR: $l"); |
| 626 |
} |
| 627 |
} |
| 628 |
waitpid($pid, 0); |
| 629 |
my $status = POSIX::WEXITSTATUS($?); |
| 630 |
|
| 631 |
close(TAR_ERR); |
| 632 |
debug("size of generated tar file: " . (defined($size)? $size : "undef")); |
| 633 |
if ($status == 1) { |
| 634 |
debug("ignored non-fatal tar exit status of 1"); |
| 635 |
} elsif ($status) { |
| 636 |
$self->{'die_cb'}->("Tar failed (exit status $status)"); |
| 637 |
} |
| 638 |
$size; |
| 639 |
} |
| 640 |
|
| 641 |
sub command_estimate { |
| 642 |
my $self = shift; |
| 643 |
|
| 644 |
$self->{'out_h'} = new IO::File("/dev/null", "w"); |
| 645 |
$self->{'out_h'} or die("Could not open /dev/null"); |
| 646 |
$self->{'index_h'} = new IO::File("/dev/null", "w"); |
| 647 |
$self->{'index_h'} or die("Could not open /dev/null"); |
| 648 |
|
| 649 |
$self->{'done_cb'} = sub { |
| 650 |
my $size = shift @_; |
| 651 |
debug("done. size $size"); |
| 652 |
$size = ceil($size/1024); |
| 653 |
debug("sending $self->{'args'}->{'level'} $size 1"); |
| 654 |
print("$self->{'args'}->{'level'} $size 1\n"); |
| 655 |
}; |
| 656 |
$self->{'die_cb'} = sub { |
| 657 |
my $msg = shift @_; |
| 658 |
debug("$msg"); |
| 659 |
$self->{'done_cb'}->(-1); |
| 660 |
die($msg); |
| 661 |
}; |
| 662 |
$self->{'state_cb'} = sub { |
| 663 |
# do nothing |
| 664 |
}; |
| 665 |
$self->{'unlink_cb'} = sub { |
| 666 |
# do nothing |
| 667 |
}; |
| 668 |
|
| 669 |
if ($self->{'args'}->{'level'} > 0) { |
| 670 |
_incr_backup($self); |
| 671 |
} else { |
| 672 |
_base_backup($self); |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
sub _get_backup_info { |
| 677 |
my ($self, $label) = @_; |
| 678 |
|
| 679 |
my ($fname, $bfile, $start_wal, $end_wal); |
| 680 |
# wait up to 60s for the .backup file to be copied |
| 681 |
for (my $count = 0; $count < 60; $count++) { |
| 682 |
my $adir = new IO::Dir($self->{'props'}->{'pg-archivedir'}); |
| 683 |
$adir or $self->{'die_cb'}->("Could not open archive WAL directory"); |
| 684 |
while (defined($fname = $adir->read())) { |
| 685 |
if ($fname =~ /\.backup$/) { |
| 686 |
my $blabel; |
| 687 |
# use runtar to read a protected file, then grep the resulting tarfile (yes, |
| 688 |
# this works!) |
| 689 |
local *TAROUT; |
| 690 |
my $conf = $self->{'args'}->{'config'} || 'NOCONFIG'; |
| 691 |
my $cmd = "$self->{'runtar'} $conf $Amanda::Constants::GNUTAR --create --dereference --file - --directory $self->{'props'}->{'pg-archivedir'} $fname | $Amanda::Constants::GNUTAR --file - --extract --to-stdout"; |
| 692 |
debug("running: $cmd"); |
| 693 |
open(TAROUT, "$cmd |"); |
| 694 |
my ($start, $end, $lab); |
| 695 |
while (my $l = <TAROUT>) { |
| 696 |
chomp($l); |
| 697 |
if ($l =~ /^START WAL LOCATION:.*?\(file ($_WAL_FILE_PAT)\)$/) { |
| 698 |
$start = $1; |
| 699 |
} elsif($l =~ /^STOP WAL LOCATION:.*?\(file ($_WAL_FILE_PAT)\)$/) { |
| 700 |
$end = $1; |
| 701 |
} elsif ($l =~ /^LABEL: (.*)$/) { |
| 702 |
$lab = $1; |
| 703 |
} |
| 704 |
} |
| 705 |
close TAROUT; |
| 706 |
if ($lab and $lab eq $label) { |
| 707 |
$start_wal = $start; |
| 708 |
$end_wal = $end; |
| 709 |
$bfile = $fname; |
| 710 |
last; |
| 711 |
} else { |
| 712 |
debug("logfile had non-matching label"); |
| 713 |
} |
| 714 |
} |
| 715 |
} |
| 716 |
$adir->close(); |
| 717 |
if ($start_wal and $end_wal) { |
| 718 |
debug("$bfile named WALs $start_wal .. $end_wal"); |
| 719 |
|
| 720 |
# try to cleanup a bit, although this may fail and that's ok |
| 721 |
my $filename = "$self->{'props'}->{'pg-archivedir'}/$bfile"; |
| 722 |
if (unlink($filename) == 0) { |
| 723 |
debug("Failed to unlink '$filename': $!"); |
| 724 |
$self->print_to_server("Failed to unlink '$filename': $!", |
| 725 |
$Amanda::Script_App::ERROR); |
| 726 |
} |
| 727 |
last; |
| 728 |
} |
| 729 |
sleep(1); |
| 730 |
} |
| 731 |
|
| 732 |
($start_wal, $end_wal); |
| 733 |
} |
| 734 |
|
| 735 |
# return the postgres version as an integer |
| 736 |
sub _get_pg_version { |
| 737 |
my $self = shift; |
| 738 |
|
| 739 |
local *VERSOUT; |
| 740 |
|
| 741 |
my @cmd = ($self->{'props'}->{'psql-path'}); |
| 742 |
push @cmd, "-X"; |
| 743 |
push @cmd, "--version"; |
| 744 |
my $pid = open3('>STDIN', \*VERSOUT, '>STDERR', @cmd) |
| 745 |
or $self->{'die_cb'}->("could not open psql to determine version"); |
| 746 |
my @lines = <VERSOUT>; |
| 747 |
waitpid($pid, 0); |
| 748 |
$self->{'die_cb'}->("could not run psql to determine version") if (($? >> 8) != 0); |
| 749 |
|
| 750 |
my ($maj, $min, $pat); |
| 751 |
if ($lines[0] =~ /([0-9]+)\.([0-9]+)\.([0-9]+)/) { |
| 752 |
$maj = $1; |
| 753 |
$min = $2; |
| 754 |
$pat = $3; |
| 755 |
} elsif ($lines[0] =~ /([0-9]+)\.([0-9]+)/) { |
| 756 |
$maj = $1; |
| 757 |
$min = $2; |
| 758 |
$pat = 0; |
| 759 |
} elsif ($lines[0] =~ /([0-9]+)/) { |
| 760 |
$maj = $1; |
| 761 |
$min = 0; |
| 762 |
$pat = 0; |
| 763 |
} |
| 764 |
|
| 765 |
return $maj * 10000 + $min * 100 + $pat; |
| 766 |
} |
| 767 |
|
| 768 |
# create a large table and immediately drop it; this can help to push a WAL file out |
| 769 |
sub _write_garbage_to_db { |
| 770 |
my $self = shift; |
| 771 |
|
| 772 |
debug("writing garbage to database to force a WAL archive"); |
| 773 |
|
| 774 |
# note: lest ye be tempted to add "if exists" to the drop table here, note that |
| 775 |
# the clause was not supported in 8.1 |
| 776 |
_run_psql_command($self, <<EOF) or |
| 777 |
CREATE TABLE _ampgsql_garbage AS SELECT * FROM GENERATE_SERIES(1, 500000); |
| 778 |
DROP TABLE _ampgsql_garbage; |
| 779 |
EOF |
| 780 |
$self->{'die_cb'}->("Failed to create or drop table _ampgsql_garbage"); |
| 781 |
} |
| 782 |
|
| 783 |
# wait up to pg-max-wal-wait seconds for a WAL file to appear |
| 784 |
sub _wait_for_wal { |
| 785 |
my ($self, $wal) = @_; |
| 786 |
|
| 787 |
my $archive_dir = $self->{'props'}->{'pg-archivedir'}; |
| 788 |
my $maxwait = 0+$self->{'props'}->{'pg-max-wal-wait'}; |
| 789 |
|
| 790 |
if ($maxwait) { |
| 791 |
debug("waiting $maxwait s for WAL $wal to be archived.."); |
| 792 |
} else { |
| 793 |
debug("waiting forever for WAL $wal to be archived.."); |
| 794 |
} |
| 795 |
|
| 796 |
my $count = 0; # try at least 4 cycles |
| 797 |
my $stoptime = time() + $maxwait; |
| 798 |
while ($maxwait == 0 || time < $stoptime || $count++ < 4) { |
| 799 |
if (-f "$archive_dir/$wal") { |
| 800 |
sleep(1); |
| 801 |
return; |
| 802 |
} |
| 803 |
|
| 804 |
# for versions 8.0 or 8.1, the only way to "force" a WAL archive is to write |
| 805 |
# garbage to the database. |
| 806 |
if ($self->{'pg-version'} < 80200) { |
| 807 |
$self->_write_garbage_to_db(); |
| 808 |
} else { |
| 809 |
sleep(1); |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
$self->{'die_cb'}->("WAL file $wal was not archived in $maxwait seconds"); |
| 814 |
} |
| 815 |
|
| 816 |
sub _remove_wal_files { |
| 817 |
my $self = shift; |
| 818 |
my $start_wal = shift; |
| 819 |
my $last_end_wal = shift; |
| 820 |
|
| 821 |
if (defined $start_wal || defined $last_end_wal) { |
| 822 |
my $adir = new IO::Dir($self->{'props'}->{'pg-archivedir'}); |
| 823 |
$adir or $self->{'die_cb'}->("Could not open archive WAL directory"); |
| 824 |
while (defined(my $fname = $adir->read())) { |
| 825 |
if ($fname =~ /^$_WAL_FILE_PAT$/) { |
| 826 |
if ((defined $start_wal and $fname lt $start_wal) || |
| 827 |
(defined $last_end_wal and $fname lt $last_end_wal)) { |
| 828 |
$self->{'unlink_cb'}->("$self->{'props'}->{'pg-archivedir'}/$fname"); |
| 829 |
} |
| 830 |
} |
| 831 |
} |
| 832 |
$adir->close(); |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
sub _base_backup { |
| 837 |
my ($self) = @_; |
| 838 |
|
| 839 |
debug("running _base_backup"); |
| 840 |
|
| 841 |
my $label = "$self->{'label-prefix'}-" . time(); |
| 842 |
|
| 843 |
-d $self->{'props'}->{'pg-archivedir'} or |
| 844 |
die("WAL file archive directory does not exist (or is not a directory)"); |
| 845 |
|
| 846 |
if ($self->{'action'} eq 'backup') { |
| 847 |
_run_psql_command($self, "SELECT pg_start_backup('$label')") or |
| 848 |
$self->{'die_cb'}->("Failed to call pg_start_backup"); |
| 849 |
} |
| 850 |
|
| 851 |
# tar data dir, using symlink to prefix |
| 852 |
# XXX: tablespaces and their symlinks? |
| 853 |
# See: http://www.postgresql.org/docs/8.0/static/manage-ag-tablespaces.html |
| 854 |
my $old_die_cb = $self->{'die_cb'}; |
| 855 |
$self->{'die_cb'} = sub { |
| 856 |
my $msg = shift @_; |
| 857 |
if ($self->{'action'} eq 'backup') { |
| 858 |
unless(_run_psql_command($self, "SELECT pg_stop_backup()")) { |
| 859 |
$msg .= " and failed to call pg_stop_backup"; |
| 860 |
} |
| 861 |
} |
| 862 |
$old_die_cb->($msg); |
| 863 |
}; |
| 864 |
|
| 865 |
my @args = ('--dereference', '--file', "-", |
| 866 |
'--directory', $self->{'props'}->{'pg-datadir'}, |
| 867 |
'--exclude', 'postmaster.pid', |
| 868 |
'--exclude', 'pg_xlog/*', # contains WAL files; will be handled below |
| 869 |
'--exclude', 'pg_wal/*', # contains WAL files; will be handled below |
| 870 |
'--exclude', 'postmaster.pid', |
| 871 |
'--exclude', 'postmaster.opts', |
| 872 |
'--exclude', 'pg_replslot/*'); |
| 873 |
if (defined $self->{args}->{'exclude-file'}) { |
| 874 |
foreach my $excl (@{$self->{args}->{'exclude-file'}}) { |
| 875 |
push @args, '--exclude', $excl; |
| 876 |
} |
| 877 |
} |
| 878 |
if (defined $self->{args}->{'exclude-list'}) { |
| 879 |
foreach my $excl (@{$self->{args}->{'exclude-list'}}) { |
| 880 |
push @args, '--exclude-from', $excl; |
| 881 |
} |
| 882 |
} |
| 883 |
push @args, '--transform', "s,^./pg_tblspc,./pg_tblspc_data,S;s,^,$_DATA_DIR_RESTORE/,S", |
| 884 |
'.'; |
| 885 |
|
| 886 |
my $size = _run_tar_totals($self,@args); |
| 887 |
|
| 888 |
# tar the symlink in pg_tblspc |
| 889 |
$size += _run_tar_totals($self, '--file', "-", |
| 890 |
'--directory', "$self->{'props'}->{'pg-datadir'}", |
| 891 |
'--transform', "s,^,$_DATA_DIR_RESTORE/,S", |
| 892 |
"./pg_tblspc"); |
| 893 |
|
| 894 |
if ($self->{'action'} eq 'backup') { |
| 895 |
unless (_run_psql_command($self, "SELECT pg_stop_backup()")) { |
| 896 |
$self->{'die_cb'}->("Failed to call pg_stop_backup"); |
| 897 |
} |
| 898 |
} |
| 899 |
$self->{'die_cb'} = $old_die_cb; |
| 900 |
|
| 901 |
# determine WAL files and append and create their tar file |
| 902 |
my $last_end_wal; |
| 903 |
my $start_wal; |
| 904 |
my $end_wal; |
| 905 |
|
| 906 |
if ($self->{'action'} eq 'backup') { |
| 907 |
($start_wal, $end_wal) = _get_backup_info($self, $label); |
| 908 |
($start_wal and $end_wal) |
| 909 |
or $self->{'die_cb'}->("A .backup file was never found in the archive " |
| 910 |
. "dir $self->{'props'}->{'pg-archivedir'}"); |
| 911 |
$self->_wait_for_wal($end_wal); |
| 912 |
if ($self->{'props'}->{'pg-full-wal'} eq "full") { |
| 913 |
$last_end_wal = _get_prev_state($self, 0); |
| 914 |
} elsif ($self->{'props'}->{'pg-full-wal'} eq "incr") { |
| 915 |
my $level = $self->_get_last_backup(); |
| 916 |
$last_end_wal = _get_prev_state($self, $level); |
| 917 |
} |
| 918 |
} else { |
| 919 |
$start_wal = undef; |
| 920 |
$end_wal = _get_prev_state($self, 0); |
| 921 |
} |
| 922 |
|
| 923 |
# now grab all of the WAL files, *inclusive* of $start_wal |
| 924 |
my $cleanup_wal_val = $self->{'props'}->{'pg-cleanupwal'} || 'yes'; |
| 925 |
my $cleanup_wal = string_to_boolean($cleanup_wal_val); |
| 926 |
my @wal_files; |
| 927 |
my $adir = new IO::Dir($self->{'props'}->{'pg-archivedir'}); |
| 928 |
$adir or $self->{'die_cb'}->("Could not open archive WAL directory"); |
| 929 |
while (defined(my $fname = $adir->read())) { |
| 930 |
if ($fname =~ /^$_WAL_FILE_PAT$/) { |
| 931 |
if (!defined $end_wal || |
| 932 |
(!defined $start_wal and ($fname le $end_wal)) || |
| 933 |
(defined $start_wal and ($fname ge $start_wal) and |
| 934 |
($fname le $end_wal)) || |
| 935 |
(defined $last_end_wal and ($fname gt $last_end_wal) and ($fname le $end_wal))) { |
| 936 |
push @wal_files, $fname; |
| 937 |
debug("will store: $fname"); |
| 938 |
} elsif (defined $start_wal and $fname lt $start_wal) { |
| 939 |
if ($cleanup_wal) { |
| 940 |
$self->{'unlink_cb'}->("$self->{'props'}->{'pg-archivedir'}/$fname"); |
| 941 |
} |
| 942 |
} |
| 943 |
} |
| 944 |
} |
| 945 |
$adir->close(); |
| 946 |
|
| 947 |
if (@wal_files) { |
| 948 |
$size += _run_tar_totals($self, '--dereference', '--file', "-", |
| 949 |
'--directory', $self->{'props'}->{'pg-archivedir'}, |
| 950 |
'--transform', "s,^,$_ARCHIVE_DIR_RESTORE/,S", |
| 951 |
@wal_files); |
| 952 |
#take the directory itself |
| 953 |
$size += _run_tar_totals($self, '--dereference', '--file', "-", |
| 954 |
'--directory', $self->{'props'}->{'pg-archivedir'}, |
| 955 |
'--transform', "s,^,$_ARCHIVE_DIR_RESTORE/,S", |
| 956 |
'--no-recursion', '.'); |
| 957 |
} else { |
| 958 |
my $dummydir = $self->_make_dummy_dir_base(); |
| 959 |
$self->{'done_cb'}->(_run_tar_totals($self, '--dereference', '--file', '-', |
| 960 |
'--directory', $dummydir, "$_ARCHIVE_DIR_RESTORE")); |
| 961 |
rmtree($dummydir); |
| 962 |
} |
| 963 |
|
| 964 |
# should be done only on confirmation the backup succeeded from the server |
| 965 |
$self->{'state_cb'}->($self, $end_wal); |
| 966 |
|
| 967 |
# remove older WAL files. |
| 968 |
my $remove_full_wal_val = $self->{'props'}->{'pg-remove-full-wal'} || 'yes'; |
| 969 |
my $remove_full_wal = string_to_boolean($remove_full_wal_val); |
| 970 |
# should be done only on confirmation the backup succeeded from the server |
| 971 |
if ($remove_full_wal) { |
| 972 |
$self->_remove_wal_files($start_wal, $last_end_wal); |
| 973 |
} |
| 974 |
|
| 975 |
$self->{'done_cb'}->($size); |
| 976 |
} |
| 977 |
|
| 978 |
sub _incr_backup { |
| 979 |
my ($self) = @_; |
| 980 |
|
| 981 |
debug("running _incr_backup"); |
| 982 |
|
| 983 |
my $incremental_val = $self->{'props'}->{'pg-incremental'} || 'yes'; |
| 984 |
my $incremental = string_to_boolean($incremental_val); |
| 985 |
|
| 986 |
if ($self->{'action'} eq 'backup') { |
| 987 |
if ($self->{'pg-version'} < 100000) { |
| 988 |
_run_psql_command($self, "SELECT file_name from pg_xlogfile_name_offset(pg_switch_xlog())"); |
| 989 |
} else { |
| 990 |
_run_psql_command($self, "SELECT file_name from pg_walfile_name_offset(pg_switch_wal())"); |
| 991 |
} |
| 992 |
if (defined($self->{'switch_xlog_filename'})) { |
| 993 |
$self->_wait_for_wal($self->{'switch_xlog_filename'}); |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
my $end_wal; |
| 998 |
if ($incremental) { |
| 999 |
$end_wal = _get_prev_state($self, $self->{'args'}->{'level'}); |
| 1000 |
for (my $level = $self->{'args'}->{'level'}-1; $level >= 0; $level--) { |
| 1001 |
my $xend_wal = _get_prev_state($self, $level); |
| 1002 |
if (!$end_wal || $xend_wal lt $end_wal) { |
| 1003 |
$end_wal = $xend_wal; |
| 1004 |
} |
| 1005 |
} |
| 1006 |
} else { |
| 1007 |
$end_wal = _get_prev_state($self); |
| 1008 |
} |
| 1009 |
if ($end_wal) { |
| 1010 |
debug("previously ended at: $end_wal"); |
| 1011 |
} else { |
| 1012 |
debug("no previous state found!"); |
| 1013 |
return _base_backup(@_); |
| 1014 |
} |
| 1015 |
|
| 1016 |
my $adir = new IO::Dir($self->{'props'}->{'pg-archivedir'}); |
| 1017 |
$adir or $self->{'die_cb'}->("Could not open archive WAL directory"); |
| 1018 |
my $max_wal = ""; |
| 1019 |
my ($fname, @wal_files); |
| 1020 |
while (defined($fname = $adir->read())) { |
| 1021 |
if (($fname =~ /^$_WAL_FILE_PAT$/) and ($fname gt $end_wal)) { |
| 1022 |
$max_wal = $fname if $fname gt $max_wal; |
| 1023 |
push @wal_files, $fname; |
| 1024 |
debug("will store: $fname"); |
| 1025 |
} |
| 1026 |
} |
| 1027 |
|
| 1028 |
my $size; |
| 1029 |
if (@wal_files) { |
| 1030 |
$size = _run_tar_totals($self, '--dereference', '--file', '-', |
| 1031 |
'--directory', $self->{'props'}->{'pg-archivedir'}, @wal_files); |
| 1032 |
} else { |
| 1033 |
my $dummydir = $self->_make_dummy_dir(); |
| 1034 |
$size = _run_tar_totals($self, '--dereference', '--file', '-', |
| 1035 |
'--directory', $dummydir, "empty-incremental"); |
| 1036 |
rmtree($dummydir); |
| 1037 |
} |
| 1038 |
|
| 1039 |
# should be done only on confirmation the backup succeeded from the server |
| 1040 |
$self->{'state_cb'}->($self, $max_wal ? $max_wal : $end_wal); |
| 1041 |
|
| 1042 |
my $remove_incremental_wal_val = $self->{'props'}->{'pg-remove-incremental-wal'} || 'no'; |
| 1043 |
my $remove_incremental_wal = string_to_boolean($remove_incremental_wal_val); |
| 1044 |
# should be done only on confirmation the backup succeeded from the server |
| 1045 |
if ($remove_incremental_wal) { |
| 1046 |
my $end_wal = pop @wal_files; # Do not unlink the latest wal files |
| 1047 |
foreach my $wal_file (@wal_files) { |
| 1048 |
$end_wal = $wal_file if $wal_file gt $end_wal; |
| 1049 |
} |
| 1050 |
$self->_remove_wal_files(undef, $end_wal); |
| 1051 |
} |
| 1052 |
|
| 1053 |
$self->{'done_cb'}->($size); |
| 1054 |
} |
| 1055 |
|
| 1056 |
sub command_backup { |
| 1057 |
my $self = shift; |
| 1058 |
|
| 1059 |
$self->{'out_h'} = IO::Handle->new_from_fd(1, 'w'); |
| 1060 |
$self->{'out_h'} or die("Could not open data fd"); |
| 1061 |
my $msg_fd = IO::Handle->new_from_fd(3, 'w'); |
| 1062 |
$msg_fd or die("Could not open message fd"); |
| 1063 |
$self->{'index_h'} = IO::Handle->new_from_fd(4, 'w'); |
| 1064 |
$self->{'index_h'} or die("Could not open index fd"); |
| 1065 |
|
| 1066 |
$self->{'done_cb'} = sub { |
| 1067 |
my $size = shift @_; |
| 1068 |
debug("done. size $size"); |
| 1069 |
$size = ceil($size/1024); |
| 1070 |
debug("sending size $size"); |
| 1071 |
$msg_fd->print("sendbackup: size $size\n"); |
| 1072 |
|
| 1073 |
$self->{'index_h'}->print("/PostgreSQL-Database-$self->{'args'}->{'level'}\n"); |
| 1074 |
|
| 1075 |
}; |
| 1076 |
$self->{'die_cb'} = sub { |
| 1077 |
my $msg = shift @_; |
| 1078 |
debug("$msg"); |
| 1079 |
$msg_fd->print("! $msg\n"); |
| 1080 |
$self->{'done_cb'}->(0); |
| 1081 |
exit(1); |
| 1082 |
}; |
| 1083 |
$self->{'state_cb'} = sub { |
| 1084 |
my ($self, $end_wal) = @_; |
| 1085 |
_write_state_file($self, $end_wal) or $self->{'die_cb'}->("Failed to write state file"); |
| 1086 |
$self->_write_last_backup($self->{'args'}->{'level'}); |
| 1087 |
}; |
| 1088 |
my $cleanup_wal_val = $self->{'props'}->{'pg-cleanupwal'} || 'yes'; |
| 1089 |
my $cleanup_wal = string_to_boolean($cleanup_wal_val); |
| 1090 |
if (!defined($cleanup_wal)) { |
| 1091 |
$self->{'die_cb'}->("couldn't interpret PG-CLEANUPWAL value '$cleanup_wal_val' as a boolean"); |
| 1092 |
} |
| 1093 |
|
| 1094 |
$self->{'unlink_cb'} = sub { |
| 1095 |
my $filename = shift @_; |
| 1096 |
debug("unlinking WAL file $filename"); |
| 1097 |
if (unlink($filename) == 0) { |
| 1098 |
debug("Failed to unlink '$filename': $!"); |
| 1099 |
$self->print_to_server("Failed to unlink '$filename': $!", |
| 1100 |
$Amanda::Script_App::ERROR); |
| 1101 |
} |
| 1102 |
}; |
| 1103 |
|
| 1104 |
if ($self->{'args'}->{'level'} > 0) { |
| 1105 |
_incr_backup($self, \*STDOUT); |
| 1106 |
} else { |
| 1107 |
_base_backup($self, \*STDOUT); |
| 1108 |
} |
| 1109 |
} |
| 1110 |
|
| 1111 |
sub command_restore { |
| 1112 |
my $self = shift; |
| 1113 |
|
| 1114 |
chdir(Amanda::Util::get_original_cwd()); |
| 1115 |
if (defined $self->{'args'}->{directory}) { |
| 1116 |
if (!-d $self->{'args'}->{directory}) { |
| 1117 |
$self->print_to_server_and_die("Directory $self->{directory}: $!", |
| 1118 |
$Amanda::Script_App::ERROR); |
| 1119 |
} |
| 1120 |
if (!-w $self->{'args'}->{directory}) { |
| 1121 |
$self->print_to_server_and_die("Directory $self->{directory}: $!", |
| 1122 |
$Amanda::Script_App::ERROR); |
| 1123 |
} |
| 1124 |
chdir($self->{'args'}->{directory}); |
| 1125 |
} |
| 1126 |
my $cur_dir = POSIX::getcwd(); |
| 1127 |
|
| 1128 |
if (!-d $_ARCHIVE_DIR_RESTORE) { |
| 1129 |
mkdir($_ARCHIVE_DIR_RESTORE) or die("could not create archive WAL directory: $!"); |
| 1130 |
} |
| 1131 |
my $status; |
| 1132 |
if ($self->{'args'}->{'level'} > 0) { |
| 1133 |
debug("extracting incremental backup to $cur_dir/$_ARCHIVE_DIR_RESTORE"); |
| 1134 |
$status = system($self->{'args'}->{'gnutar-path'}, |
| 1135 |
'--extract', |
| 1136 |
'--dereference', |
| 1137 |
'--file', '-', |
| 1138 |
'--ignore-zeros', |
| 1139 |
'--exclude', 'empty-incremental', |
| 1140 |
'--directory', $_ARCHIVE_DIR_RESTORE) >> 8; |
| 1141 |
my $uid = (stat $_ARCHIVE_DIR_RESTORE)[4]; |
| 1142 |
if ($uid == 0) { |
| 1143 |
opendir(my $dh, $_ARCHIVE_DIR_RESTORE) || die; |
| 1144 |
while (my $name = readdir $dh) { |
| 1145 |
next if $name eq '.'; |
| 1146 |
next if $name eq '..'; |
| 1147 |
my $uid = (stat "$_ARCHIVE_DIR_RESTORE/$name")[4]; |
| 1148 |
my $gid = (stat "$_ARCHIVE_DIR_RESTORE/$name")[5]; |
| 1149 |
chown $uid, $gid, $_ARCHIVE_DIR_RESTORE; |
| 1150 |
last; |
| 1151 |
} |
| 1152 |
closedir $dh; |
| 1153 |
} |
| 1154 |
(0 == $status) or die("Failed to extract level $self->{'args'}->{'level'} backup (exit status: $status)"); |
| 1155 |
} else { |
| 1156 |
debug("extracting base of full backup to $cur_dir/$_DATA_DIR_RESTORE"); |
| 1157 |
debug("extracting archive dir to $cur_dir/$_ARCHIVE_DIR_RESTORE"); |
| 1158 |
if (!-d $_DATA_DIR_RESTORE) { |
| 1159 |
mkdir($_DATA_DIR_RESTORE) or die("could not create archive WAL directory: $!"); |
| 1160 |
} |
| 1161 |
my @cmd = ($self->{'args'}->{'gnutar-path'}, '--extract', |
| 1162 |
'--dereference', |
| 1163 |
'--file', '-', |
| 1164 |
'--ignore-zero', |
| 1165 |
'--transform', "s,^DATA/,$_DATA_DIR_RESTORE/,S", |
| 1166 |
'--transform', "s,^WAL/,$_ARCHIVE_DIR_RESTORE/,S"); |
| 1167 |
debug("run: " . join ' ',@cmd); |
| 1168 |
$status = system(@cmd) >> 8; |
| 1169 |
(0 == $status) or die("Failed to extract base backup (exit status: $status)"); |
| 1170 |
|
| 1171 |
if (-f $_ARCHIVE_DIR_TAR) { |
| 1172 |
debug("extracting archive dir to $cur_dir/$_ARCHIVE_DIR_RESTORE"); |
| 1173 |
my @cmd = ($self->{'args'}->{'gnutar-path'}, '--extract', |
| 1174 |
'--dereference', |
| 1175 |
'--exclude', 'empty-incremental', |
| 1176 |
'--file', $_ARCHIVE_DIR_TAR, '--directory', |
| 1177 |
$_ARCHIVE_DIR_RESTORE); |
| 1178 |
debug("run: " . join ' ',@cmd); |
| 1179 |
$status = system(@cmd) >> 8; |
| 1180 |
(0 == $status) or die("Failed to extract archived WAL files from base backup (exit status: $status)"); |
| 1181 |
if (unlink($_ARCHIVE_DIR_TAR) == 0) { |
| 1182 |
debug("Failed to unlink '$_ARCHIVE_DIR_TAR': $!"); |
| 1183 |
$self->print_to_server( |
| 1184 |
"Failed to unlink '$_ARCHIVE_DIR_TAR': $!", |
| 1185 |
$Amanda::Script_App::ERROR); |
| 1186 |
} |
| 1187 |
} |
| 1188 |
|
| 1189 |
if (-f $_DATA_DIR_TAR) { |
| 1190 |
debug("extracting data dir to $cur_dir/$_DATA_DIR_RESTORE"); |
| 1191 |
my @cmd = ($self->{'args'}->{'gnutar-path'}, '--extract', |
| 1192 |
'--dereference', |
| 1193 |
'--file', $_DATA_DIR_TAR, |
| 1194 |
'--directory', $_DATA_DIR_RESTORE); |
| 1195 |
debug("run: " . join ' ',@cmd); |
| 1196 |
$status = system(@cmd) >> 8; |
| 1197 |
(0 == $status) or die("Failed to extract data directory from base backup (exit status: $status)"); |
| 1198 |
if (unlink($_DATA_DIR_TAR) == 0) { |
| 1199 |
debug("Failed to unlink '$_DATA_DIR_TAR': $!"); |
| 1200 |
$self->print_to_server("Failed to unlink '$_DATA_DIR_TAR': $!", |
| 1201 |
$Amanda::Script_App::ERROR); |
| 1202 |
} |
| 1203 |
} |
| 1204 |
} |
| 1205 |
} |
| 1206 |
|
| 1207 |
sub command_validate { |
| 1208 |
my $self = shift; |
| 1209 |
|
| 1210 |
# set up to handle errors correctly |
| 1211 |
$self->{'die_cb'} = sub { |
| 1212 |
my ($msg) = @_; |
| 1213 |
debug("$msg"); |
| 1214 |
print "$msg\n"; |
| 1215 |
exit(1); |
| 1216 |
}; |
| 1217 |
|
| 1218 |
if (!defined($self->{'args'}->{'gnutar-path'}) || |
| 1219 |
!-x $self->{'args'}->{'gnutar-path'}) { |
| 1220 |
return $self->default_validate(); |
| 1221 |
} |
| 1222 |
|
| 1223 |
my(@cmd) = ($self->{'args'}->{'gnutar-path'}, "--ignore-zeros", "-tf", "-"); |
| 1224 |
debug("cmd:" . join(" ", @cmd)); |
| 1225 |
my $pid = open3('>&STDIN', '>&STDOUT', '>&STDERR', @cmd) || |
| 1226 |
$self->print_to_server_and_die("Unable to run @cmd", |
| 1227 |
$Amanda::Application::ERROR); |
| 1228 |
waitpid $pid, 0; |
| 1229 |
if ($? != 0){ |
| 1230 |
$self->print_to_server_and_die("$self->{gnutar} returned error", |
| 1231 |
$Amanda::Application::ERROR); |
| 1232 |
} |
| 1233 |
} |
| 1234 |
|
| 1235 |
sub command_index { |
| 1236 |
my $self = shift; |
| 1237 |
my $buffer; |
| 1238 |
|
| 1239 |
print "/PostgreSQL-Database-$self->{'args'}->{'level'}\n"; |
| 1240 |
|
| 1241 |
do { |
| 1242 |
sysread STDIN, $buffer, 1048576; |
| 1243 |
} while (defined $buffer and length($buffer) > 0); |
| 1244 |
} |
| 1245 |
|
| 1246 |
package main; |
| 1247 |
|
| 1248 |
sub usage { |
| 1249 |
print <<EOF; |
| 1250 |
Usage: ampgsql <command> --config=<config> --host=<host> --disk=<disk> --device=<device> --level=<level> --index=<yes|no> --message=<text> --collection=<no> --record=<yes|no> --calcsize. |
| 1251 |
EOF |
| 1252 |
exit(1); |
| 1253 |
} |
| 1254 |
|
| 1255 |
my @orig_argv = @ARGV; |
| 1256 |
|
| 1257 |
my $opts = {}; |
| 1258 |
my $opt_version; |
| 1259 |
|
| 1260 |
GetOptions( |
| 1261 |
$opts, |
| 1262 |
'version' => \$opt_version, |
| 1263 |
'config=s', |
| 1264 |
'host=s', |
| 1265 |
'disk=s', |
| 1266 |
'device=s', |
| 1267 |
'level=s', |
| 1268 |
'index=s', |
| 1269 |
'message=s', |
| 1270 |
'collection=s', |
| 1271 |
'record', |
| 1272 |
'calcsize', |
| 1273 |
'exclude-file=s@', |
| 1274 |
'exclude-list=s@', |
| 1275 |
'include-list=s@', |
| 1276 |
'directory|target=s', |
| 1277 |
# ampgsql-specific |
| 1278 |
'statedir=s', |
| 1279 |
'tmpdir=s', |
| 1280 |
'gnutar-path=s', |
| 1281 |
'cleanupwal=s', |
| 1282 |
'archivedir=s', |
| 1283 |
'db=s', |
| 1284 |
'full-wal=s', |
| 1285 |
'host=s', |
| 1286 |
'max-wal-wait=s', |
| 1287 |
'passfile=s', |
| 1288 |
'port=s', |
| 1289 |
'user=s', |
| 1290 |
'psql-path=s', |
| 1291 |
'verbose=s', |
| 1292 |
'incremental=s', |
| 1293 |
'remove-full-wal=s', |
| 1294 |
'remove-incremental-wal=s' |
| 1295 |
) or usage(); |
| 1296 |
|
| 1297 |
if (defined $opt_version) { |
| 1298 |
print "ampgsql-" . $Amanda::Constants::VERSION , "\n"; |
| 1299 |
exit(0); |
| 1300 |
} |
| 1301 |
|
| 1302 |
my $application = Amanda::Application::ampgsql->new($opts); |
| 1303 |
|
| 1304 |
Amanda::Debug::debug("Arguments: " . join(' ', @orig_argv)); |
| 1305 |
|
| 1306 |
$application->do($ARGV[0]); |
| 1307 |
# NOTREACHED |