| 1 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 2 |
# |
| 3 |
# This test aims to validate that an incremental backup can be combined |
| 4 |
# with a valid prior backup and that it cannot be combined with an invalid |
| 5 |
# prior backup. |
| 6 |
|
| 7 |
use strict; |
| 8 |
use warnings FATAL => 'all'; |
| 9 |
use File::Compare; |
| 10 |
use File::Path qw(rmtree); |
| 11 |
use File::Copy; |
| 12 |
use PostgreSQL::Test::Cluster; |
| 13 |
use PostgreSQL::Test::Utils; |
| 14 |
use Test::More; |
| 15 |
|
| 16 |
# Can be changed to test the other modes. |
| 17 |
my $mode = $ENV{PG_TEST_PG_COMBINEBACKUP_MODE} || '--copy'; |
| 18 |
|
| 19 |
note "testing using mode $mode"; |
| 20 |
|
| 21 |
# Set up a new database instance. |
| 22 |
my $node1 = PostgreSQL::Test::Cluster->new('node1'); |
| 23 |
$node1->init(has_archiving => 1, allows_streaming => 1); |
| 24 |
$node1->append_conf('postgresql.conf', 'summarize_wal = on'); |
| 25 |
$node1->start; |
| 26 |
|
| 27 |
# Create a file called INCREMENTAL.config in the root directory of the |
| 28 |
# first database instance. We only recognize INCREMENTAL.${original_name} |
| 29 |
# files under base and global and in tablespace directories, so this shouldn't |
| 30 |
# cause anything to fail. |
| 31 |
my $strangely_named_config_file = $node1->data_dir . '/INCREMENTAL.config'; |
| 32 |
open(my $icfg, '>', $strangely_named_config_file) |
| 33 |
|| die "$strangely_named_config_file: $!"; |
| 34 |
close($icfg); |
| 35 |
|
| 36 |
# Set up another new database instance. force_initdb is used because |
| 37 |
# we want it to be a separate cluster with a different system ID. |
| 38 |
my $node2 = PostgreSQL::Test::Cluster->new('node2'); |
| 39 |
$node2->init(force_initdb => 1, has_archiving => 1, allows_streaming => 1); |
| 40 |
$node2->append_conf('postgresql.conf', 'summarize_wal = on'); |
| 41 |
$node2->start; |
| 42 |
|
| 43 |
# Take a full backup from node1. |
| 44 |
my $backup1path = $node1->backup_dir . '/backup1'; |
| 45 |
$node1->command_ok( |
| 46 |
[ |
| 47 |
'pg_basebackup', |
| 48 |
'--pgdata' => $backup1path, |
| 49 |
'--no-sync', |
| 50 |
'--checkpoint' => 'fast', |
| 51 |
], |
| 52 |
"full backup from node1"); |
| 53 |
|
| 54 |
# Now take an incremental backup. |
| 55 |
my $backup2path = $node1->backup_dir . '/backup2'; |
| 56 |
$node1->command_ok( |
| 57 |
[ |
| 58 |
'pg_basebackup', |
| 59 |
'--pgdata' => $backup2path, |
| 60 |
'--no-sync', |
| 61 |
'--checkpoint' => 'fast', |
| 62 |
'--incremental' => $backup1path . '/backup_manifest', |
| 63 |
], |
| 64 |
"incremental backup from node1"); |
| 65 |
|
| 66 |
# Now take another incremental backup. |
| 67 |
my $backup3path = $node1->backup_dir . '/backup3'; |
| 68 |
$node1->command_ok( |
| 69 |
[ |
| 70 |
'pg_basebackup', |
| 71 |
'--pgdata' => $backup3path, |
| 72 |
'--no-sync', |
| 73 |
'--checkpoint' => 'fast', |
| 74 |
'--incremental' => $backup2path . '/backup_manifest', |
| 75 |
], |
| 76 |
"another incremental backup from node1"); |
| 77 |
|
| 78 |
# Take a full backup from node2. |
| 79 |
my $backupother1path = $node1->backup_dir . '/backupother1'; |
| 80 |
$node2->command_ok( |
| 81 |
[ |
| 82 |
'pg_basebackup', |
| 83 |
'--pgdata' => $backupother1path, |
| 84 |
'--no-sync', |
| 85 |
'--checkpoint' => 'fast', |
| 86 |
], |
| 87 |
"full backup from node2"); |
| 88 |
|
| 89 |
# Take an incremental backup from node2. |
| 90 |
my $backupother2path = $node1->backup_dir . '/backupother2'; |
| 91 |
$node2->command_ok( |
| 92 |
[ |
| 93 |
'pg_basebackup', |
| 94 |
'--pgdata' => $backupother2path, |
| 95 |
'--no-sync', |
| 96 |
'--checkpoint' => 'fast', |
| 97 |
'--incremental' => $backupother1path . '/backup_manifest', |
| 98 |
], |
| 99 |
"incremental backup from node2"); |
| 100 |
|
| 101 |
# Result directory. |
| 102 |
my $resultpath = $node1->backup_dir . '/result'; |
| 103 |
|
| 104 |
# Can't combine 2 full backups. |
| 105 |
$node1->command_fails_like( |
| 106 |
[ |
| 107 |
'pg_combinebackup', $backup1path, $backup1path, |
| 108 |
'--output' => $resultpath, |
| 109 |
$mode, |
| 110 |
], |
| 111 |
qr/is a full backup, but only the first backup should be a full backup/, |
| 112 |
"can't combine full backups"); |
| 113 |
|
| 114 |
# Can't combine 2 incremental backups. |
| 115 |
$node1->command_fails_like( |
| 116 |
[ |
| 117 |
'pg_combinebackup', $backup2path, $backup2path, |
| 118 |
'--output' => $resultpath, |
| 119 |
$mode, |
| 120 |
], |
| 121 |
qr/is an incremental backup, but the first backup should be a full backup/, |
| 122 |
"can't combine full backups"); |
| 123 |
|
| 124 |
# Can't combine full backup with an incremental backup from a different system. |
| 125 |
$node1->command_fails_like( |
| 126 |
[ |
| 127 |
'pg_combinebackup', $backup1path, $backupother2path, |
| 128 |
'--output' => $resultpath, |
| 129 |
$mode, |
| 130 |
], |
| 131 |
qr/expected system identifier.*but found/, |
| 132 |
"can't combine backups from different nodes"); |
| 133 |
|
| 134 |
# Can't combine when different manifest system identifier |
| 135 |
rename("$backup2path/backup_manifest", "$backup2path/backup_manifest.orig") |
| 136 |
or die "could not move $backup2path/backup_manifest"; |
| 137 |
copy("$backupother2path/backup_manifest", "$backup2path/backup_manifest") |
| 138 |
or die "could not copy $backupother2path/backup_manifest"; |
| 139 |
$node1->command_fails_like( |
| 140 |
[ |
| 141 |
'pg_combinebackup', $backup1path, $backup2path, $backup3path, |
| 142 |
'--output' => $resultpath, |
| 143 |
$mode, |
| 144 |
], |
| 145 |
qr/ manifest system identifier is .*, but control file has /, |
| 146 |
"can't combine backups with different manifest system identifier "); |
| 147 |
# Restore the backup state |
| 148 |
move("$backup2path/backup_manifest.orig", "$backup2path/backup_manifest") |
| 149 |
or die "could not move $backup2path/backup_manifest"; |
| 150 |
|
| 151 |
# Can't omit a required backup. |
| 152 |
$node1->command_fails_like( |
| 153 |
[ |
| 154 |
'pg_combinebackup', $backup1path, $backup3path, |
| 155 |
'--output' => $resultpath, |
| 156 |
$mode, |
| 157 |
], |
| 158 |
qr/starts at LSN.*but expected/, |
| 159 |
"can't omit a required backup"); |
| 160 |
|
| 161 |
# Can't combine backups in the wrong order. |
| 162 |
$node1->command_fails_like( |
| 163 |
[ |
| 164 |
'pg_combinebackup', $backup1path, $backup3path, $backup2path, |
| 165 |
'--output' => $resultpath, |
| 166 |
$mode, |
| 167 |
], |
| 168 |
qr/starts at LSN.*but expected/, |
| 169 |
"can't combine backups in the wrong order"); |
| 170 |
|
| 171 |
# Can combine 3 backups that match up properly. |
| 172 |
$node1->command_ok( |
| 173 |
[ |
| 174 |
'pg_combinebackup', $backup1path, $backup2path, $backup3path, |
| 175 |
'--output' => $resultpath, |
| 176 |
$mode, |
| 177 |
], |
| 178 |
"can combine 3 matching backups"); |
| 179 |
rmtree($resultpath); |
| 180 |
|
| 181 |
# Can combine full backup with first incremental. |
| 182 |
my $synthetic12path = $node1->backup_dir . '/synthetic12'; |
| 183 |
$node1->command_ok( |
| 184 |
[ |
| 185 |
'pg_combinebackup', $backup1path, $backup2path, |
| 186 |
'--output' => $synthetic12path, |
| 187 |
$mode, |
| 188 |
], |
| 189 |
"can combine 2 matching backups"); |
| 190 |
|
| 191 |
# Can combine result of previous step with second incremental. |
| 192 |
$node1->command_ok( |
| 193 |
[ |
| 194 |
'pg_combinebackup', $synthetic12path, $backup3path, |
| 195 |
'--output' => $resultpath, |
| 196 |
$mode, |
| 197 |
], |
| 198 |
"can combine synthetic backup with later incremental"); |
| 199 |
rmtree($resultpath); |
| 200 |
|
| 201 |
# Can't combine result of 1+2 with 2. |
| 202 |
$node1->command_fails_like( |
| 203 |
[ |
| 204 |
'pg_combinebackup', $synthetic12path, $backup2path, |
| 205 |
'--output' => $resultpath, |
| 206 |
$mode, |
| 207 |
], |
| 208 |
qr/starts at LSN.*but expected/, |
| 209 |
"can't combine synthetic backup with included incremental"); |
| 210 |
|
| 211 |
# OK, that's all. |
| 212 |
done_testing(); |