| 1 |
#!/usr/bin/perl -wT |
| 2 |
|
| 3 |
# pg_restorecluster: restore from a pg_backupcluster backup |
| 4 |
# |
| 5 |
# Copyright (C) 2021-2024 Christoph Berg <[email protected]> |
| 6 |
# |
| 7 |
# This program is free software; you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 2 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
|
| 17 |
use strict; |
| 18 |
use warnings; |
| 19 |
use Cwd 'abs_path'; |
| 20 |
use Getopt::Long; |
| 21 |
use PgCommon; |
| 22 |
|
| 23 |
my ($version, $cluster); |
| 24 |
|
| 25 |
# untaint environment |
| 26 |
$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin'; |
| 27 |
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; |
| 28 |
umask 022; |
| 29 |
|
| 30 |
sub help () { |
| 31 |
print "Syntax: $0 [options] <version> <cluster> <backup> |
| 32 |
Options: |
| 33 |
-d --datadir DIR Data directory for restored cluster (default per createcluster.conf) |
| 34 |
-p --port PORT Use port PORT for restored cluster (default is next free port) |
| 35 |
-s --start Start cluster after restoring (default for restore from dump) |
| 36 |
--archive Configure recovery from WAL archive |
| 37 |
--pitr TIMEST Configure point-in-time recovery to TIMESTAMP from WAL archive |
| 38 |
--wal-archive DIR Read WAL from archive DIR (default <backup>/../wal) |
| 39 |
"; |
| 40 |
} |
| 41 |
|
| 42 |
my ($datadir, $port, $start, $recovery_target_time, $archive_recovery, $wal_archive, $createclusterconf); |
| 43 |
|
| 44 |
exit 1 unless GetOptions ( |
| 45 |
'd|datadir=s' => \$datadir, |
| 46 |
'p|port=s' => \$port, |
| 47 |
's|start' => \$start, |
| 48 |
'archive' => \$archive_recovery, |
| 49 |
'pitr=s' => \$recovery_target_time, |
| 50 |
'recovery-target-time=s' => \$recovery_target_time, |
| 51 |
'wal-archive=s' => \$wal_archive, |
| 52 |
'createclusterconf=s' => \$createclusterconf, |
| 53 |
); |
| 54 |
if ($recovery_target_time) { |
| 55 |
($recovery_target_time) = $recovery_target_time =~ /(.*)/; # untaint |
| 56 |
} |
| 57 |
|
| 58 |
# accept both "version cluster" and "version[-/]cluster" |
| 59 |
if (@ARGV >= 2 and $ARGV[0] =~ m!^(\d+\.?\d)[-/]([^/]+)$!) { |
| 60 |
($version, $cluster) = ($1, $2); |
| 61 |
shift @ARGV; |
| 62 |
} elsif (@ARGV >= 3 and $ARGV[0] =~ /^(\d+\.?\d)$/) { |
| 63 |
$version = $1; |
| 64 |
($cluster) = ($ARGV[1]) =~ m!^([^/]+)$!; |
| 65 |
shift @ARGV; |
| 66 |
shift @ARGV; |
| 67 |
} else { |
| 68 |
help(); |
| 69 |
exit 1; |
| 70 |
} |
| 71 |
|
| 72 |
error "cluster $version $cluster already exists" if cluster_exists $version, $cluster; |
| 73 |
|
| 74 |
my %defaultconf = PgCommon::read_conf_file ($createclusterconf // "$PgCommon::common_confdir/createcluster.conf", not $createclusterconf); |
| 75 |
|
| 76 |
# functions to be run as root |
| 77 |
|
| 78 |
sub create_cluster_directories($$$$) { |
| 79 |
my ($owneruid, $ownergid, $configdir, $datadir) = @_; |
| 80 |
my @postgres_user = getpwnam 'postgres'; |
| 81 |
my ($pg_uid, $pg_gid) = (getpwnam 'postgres')[2, 3]; |
| 82 |
|
| 83 |
for my $pgdir ("/etc/postgresql", "/etc/postgresql/$version", "/var/lib/postgresql", "/var/lib/postgresql/$version") { |
| 84 |
if (! -e $pgdir) { |
| 85 |
mkdir $pgdir or error "mkdir $pgdir: $!"; |
| 86 |
chown $pg_uid, $pg_gid, $pgdir or error "chown $pgdir: $!"; |
| 87 |
} |
| 88 |
} |
| 89 |
mkdir $configdir or error "mkdir $configdir: $!"; |
| 90 |
chown $owneruid, $ownergid, $configdir or error "chown $configdir: $!"; |
| 91 |
mkdir $datadir, 0700 or error "mkdir $datadir: $!"; |
| 92 |
chown $owneruid, $ownergid, $datadir or error "chown $datadir: $!"; |
| 93 |
} |
| 94 |
|
| 95 |
sub create_cluster($$$$$) { |
| 96 |
my ($backup, $owneruid, $ownergid, $configdir, $datadir) = @_; |
| 97 |
|
| 98 |
my @createclusteropts = (); |
| 99 |
if (-f "$backup/createcluster.opts") { |
| 100 |
open my $fh, "$backup/createcluster.opts" or error "$backup/createcluster.opts: $!"; |
| 101 |
local $/; # slurp mode |
| 102 |
my ($opts) = <$fh> =~ /(.*)/; # untaint |
| 103 |
@createclusteropts = split /\s+/, $opts; |
| 104 |
close $fh; |
| 105 |
} |
| 106 |
|
| 107 |
system_or_error "pg_createcluster", |
| 108 |
"--datadir", $datadir, |
| 109 |
"--user", $owneruid, "--group", $ownergid, |
| 110 |
$version, $cluster, |
| 111 |
"--", |
| 112 |
@createclusteropts; |
| 113 |
} |
| 114 |
|
| 115 |
sub start_cluster() { |
| 116 |
print "Starting cluster $version $cluster ...\n"; |
| 117 |
system_or_error "pg_ctlcluster", $version, $cluster, "start"; |
| 118 |
} |
| 119 |
|
| 120 |
sub switch_to_cluster_owner($$) { |
| 121 |
my ($owneruid, $ownergid) = @_; |
| 122 |
change_ugid $owneruid, $ownergid; |
| 123 |
} |
| 124 |
|
| 125 |
# restore functions |
| 126 |
|
| 127 |
sub unpack_tar($$$) { |
| 128 |
my ($backup, $tar, $dir) = @_; |
| 129 |
|
| 130 |
if (-f "$backup/$tar.gz") { |
| 131 |
$tar = "$tar.gz"; |
| 132 |
} elsif (-f "$backup/$tar.bz2") { |
| 133 |
$tar = "$tar.bz2"; |
| 134 |
} elsif (-f "$backup/$tar.xz") { |
| 135 |
$tar = "$tar.xz"; |
| 136 |
} elsif (-f "$backup/$tar") { |
| 137 |
# do nothing |
| 138 |
} else { |
| 139 |
error "$backup/config.tar* is missing"; |
| 140 |
} |
| 141 |
|
| 142 |
print "Restoring $backup/$tar to $dir ...\n"; |
| 143 |
system_or_error "tar", "-C", $dir, "-xf", "$backup/$tar"; |
| 144 |
} |
| 145 |
|
| 146 |
sub restore_config($$) { |
| 147 |
my ($backup, $configdir) = @_; |
| 148 |
unpack_tar($backup, "config.tar", $configdir); |
| 149 |
} |
| 150 |
|
| 151 |
sub update_config($$$) { |
| 152 |
my ($configdir, $datadir, $port) = @_; |
| 153 |
my %settings = ( |
| 154 |
data_directory => $datadir, |
| 155 |
hba_file => "$configdir/pg_hba.conf", |
| 156 |
ident_file => "$configdir/pg_ident.conf", |
| 157 |
external_pid_file => "/var/run/postgresql/$version-$cluster.pid", |
| 158 |
port => $port, |
| 159 |
); |
| 160 |
$settings{cluster_name} = "$version/$cluster" if ($version >= 9.5); |
| 161 |
$settings{stats_temp_directory} = "/var/run/postgresql/$version-$cluster.pg_stat_tmp" if ($version < 15); |
| 162 |
my %config = PgCommon::read_conf_file("$configdir/postgresql.conf", 0); |
| 163 |
for my $guc (sort keys %settings) { |
| 164 |
if (not exists $config{$guc} or $config{$guc} ne $settings{$guc}) { |
| 165 |
print "Setting $guc = $settings{$guc}\n"; |
| 166 |
PgCommon::set_conffile_value("$configdir/postgresql.conf", $guc, $settings{$guc}); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
sub restore_basebackup($$) { |
| 172 |
my ($backup, $datadir) = @_; |
| 173 |
unpack_tar($backup, "base.tar", $datadir); |
| 174 |
} |
| 175 |
|
| 176 |
sub restore_wal($$) { |
| 177 |
my ($backup, $datadir) = @_; |
| 178 |
return if ($version < 10); # WAL contained in base.tar.gz in PG 9.x |
| 179 |
unpack_tar($backup, "pg_wal.tar", "$datadir/pg_wal"); |
| 180 |
} |
| 181 |
|
| 182 |
sub archive_recovery_options($$$) { |
| 183 |
my ($backup, $datadir, $wal_archive) = @_; |
| 184 |
|
| 185 |
$wal_archive = abs_path($wal_archive) or error "$wal_archive: $!"; |
| 186 |
-d $wal_archive or error "$wal_archive is not a directory"; |
| 187 |
|
| 188 |
print "Setting archive recovery options"; |
| 189 |
my $recovery_options = "restore_command = '/usr/share/postgresql-common/pg_getwal $wal_archive/%f %p'\n"; |
| 190 |
if ($recovery_target_time) { |
| 191 |
$recovery_options .= "recovery_target_time = '$recovery_target_time'\n"; |
| 192 |
$recovery_options .= "recovery_target_action = 'promote'\n"; |
| 193 |
} |
| 194 |
|
| 195 |
if ($version >= 12) { |
| 196 |
my $autoconf = "$datadir/postgresql.auto.conf"; |
| 197 |
open my $fh, ">>", $autoconf or error "$autoconf: $!"; |
| 198 |
print $fh $recovery_options or error "$autoconf: $!"; |
| 199 |
close $fh or error "$autoconf: $!"; |
| 200 |
|
| 201 |
my $recoverysignal = "$datadir/recovery.signal"; |
| 202 |
open my $fh2, ">", $recoverysignal or error "$recoverysignal: $!"; |
| 203 |
close $fh2 or error "$recoverysignal: $!"; |
| 204 |
|
| 205 |
} else { |
| 206 |
my $recoveryconf = "$datadir/recovery.conf"; |
| 207 |
open my $fh, ">>", $recoveryconf or error "$recoveryconf: $!"; |
| 208 |
print $fh $recovery_options or error "$recoveryconf: $!"; |
| 209 |
close $fh or error "$recoveryconf: $!"; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
sub reset_archive_recovery_options() { |
| 214 |
if ($version >= 12) { |
| 215 |
system_or_error "psql", |
| 216 |
"--cluster", "$version/$cluster", |
| 217 |
"-XAtqc", "ALTER SYSTEM RESET restore_command"; |
| 218 |
system_or_error "psql", |
| 219 |
"--cluster", "$version/$cluster", |
| 220 |
"-XAtqc", "ALTER SYSTEM RESET recovery_target_time" |
| 221 |
if ($recovery_target_time); |
| 222 |
system_or_error "psql", |
| 223 |
"--cluster", "$version/$cluster", |
| 224 |
"-XAtqc", "ALTER SYSTEM RESET recovery_target_action" |
| 225 |
if ($recovery_target_time); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
sub restore_globals($$) { |
| 230 |
my ($backup, $owneruid) = @_; |
| 231 |
my $owner = (getpwuid $owneruid)[0] or error "UID $owneruid has no name"; |
| 232 |
|
| 233 |
print "Restoring $backup/globals.sql ...\n"; |
| 234 |
open my $globals, "$backup/globals.sql" or error "$backup/globals.sql: $!"; |
| 235 |
open my $psql, "|-", "psql", "--cluster", "$version/$cluster", "-vON_ERROR_STOP=1", "-Xq" or error "psql: $!"; |
| 236 |
while (my $line = <$globals>) { |
| 237 |
next if ($line eq "CREATE ROLE $owner;\n"); |
| 238 |
print $psql $line or error "psql: $!"; |
| 239 |
} |
| 240 |
close $globals; |
| 241 |
close $psql; |
| 242 |
error "psql failed" if ($?); |
| 243 |
} |
| 244 |
|
| 245 |
sub restore_dumps($) { |
| 246 |
my ($backup) = @_; |
| 247 |
|
| 248 |
for my $dump (sort glob "$backup/*.dump") { |
| 249 |
$dump =~ m!(.*/([^/]*).dump)$!; |
| 250 |
$dump = $1; # untaint |
| 251 |
my $db = $2; |
| 252 |
print "Restoring $dump to database $db ...\n"; |
| 253 |
my @create = ($db eq "postgres" or $db eq "template1") ? () : ("--create"); |
| 254 |
system_or_error "pg_restore", "--cluster", "$version/$cluster", @create, "-d", "postgres", $dump; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
sub wait_for_recovery() { |
| 259 |
my $sleep = 1; |
| 260 |
|
| 261 |
print "Waiting for end of recovery ...\n"; |
| 262 |
while (1) { |
| 263 |
open my $psql, "-|", "psql", "--cluster", "$version/$cluster", "-XAtc", "SELECT pg_is_in_recovery()" or error "psql: $!"; |
| 264 |
my $status = <$psql>; |
| 265 |
error "psql: $!" unless (defined $status); |
| 266 |
close $psql or error "psql: $!"; |
| 267 |
last if ($status eq "f\n"); |
| 268 |
sleep($sleep++); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
sub analyze() { |
| 273 |
system_or_error "vacuumdb", |
| 274 |
"--cluster", "$version/$cluster", |
| 275 |
"--analyze-only", |
| 276 |
($version >= 9.4 ? "--analyze-in-stages" : ()), |
| 277 |
"--all"; |
| 278 |
} |
| 279 |
|
| 280 |
sub lscluster() { |
| 281 |
system_or_error "pg_lsclusters", $version, $cluster; |
| 282 |
} |
| 283 |
|
| 284 |
# main |
| 285 |
|
| 286 |
my ($backup) = $ARGV[0] =~ /(.*)/; # untaint |
| 287 |
error "$backup is not a directory" unless (-d $backup); |
| 288 |
$backup =~ s/\/$//; # strip trailing slash |
| 289 |
my ($owneruid, $ownergid) = (stat $backup)[4, 5]; |
| 290 |
my $configdir = "/etc/postgresql/$version/$cluster"; |
| 291 |
$datadir //= replace_v_c($defaultconf{data_directory} // "/var/lib/postgresql/%v/%c", $version, $cluster); |
| 292 |
($datadir) = $datadir =~ /(.*)/; # untaint |
| 293 |
$wal_archive //= "$backup/../wal"; |
| 294 |
$port //= next_free_port(); |
| 295 |
|
| 296 |
if ($backup =~ /\.backup$/) { |
| 297 |
create_cluster_directories($owneruid, $ownergid, $configdir, $datadir); |
| 298 |
if (fork == 0) { |
| 299 |
switch_to_cluster_owner($owneruid, $ownergid); |
| 300 |
restore_config($backup, $configdir); |
| 301 |
update_config($configdir, $datadir, $port); |
| 302 |
restore_basebackup($backup, $datadir); |
| 303 |
restore_wal($backup, $datadir); |
| 304 |
archive_recovery_options($backup, $datadir, $wal_archive) |
| 305 |
if ($archive_recovery or $recovery_target_time); |
| 306 |
exit(0); |
| 307 |
} |
| 308 |
wait; |
| 309 |
exit(1) if ($?); |
| 310 |
if ($start) { |
| 311 |
print "\n"; |
| 312 |
start_cluster(); |
| 313 |
switch_to_cluster_owner($owneruid, $ownergid); |
| 314 |
wait_for_recovery(); |
| 315 |
reset_archive_recovery_options() if ($archive_recovery or $recovery_target_time); |
| 316 |
analyze(); |
| 317 |
} |
| 318 |
print "\n"; |
| 319 |
lscluster(); |
| 320 |
|
| 321 |
} elsif ($backup =~ /\.dump$/) { |
| 322 |
create_cluster($backup, $owneruid, $ownergid, $configdir, $datadir); |
| 323 |
print "\n"; |
| 324 |
if (fork == 0) { |
| 325 |
switch_to_cluster_owner($owneruid, $ownergid); |
| 326 |
restore_config($backup, $configdir); |
| 327 |
update_config($configdir, $datadir, $port); |
| 328 |
exit(0); |
| 329 |
} |
| 330 |
wait; |
| 331 |
exit(1) if ($?); |
| 332 |
start_cluster(); |
| 333 |
switch_to_cluster_owner($owneruid, $ownergid); |
| 334 |
restore_globals($backup, $owneruid); |
| 335 |
restore_dumps($backup); |
| 336 |
analyze(); |
| 337 |
print "\n"; |
| 338 |
lscluster(); |
| 339 |
|
| 340 |
} else { |
| 341 |
error "$backup must end in either .backup or .dump"; |
| 342 |
} |
| 343 |
|
| 344 |
__END__ |
| 345 |
|
| 346 |
=head1 NAME |
| 347 |
|
| 348 |
pg_restorecluster - Restore from a pg_backupcluster backup |
| 349 |
|
| 350 |
=head1 SYNOPSIS |
| 351 |
|
| 352 |
B<pg_restorecluster> [I<options>] I<version> I<cluster> I<backup> |
| 353 |
|
| 354 |
=head1 DESCRIPTION |
| 355 |
|
| 356 |
B<pg_restorecluster> restores a PostgreSQL cluster from a backup created by |
| 357 |
B<pg_backupcluster>. The cluster will be newly created in the system using the |
| 358 |
name provided on the command line; this allows renaming a cluster on restore. |
| 359 |
The restored cluster configuration will be updated to reflect the new name and |
| 360 |
location. |
| 361 |
|
| 362 |
The I<backup> name passed must end in either B<.basebackup> or B<.dump>; |
| 363 |
usually this will be the full path to a backup directory in |
| 364 |
C</var/backups/postgresql/version-cluster/> as reported by |
| 365 |
B<pg_backupcluster ... list>. |
| 366 |
|
| 367 |
Basebackups are restored as-is. For dumps, B<pg_createcluster> is used to |
| 368 |
create a new cluster, and schema and data are restored via B<pg_restore>. |
| 369 |
|
| 370 |
=head1 OPTIONS |
| 371 |
|
| 372 |
=over 4 |
| 373 |
|
| 374 |
=item B<-d --datadir> I<DIR> |
| 375 |
|
| 376 |
Use I<DIR> as data directory for the restored cluster (default per |
| 377 |
createcluster.conf, by default /var/lib/postgresql/I<version>/I<cluster>). |
| 378 |
|
| 379 |
=item B<-p --port> I<N> |
| 380 |
|
| 381 |
Use port I<N> for restored cluster (default is next free port). |
| 382 |
|
| 383 |
=item B<-s --start> |
| 384 |
|
| 385 |
Start cluster after restoring (default for restore from dump; off for |
| 386 |
basebackup restores). |
| 387 |
|
| 388 |
After the cluster has been started, B<ANALYZE> is run on all databases. |
| 389 |
|
| 390 |
=item B<--archive> |
| 391 |
|
| 392 |
Configure cluster for recovery from WAL archive. This sets B<restore_command> |
| 393 |
to retrieve WAL files from I<backup>B</../wal>. |
| 394 |
|
| 395 |
=item B<--pitr> I<TIMESTAMP> |
| 396 |
|
| 397 |
=item B<--recovery-target-time> I<TIMESTAMP> |
| 398 |
|
| 399 |
Additionally to setting B<restore_command>, set B<recovery_target_time> to |
| 400 |
I<TIMESTAMP> for point-in-time recovery. Also sets |
| 401 |
B<recovery_target_action='promote'>. |
| 402 |
|
| 403 |
=item B<--wal-archive> I<DIR> |
| 404 |
|
| 405 |
For archive recovery, read WAL from archive I<DIR> (default is |
| 406 |
I<backup>B</../wal>). |
| 407 |
|
| 408 |
=back |
| 409 |
|
| 410 |
=head1 FILES |
| 411 |
|
| 412 |
=over 4 |
| 413 |
|
| 414 |
=item /var/backups |
| 415 |
|
| 416 |
Default root directory for cluster backup directories. |
| 417 |
|
| 418 |
=back |
| 419 |
|
| 420 |
See L<pg_backupcluster(1)> for a description of files. |
| 421 |
|
| 422 |
=head1 SEE ALSO |
| 423 |
|
| 424 |
L<pg_backupcluster(1)>, L<pg_restore(1)>, L<vacuumdb(1)>. |
| 425 |
|
| 426 |
=head1 AUTHOR |
| 427 |
|
| 428 |
Christoph Berg L<E<lt>[email protected]<gt>> |