Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6bf5c42

Browse files
committed
Make PostgreSQL::Test::Cluster::init_from_backup handle tablespaces.
This commit doesn't use this infrastructure for anything new, although it does adapt 010_pg_basebackup.pl to use it. However, a future commit will use this to improve test coverage for pg_combinebackup. Patch by me, reviewed (but not fully endorsed) by Andres Freund. Discussion: http://postgr.es/m/CA+TgmoYdXTjo9iQeoipTccDpWZzvBNS6EndY2uARM+T4yG_yDg@mail.gmail.com
1 parent 41d2c6f commit 6bf5c42

File tree

2 files changed

+90
-24
lines changed

2 files changed

+90
-24
lines changed

src/bin/pg_basebackup/t/010_pg_basebackup.pl

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -407,25 +407,12 @@
407407

408408
my $node2 = PostgreSQL::Test::Cluster->new('replica');
409409

410-
# Recover main data directory
411-
$node2->init_from_backup($node, 'tarbackup2', tar_program => $tar);
412-
413-
# Recover tablespace into a new directory (not where it was!)
414-
my $repTsDir = "$tempdir/tblspc1replica";
415-
my $realRepTsDir = "$real_sys_tempdir/tblspc1replica";
416-
mkdir $repTsDir;
417-
PostgreSQL::Test::Utils::system_or_bail($tar, 'xf', $tblspc_tars[0],
418-
'-C', $repTsDir);
419-
420-
# Update tablespace map to point to new directory.
421-
# XXX Ideally pg_basebackup would handle this.
410+
# Recover the backup
422411
$tblspc_tars[0] =~ m|/([0-9]*)\.tar$|;
423412
my $tblspcoid = $1;
424-
my $escapedRepTsDir = $realRepTsDir;
425-
$escapedRepTsDir =~ s/\\/\\\\/g;
426-
open my $mapfile, '>', $node2->data_dir . '/tablespace_map' or die $!;
427-
print $mapfile "$tblspcoid $escapedRepTsDir\n";
428-
close $mapfile;
413+
my $realRepTsDir = "$real_sys_tempdir/tblspc1replica";
414+
$node2->init_from_backup($node, 'tarbackup2', tar_program => $tar,
415+
'tablespace_map' => { $tblspcoid => $realRepTsDir });
429416

430417
$node2->start;
431418
my $result = $node2->safe_psql('postgres', 'SELECT * FROM test1');

src/test/perl/PostgreSQL/Test/Cluster.pm

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ sub backup_fs_cold
777777

778778
=pod
779779
780-
=item $node->init_from_backup(root_node, backup_name)
780+
=item $node->init_from_backup(root_node, backup_name, %params)
781781
782782
Initialize a node from a backup, which may come from this node or a different
783783
node. root_node must be a PostgreSQL::Test::Cluster reference, backup_name the string name
@@ -787,8 +787,13 @@ Does not start the node after initializing it.
787787
788788
By default, the backup is assumed to be plain format. To restore from
789789
a tar-format backup, pass the name of the tar program to use in the
790-
keyword parameter tar_program. Note that tablespace tar files aren't
791-
handled here.
790+
keyword parameter tar_program.
791+
792+
If there are tablespace present in the backup, include tablespace_map as
793+
a keyword parameter whose values is a hash. When tar_program is used, the
794+
hash keys are tablespace OIDs; otherwise, they are the tablespace pathnames
795+
used in the backup. In either case, the values are the tablespace pathnames
796+
that should be used for the target cluster.
792797
793798
To restore from an incremental backup, pass the parameter combine_with_prior
794799
as a reference to an array of prior backup names with which this backup
@@ -843,24 +848,98 @@ sub init_from_backup
843848
}
844849

845850
local %ENV = $self->_get_env();
846-
PostgreSQL::Test::Utils::system_or_bail('pg_combinebackup', '-d',
847-
@prior_backup_path, $backup_path, '-o', $data_path);
851+
my @combineargs = ('pg_combinebackup', '-d');
852+
if (exists $params{tablespace_map})
853+
{
854+
while (my ($olddir, $newdir) = each %{$params{tablespace_map}})
855+
{
856+
push @combineargs, "-T$olddir=$newdir";
857+
}
858+
}
859+
push @combineargs, @prior_backup_path, $backup_path, '-o', $data_path;
860+
PostgreSQL::Test::Utils::system_or_bail(@combineargs);
848861
}
849862
elsif (defined $params{tar_program})
850863
{
851-
mkdir($data_path);
864+
mkdir($data_path) || die "mkdir $data_path: $!";
852865
PostgreSQL::Test::Utils::system_or_bail($params{tar_program}, 'xf',
853866
$backup_path . '/base.tar',
854867
'-C', $data_path);
855868
PostgreSQL::Test::Utils::system_or_bail(
856869
$params{tar_program}, 'xf',
857870
$backup_path . '/pg_wal.tar', '-C',
858871
$data_path . '/pg_wal');
872+
873+
# We need to generate a tablespace_map file.
874+
open(my $tsmap, ">", "$data_path/tablespace_map")
875+
|| die "$data_path/tablespace_map: $!";
876+
877+
# Extract tarfiles and add tablespace_map entries
878+
my @tstars = grep { /^\d+.tar/ }
879+
PostgreSQL::Test::Utils::slurp_dir($backup_path);
880+
for my $tstar (@tstars)
881+
{
882+
my $tsoid = $tstar;
883+
$tsoid =~ s/\.tar$//;
884+
885+
die "no tablespace mapping for $tstar"
886+
if !exists $params{tablespace_map} ||
887+
!exists $params{tablespace_map}{$tsoid};
888+
my $newdir = $params{tablespace_map}{$tsoid};
889+
890+
mkdir($newdir) || die "mkdir $newdir: $!";
891+
PostgreSQL::Test::Utils::system_or_bail($params{tar_program}, 'xf',
892+
$backup_path . '/' . $tstar, '-C', $newdir);
893+
894+
my $escaped_newdir = $newdir;
895+
$escaped_newdir =~ s/\\/\\\\/g;
896+
print $tsmap "$tsoid $escaped_newdir\n";
897+
}
898+
899+
# Close tablespace_map.
900+
close($tsmap);
859901
}
860902
else
861903
{
904+
my @tsoids;
862905
rmdir($data_path);
863-
PostgreSQL::Test::RecursiveCopy::copypath($backup_path, $data_path);
906+
907+
# Copy the main backup. Exclude tablespace links, but remember them.
908+
PostgreSQL::Test::RecursiveCopy::copypath($backup_path, $data_path,
909+
'filterfn' => sub {
910+
my ($path) = @_;
911+
if ($path =~ /^pg_tblspc\/(\d+)$/ && -l "$backup_path/$path")
912+
{
913+
push @tsoids, $1;
914+
return 0;
915+
}
916+
return 1;
917+
});
918+
919+
# We need to generate a tablespace_map file.
920+
open(my $tsmap, ">", "$data_path/tablespace_map")
921+
|| die "$data_path/tablespace_map: $!";
922+
923+
# Now use the list of tablespace links to copy each tablespace.
924+
for my $tsoid (@tsoids)
925+
{
926+
my $olddir = readlink("$backup_path/pg_tblspc/$tsoid")
927+
|| die "readlink $backup_path/pg_tblspc/$tsoid: $!";
928+
929+
die "no tablespace mapping for $olddir"
930+
if !exists $params{tablespace_map} ||
931+
!exists $params{tablespace_map}{$olddir};
932+
933+
my $newdir = $params{tablespace_map}{$olddir};
934+
PostgreSQL::Test::RecursiveCopy::copypath($olddir, $newdir);
935+
936+
my $escaped_newdir = $newdir;
937+
$escaped_newdir =~ s/\\/\\\\/g;
938+
print $tsmap "$tsoid $escaped_newdir\n";
939+
}
940+
941+
# Close tablespace_map.
942+
close($tsmap);
864943
}
865944
chmod(0700, $data_path) or die $!;
866945

0 commit comments

Comments
 (0)