Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pg_combinebackup.c
4 : : * Combine incremental backups with prior backups.
5 : : *
6 : : * Copyright (c) 2017-2025, PostgreSQL Global Development Group
7 : : *
8 : : * IDENTIFICATION
9 : : * src/bin/pg_combinebackup/pg_combinebackup.c
10 : : *
11 : : *-------------------------------------------------------------------------
12 : : */
13 : : #include "postgres_fe.h"
14 : :
15 : : #include <dirent.h>
16 : : #include <fcntl.h>
17 : : #include <limits.h>
18 : :
19 : : #ifdef HAVE_COPYFILE_H
20 : : #include <copyfile.h>
21 : : #endif
22 : : #ifdef __linux__
23 : : #include <sys/ioctl.h>
24 : : #include <linux/fs.h>
25 : : #endif
26 : :
27 : : #include "access/xlog_internal.h"
28 : : #include "backup_label.h"
29 : : #include "common/checksum_helper.h"
30 : : #include "common/controldata_utils.h"
31 : : #include "common/file_perm.h"
32 : : #include "common/file_utils.h"
33 : : #include "common/logging.h"
34 : : #include "common/relpath.h"
35 : : #include "copy_file.h"
36 : : #include "fe_utils/option_utils.h"
37 : : #include "fe_utils/version.h"
38 : : #include "getopt_long.h"
39 : : #include "lib/stringinfo.h"
40 : : #include "load_manifest.h"
41 : : #include "reconstruct.h"
42 : : #include "write_manifest.h"
43 : :
44 : : /* Incremental file naming convention. */
45 : : #define INCREMENTAL_PREFIX "INCREMENTAL."
46 : : #define INCREMENTAL_PREFIX_LENGTH (sizeof(INCREMENTAL_PREFIX) - 1)
47 : :
48 : : /*
49 : : * Tracking for directories that need to be removed, or have their contents
50 : : * removed, if the operation fails.
51 : : */
52 : : typedef struct cb_cleanup_dir
53 : : {
54 : : char *target_path;
55 : : bool rmtopdir;
56 : : struct cb_cleanup_dir *next;
57 : : } cb_cleanup_dir;
58 : :
59 : : /*
60 : : * Stores a tablespace mapping provided using -T, --tablespace-mapping.
61 : : */
62 : : typedef struct cb_tablespace_mapping
63 : : {
64 : : char old_dir[MAXPGPATH];
65 : : char new_dir[MAXPGPATH];
66 : : struct cb_tablespace_mapping *next;
67 : : } cb_tablespace_mapping;
68 : :
69 : : /*
70 : : * Stores data parsed from all command-line options.
71 : : */
72 : : typedef struct cb_options
73 : : {
74 : : bool debug;
75 : : char *output;
76 : : bool dry_run;
77 : : bool no_sync;
78 : : cb_tablespace_mapping *tsmappings;
79 : : pg_checksum_type manifest_checksums;
80 : : bool no_manifest;
81 : : DataDirSyncMethod sync_method;
82 : : CopyMethod copy_method;
83 : : } cb_options;
84 : :
85 : : /*
86 : : * Data about a tablespace.
87 : : *
88 : : * Every normal tablespace needs a tablespace mapping, but in-place tablespaces
89 : : * don't, so the list of tablespaces can contain more entries than the list of
90 : : * tablespace mappings.
91 : : */
92 : : typedef struct cb_tablespace
93 : : {
94 : : Oid oid;
95 : : bool in_place;
96 : : char old_dir[MAXPGPATH];
97 : : char new_dir[MAXPGPATH];
98 : : struct cb_tablespace *next;
99 : : } cb_tablespace;
100 : :
101 : : /* Directories to be removed if we exit uncleanly. */
102 : : static cb_cleanup_dir *cleanup_dir_list = NULL;
103 : :
104 : : static void add_tablespace_mapping(cb_options *opt, char *arg);
105 : : static StringInfo check_backup_label_files(int n_backups, char **backup_dirs);
106 : : static uint64 check_control_files(int n_backups, char **backup_dirs);
107 : : static void check_input_dir_permissions(char *dir);
108 : : static void cleanup_directories_atexit(void);
109 : : static void create_output_directory(char *dirname, cb_options *opt);
110 : : static void help(const char *progname);
111 : : static bool parse_oid(char *s, Oid *result);
112 : : static void process_directory_recursively(Oid tsoid,
113 : : char *input_directory,
114 : : char *output_directory,
115 : : char *relative_path,
116 : : int n_prior_backups,
117 : : char **prior_backup_dirs,
118 : : manifest_data **manifests,
119 : : manifest_writer *mwriter,
120 : : cb_options *opt);
121 : : static void remember_to_cleanup_directory(char *target_path, bool rmtopdir);
122 : : static void reset_directory_cleanup_list(void);
123 : : static cb_tablespace *scan_for_existing_tablespaces(char *pathname,
124 : : cb_options *opt);
125 : : static void slurp_file(int fd, char *filename, StringInfo buf, int maxlen);
126 : :
127 : : /*
128 : : * Main program.
129 : : */
130 : : int
677 rhaas@postgresql.org 131 :CBC 25 : main(int argc, char *argv[])
132 : : {
133 : : static struct option long_options[] = {
134 : : {"debug", no_argument, NULL, 'd'},
135 : : {"dry-run", no_argument, NULL, 'n'},
136 : : {"no-sync", no_argument, NULL, 'N'},
137 : : {"output", required_argument, NULL, 'o'},
138 : : {"tablespace-mapping", required_argument, NULL, 'T'},
139 : : {"link", no_argument, NULL, 'k'},
140 : : {"manifest-checksums", required_argument, NULL, 1},
141 : : {"no-manifest", no_argument, NULL, 2},
142 : : {"sync-method", required_argument, NULL, 3},
143 : : {"clone", no_argument, NULL, 4},
144 : : {"copy", no_argument, NULL, 5},
145 : : {"copy-file-range", no_argument, NULL, 6},
146 : : {NULL, 0, NULL, 0}
147 : : };
148 : :
149 : : const char *progname;
150 : : char *last_input_dir;
151 : : int i;
152 : : int optindex;
153 : : int c;
154 : : int n_backups;
155 : : int n_prior_backups;
156 : : uint32 version;
157 : : uint64 system_identifier;
158 : : char **prior_backup_dirs;
159 : : cb_options opt;
160 : : cb_tablespace *tablespaces;
161 : : cb_tablespace *ts;
162 : : StringInfo last_backup_label;
163 : : manifest_data **manifests;
164 : : manifest_writer *mwriter;
165 : : char *pgdata;
166 : :
167 : 25 : pg_logging_init(argv[0]);
168 : 25 : progname = get_progname(argv[0]);
566 michael@paquier.xyz 169 : 25 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_combinebackup"));
677 rhaas@postgresql.org 170 : 25 : handle_help_version_opts(argc, argv, progname, help);
171 : :
172 : 23 : memset(&opt, 0, sizeof(opt));
173 : 23 : opt.manifest_checksums = CHECKSUM_TYPE_CRC32C;
174 : 23 : opt.sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
570 tomas.vondra@postgre 175 : 23 : opt.copy_method = COPY_METHOD_COPY;
176 : :
177 : : /* process command-line options */
224 rhaas@postgresql.org 178 : 72 : while ((c = getopt_long(argc, argv, "dknNo:T:",
677 179 [ + + ]: 72 : long_options, &optindex)) != -1)
180 : : {
181 [ + + - + : 50 : switch (c)
+ + + + -
- + - + ]
182 : : {
183 : 6 : case 'd':
184 : 6 : opt.debug = true;
185 : 6 : pg_logging_increase_verbosity();
186 : 6 : break;
224 187 : 1 : case 'k':
188 : 1 : opt.copy_method = COPY_METHOD_LINK;
189 : 1 : break;
677 rhaas@postgresql.org 190 :UBC 0 : case 'n':
191 : 0 : opt.dry_run = true;
192 : 0 : break;
677 rhaas@postgresql.org 193 :CBC 3 : case 'N':
194 : 3 : opt.no_sync = true;
195 : 3 : break;
196 : 20 : case 'o':
197 : 20 : opt.output = optarg;
198 : 20 : break;
199 : 1 : case 'T':
200 : 1 : add_tablespace_mapping(&opt, optarg);
201 : 1 : break;
202 : 2 : case 1:
203 [ - + ]: 2 : if (!pg_checksum_parse_type(optarg,
204 : : &opt.manifest_checksums))
677 rhaas@postgresql.org 205 :UBC 0 : pg_fatal("unrecognized checksum algorithm: \"%s\"",
206 : : optarg);
677 rhaas@postgresql.org 207 :CBC 2 : break;
208 : 1 : case 2:
209 : 1 : opt.no_manifest = true;
210 : 1 : break;
677 rhaas@postgresql.org 211 :UBC 0 : case 3:
212 [ # # ]: 0 : if (!parse_sync_method(optarg, &opt.sync_method))
213 : 0 : exit(1);
214 : 0 : break;
570 tomas.vondra@postgre 215 : 0 : case 4:
216 : 0 : opt.copy_method = COPY_METHOD_CLONE;
217 : 0 : break;
570 tomas.vondra@postgre 218 :CBC 15 : case 5:
484 219 : 15 : opt.copy_method = COPY_METHOD_COPY;
220 : 15 : break;
484 tomas.vondra@postgre 221 :UBC 0 : case 6:
570 222 : 0 : opt.copy_method = COPY_METHOD_COPY_FILE_RANGE;
223 : 0 : break;
677 rhaas@postgresql.org 224 :CBC 1 : default:
225 : : /* getopt_long already emitted a complaint */
226 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
227 : 1 : exit(1);
228 : : }
229 : : }
230 : :
231 [ + + ]: 22 : if (optind >= argc)
232 : : {
494 peter@eisentraut.org 233 : 1 : pg_log_error("no input directories specified");
677 rhaas@postgresql.org 234 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
235 : 1 : exit(1);
236 : : }
237 : :
238 [ + + ]: 21 : if (opt.output == NULL)
239 : 1 : pg_fatal("no output directory specified");
240 : :
241 : : /* If no manifest is needed, no checksums are needed, either. */
242 [ + + ]: 20 : if (opt.no_manifest)
243 : 1 : opt.manifest_checksums = CHECKSUM_TYPE_NONE;
244 : :
245 : : /* Check that the platform supports the requested copy method. */
570 tomas.vondra@postgre 246 [ - + ]: 20 : if (opt.copy_method == COPY_METHOD_CLONE)
247 : : {
248 : : #if (defined(HAVE_COPYFILE) && defined(COPYFILE_CLONE_FORCE)) || \
249 : : (defined(__linux__) && defined(FICLONE))
250 : :
570 tomas.vondra@postgre 251 [ # # ]:UBC 0 : if (opt.dry_run)
252 [ # # ]: 0 : pg_log_debug("would use cloning to copy files");
253 : : else
254 [ # # ]: 0 : pg_log_debug("will use cloning to copy files");
255 : :
256 : : #else
257 : : pg_fatal("file cloning not supported on this platform");
258 : : #endif
259 : : }
570 tomas.vondra@postgre 260 [ - + ]:CBC 20 : else if (opt.copy_method == COPY_METHOD_COPY_FILE_RANGE)
261 : : {
262 : : #if defined(HAVE_COPY_FILE_RANGE)
263 : :
570 tomas.vondra@postgre 264 [ # # ]:UBC 0 : if (opt.dry_run)
265 [ # # ]: 0 : pg_log_debug("would use copy_file_range to copy blocks");
266 : : else
267 [ # # ]: 0 : pg_log_debug("will use copy_file_range to copy blocks");
268 : :
269 : : #else
270 : : pg_fatal("copy_file_range not supported on this platform");
271 : : #endif
272 : : }
273 : :
274 : : /* Read the server version from the final backup. */
12 michael@paquier.xyz 275 :GNC 20 : pgdata = argv[argc - 1];
276 : 20 : version = get_pg_version(pgdata, NULL);
277 [ - + ]: 20 : if (GET_PG_MAJORVERSION_NUM(version) < 10)
12 michael@paquier.xyz 278 :UNC 0 : pg_fatal("server version too old");
12 michael@paquier.xyz 279 [ + + ]:GNC 20 : pg_log_debug("read server version %u from file \"%s/%s\"",
280 : : GET_PG_MAJORVERSION_NUM(version), pgdata, "PG_VERSION");
281 : :
282 : : /* Sanity-check control files. */
677 rhaas@postgresql.org 283 :CBC 20 : n_backups = argc - optind;
593 284 : 20 : system_identifier = check_control_files(n_backups, argv + optind);
285 : :
286 : : /* Sanity-check backup_label files, and get the contents of the last one. */
677 287 : 19 : last_backup_label = check_backup_label_files(n_backups, argv + optind);
288 : :
289 : : /*
290 : : * We'll need the pathnames to the prior backups. By "prior" we mean all
291 : : * but the last one listed on the command line.
292 : : */
293 : 14 : n_prior_backups = argc - optind - 1;
294 : 14 : prior_backup_dirs = argv + optind;
295 : :
296 : : /* Load backup manifests. */
297 : 14 : manifests = load_backup_manifests(n_backups, prior_backup_dirs);
298 : :
299 : : /*
300 : : * Validate the manifest system identifier against the backup system
301 : : * identifier.
302 : : */
593 303 [ + + ]: 40 : for (i = 0; i < n_backups; i++)
304 : : {
305 [ + - ]: 27 : if (manifests[i] &&
306 [ + + ]: 27 : manifests[i]->system_identifier != system_identifier)
307 : : {
308 : : char *controlpath;
309 : :
203 fujii@postgresql.org 310 : 1 : controlpath = psprintf("%s/%s", prior_backup_dirs[i], XLOG_CONTROL_FILE);
311 : :
212 peter@eisentraut.org 312 : 1 : pg_fatal("%s: manifest system identifier is %" PRIu64 ", but control file has %" PRIu64,
313 : : controlpath,
314 : : manifests[i]->system_identifier,
315 : : system_identifier);
316 : : }
317 : : }
318 : :
319 : : /* Figure out which tablespaces are going to be included in the output. */
677 rhaas@postgresql.org 320 : 13 : last_input_dir = argv[argc - 1];
321 : 13 : check_input_dir_permissions(last_input_dir);
322 : 13 : tablespaces = scan_for_existing_tablespaces(last_input_dir, &opt);
323 : :
324 : : /*
325 : : * Create output directories.
326 : : *
327 : : * We create one output directory for the main data directory plus one for
328 : : * each non-in-place tablespace. create_output_directory() will arrange
329 : : * for those directories to be cleaned up on failure. In-place tablespaces
330 : : * aren't handled at this stage because they're located beneath the main
331 : : * output directory, and thus the cleanup of that directory will get rid
332 : : * of them. Plus, the pg_tblspc directory that needs to contain them
333 : : * doesn't exist yet.
334 : : */
335 : 13 : atexit(cleanup_directories_atexit);
336 : 13 : create_output_directory(opt.output, &opt);
337 [ + + ]: 14 : for (ts = tablespaces; ts != NULL; ts = ts->next)
338 [ + - ]: 1 : if (!ts->in_place)
339 : 1 : create_output_directory(ts->new_dir, &opt);
340 : :
341 : : /* If we need to write a backup_manifest, prepare to do so. */
342 [ + - + + ]: 13 : if (!opt.dry_run && !opt.no_manifest)
343 : : {
593 344 : 12 : mwriter = create_manifest_writer(opt.output, system_identifier);
345 : :
346 : : /*
347 : : * Verify that we have a backup manifest for the final backup; else we
348 : : * won't have the WAL ranges for the resulting manifest.
349 : : */
677 350 [ - + ]: 12 : if (manifests[n_prior_backups] == NULL)
426 peter@eisentraut.org 351 :UBC 0 : pg_fatal("cannot generate a manifest because no manifest is available for the final input backup");
352 : : }
353 : : else
677 rhaas@postgresql.org 354 :CBC 1 : mwriter = NULL;
355 : :
356 : : /* Write backup label into output directory. */
357 [ - + ]: 13 : if (opt.dry_run)
677 rhaas@postgresql.org 358 [ # # ]:UBC 0 : pg_log_debug("would generate \"%s/backup_label\"", opt.output);
359 : : else
360 : : {
677 rhaas@postgresql.org 361 [ + + ]:CBC 13 : pg_log_debug("generating \"%s/backup_label\"", opt.output);
362 : 13 : last_backup_label->cursor = 0;
363 : 13 : write_backup_label(opt.output, last_backup_label,
364 : : opt.manifest_checksums, mwriter);
365 : : }
366 : :
367 : : /* Process everything that's not part of a user-defined tablespace. */
368 [ + + ]: 13 : pg_log_debug("processing backup directory \"%s\"", last_input_dir);
369 : 13 : process_directory_recursively(InvalidOid, last_input_dir, opt.output,
370 : : NULL, n_prior_backups, prior_backup_dirs,
371 : : manifests, mwriter, &opt);
372 : :
373 : : /* Process user-defined tablespaces. */
374 [ + + ]: 13 : for (ts = tablespaces; ts != NULL; ts = ts->next)
375 : : {
376 [ + - ]: 1 : pg_log_debug("processing tablespace directory \"%s\"", ts->old_dir);
377 : :
378 : : /*
379 : : * If it's a normal tablespace, we need to set up a symbolic link from
380 : : * pg_tblspc/${OID} to the target directory; if it's an in-place
381 : : * tablespace, we need to create a directory at pg_tblspc/${OID}.
382 : : */
383 [ + - ]: 1 : if (!ts->in_place)
384 : : {
385 : : char linkpath[MAXPGPATH];
386 : :
419 michael@paquier.xyz 387 : 1 : snprintf(linkpath, MAXPGPATH, "%s/%s/%u", opt.output, PG_TBLSPC_DIR,
388 : : ts->oid);
389 : :
677 rhaas@postgresql.org 390 [ - + ]: 1 : if (opt.dry_run)
677 rhaas@postgresql.org 391 [ # # ]:UBC 0 : pg_log_debug("would create symbolic link from \"%s\" to \"%s\"",
392 : : linkpath, ts->new_dir);
393 : : else
394 : : {
677 rhaas@postgresql.org 395 [ + - ]:CBC 1 : pg_log_debug("creating symbolic link from \"%s\" to \"%s\"",
396 : : linkpath, ts->new_dir);
397 [ - + ]: 1 : if (symlink(ts->new_dir, linkpath) != 0)
677 rhaas@postgresql.org 398 :UBC 0 : pg_fatal("could not create symbolic link from \"%s\" to \"%s\": %m",
399 : : linkpath, ts->new_dir);
400 : : }
401 : : }
402 : : else
403 : : {
404 [ # # ]: 0 : if (opt.dry_run)
405 [ # # ]: 0 : pg_log_debug("would create directory \"%s\"", ts->new_dir);
406 : : else
407 : : {
408 [ # # ]: 0 : pg_log_debug("creating directory \"%s\"", ts->new_dir);
409 [ # # ]: 0 : if (pg_mkdir_p(ts->new_dir, pg_dir_create_mode) == -1)
410 : 0 : pg_fatal("could not create directory \"%s\": %m",
411 : : ts->new_dir);
412 : : }
413 : : }
414 : :
415 : : /* OK, now handle the directory contents. */
677 rhaas@postgresql.org 416 :CBC 1 : process_directory_recursively(ts->oid, ts->old_dir, ts->new_dir,
417 : : NULL, n_prior_backups, prior_backup_dirs,
418 : : manifests, mwriter, &opt);
419 : : }
420 : :
421 : : /* Finalize the backup_manifest, if we're generating one. */
422 [ + + ]: 12 : if (mwriter != NULL)
423 : 11 : finalize_manifest(mwriter,
424 : 11 : manifests[n_prior_backups]->first_wal_range);
425 : :
426 : : /* fsync that output directory unless we've been told not to do so */
427 [ + + ]: 12 : if (!opt.no_sync)
428 : : {
429 [ - + ]: 9 : if (opt.dry_run)
677 rhaas@postgresql.org 430 [ # # ]:UBC 0 : pg_log_debug("would recursively fsync \"%s\"", opt.output);
431 : : else
432 : : {
677 rhaas@postgresql.org 433 [ + + ]:CBC 9 : pg_log_debug("recursively fsyncing \"%s\"", opt.output);
13 michael@paquier.xyz 434 : 9 : sync_pgdata(opt.output, version, opt.sync_method, true);
435 : : }
436 : : }
437 : :
438 : : /* Warn about the possibility of compromising the backups, when link mode */
224 rhaas@postgresql.org 439 [ + + ]: 12 : if (opt.copy_method == COPY_METHOD_LINK)
440 : 1 : pg_log_warning("--link mode was used; any modifications to the output "
441 : : "directory might destructively modify input directories");
442 : :
443 : : /* It's a success, so don't remove the output directories. */
677 444 : 12 : reset_directory_cleanup_list();
445 : 12 : exit(0);
446 : : }
447 : :
448 : : /*
449 : : * Process the option argument for the -T, --tablespace-mapping switch.
450 : : */
451 : : static void
452 : 1 : add_tablespace_mapping(cb_options *opt, char *arg)
453 : : {
454 : 1 : cb_tablespace_mapping *tsmap = pg_malloc0(sizeof(cb_tablespace_mapping));
455 : : char *dst;
456 : : char *dst_ptr;
457 : : char *arg_ptr;
458 : :
459 : : /*
460 : : * Basically, we just want to copy everything before the equals sign to
461 : : * tsmap->old_dir and everything afterwards to tsmap->new_dir, but if
462 : : * there's more or less than one equals sign, that's an error, and if
463 : : * there's an equals sign preceded by a backslash, don't treat it as a
464 : : * field separator but instead copy a literal equals sign.
465 : : */
466 : 1 : dst_ptr = dst = tsmap->old_dir;
467 [ + + ]: 50 : for (arg_ptr = arg; *arg_ptr != '\0'; arg_ptr++)
468 : : {
469 [ - + ]: 49 : if (dst_ptr - dst >= MAXPGPATH)
677 rhaas@postgresql.org 470 :UBC 0 : pg_fatal("directory name too long");
471 : :
677 rhaas@postgresql.org 472 [ - + - - ]:CBC 49 : if (*arg_ptr == '\\' && *(arg_ptr + 1) == '=')
473 : : ; /* skip backslash escaping = */
474 [ + + + - : 49 : else if (*arg_ptr == '=' && (arg_ptr == arg || *(arg_ptr - 1) != '\\'))
+ - ]
475 : : {
476 [ - + ]: 1 : if (tsmap->new_dir[0] != '\0')
677 rhaas@postgresql.org 477 :UBC 0 : pg_fatal("multiple \"=\" signs in tablespace mapping");
478 : : else
677 rhaas@postgresql.org 479 :CBC 1 : dst = dst_ptr = tsmap->new_dir;
480 : : }
481 : : else
482 : 48 : *dst_ptr++ = *arg_ptr;
483 : : }
484 [ + - - + ]: 1 : if (!tsmap->old_dir[0] || !tsmap->new_dir[0])
677 rhaas@postgresql.org 485 :UBC 0 : pg_fatal("invalid tablespace mapping format \"%s\", must be \"OLDDIR=NEWDIR\"", arg);
486 : :
487 : : /*
488 : : * All tablespaces are created with absolute directories, so specifying a
489 : : * non-absolute path here would never match, possibly confusing users.
490 : : *
491 : : * In contrast to pg_basebackup, both the old and new directories are on
492 : : * the local machine, so the local machine's definition of an absolute
493 : : * path is the only relevant one.
494 : : */
677 rhaas@postgresql.org 495 [ - + ]:CBC 1 : if (!is_absolute_path(tsmap->old_dir))
677 rhaas@postgresql.org 496 :UBC 0 : pg_fatal("old directory is not an absolute path in tablespace mapping: %s",
497 : : tsmap->old_dir);
498 : :
677 rhaas@postgresql.org 499 [ - + ]:CBC 1 : if (!is_absolute_path(tsmap->new_dir))
677 rhaas@postgresql.org 500 :UBC 0 : pg_fatal("old directory is not an absolute path in tablespace mapping: %s",
501 : : tsmap->new_dir);
502 : :
503 : : /* Canonicalize paths to avoid spurious failures when comparing. */
677 rhaas@postgresql.org 504 :CBC 1 : canonicalize_path(tsmap->old_dir);
505 : 1 : canonicalize_path(tsmap->new_dir);
506 : :
507 : : /* Add it to the list. */
508 : 1 : tsmap->next = opt->tsmappings;
509 : 1 : opt->tsmappings = tsmap;
510 : 1 : }
511 : :
512 : : /*
513 : : * Check that the backup_label files form a coherent backup chain, and return
514 : : * the contents of the backup_label file from the latest backup.
515 : : */
516 : : static StringInfo
517 : 19 : check_backup_label_files(int n_backups, char **backup_dirs)
518 : : {
519 : 19 : StringInfo buf = makeStringInfo();
520 : 19 : StringInfo lastbuf = buf;
521 : : int i;
522 : 19 : TimeLineID check_tli = 0;
523 : 19 : XLogRecPtr check_lsn = InvalidXLogRecPtr;
524 : :
525 : : /* Try to read each backup_label file in turn, last to first. */
526 [ + + ]: 51 : for (i = n_backups - 1; i >= 0; --i)
527 : : {
528 : : char pathbuf[MAXPGPATH];
529 : : int fd;
530 : : TimeLineID start_tli;
531 : : TimeLineID previous_tli;
532 : : XLogRecPtr start_lsn;
533 : : XLogRecPtr previous_lsn;
534 : :
535 : : /* Open the backup_label file. */
536 : 37 : snprintf(pathbuf, MAXPGPATH, "%s/backup_label", backup_dirs[i]);
537 [ + + ]: 37 : pg_log_debug("reading \"%s\"", pathbuf);
538 [ - + ]: 37 : if ((fd = open(pathbuf, O_RDONLY, 0)) < 0)
677 rhaas@postgresql.org 539 :UBC 0 : pg_fatal("could not open file \"%s\": %m", pathbuf);
540 : :
541 : : /*
542 : : * Slurp the whole file into memory.
543 : : *
544 : : * The exact size limit that we impose here doesn't really matter --
545 : : * most of what's supposed to be in the file is fixed size and quite
546 : : * short. However, the length of the backup_label is limited (at least
547 : : * by some parts of the code) to MAXPGPATH, so include that value in
548 : : * the maximum length that we tolerate.
549 : : */
677 rhaas@postgresql.org 550 :CBC 37 : slurp_file(fd, pathbuf, buf, 10000 + MAXPGPATH);
551 : :
552 : : /* Close the file. */
553 [ - + ]: 37 : if (close(fd) != 0)
493 peter@eisentraut.org 554 :UBC 0 : pg_fatal("could not close file \"%s\": %m", pathbuf);
555 : :
556 : : /* Parse the file contents. */
677 rhaas@postgresql.org 557 :CBC 37 : parse_backup_label(pathbuf, buf, &start_tli, &start_lsn,
558 : : &previous_tli, &previous_lsn);
559 : :
560 : : /*
561 : : * Sanity checks.
562 : : *
563 : : * XXX. It's actually not required that start_lsn == check_lsn. It
564 : : * would be OK if start_lsn > check_lsn provided that start_lsn is
565 : : * less than or equal to the relevant switchpoint. But at the moment
566 : : * we don't have that information.
567 : : */
568 [ + + + + ]: 37 : if (i > 0 && previous_tli == 0)
569 : 1 : pg_fatal("backup at \"%s\" is a full backup, but only the first backup should be a full backup",
570 : : backup_dirs[i]);
571 [ + + + + ]: 36 : if (i == 0 && previous_tli != 0)
572 : 1 : pg_fatal("backup at \"%s\" is an incremental backup, but the first backup should be a full backup",
573 : : backup_dirs[i]);
574 [ + + - + ]: 35 : if (i < n_backups - 1 && start_tli != check_tli)
677 rhaas@postgresql.org 575 :UBC 0 : pg_fatal("backup at \"%s\" starts on timeline %u, but expected %u",
576 : : backup_dirs[i], start_tli, check_tli);
677 rhaas@postgresql.org 577 [ + + + + ]:CBC 35 : if (i < n_backups - 1 && start_lsn != check_lsn)
112 alvherre@kurilemu.de 578 :GNC 3 : pg_fatal("backup at \"%s\" starts at LSN %X/%08X, but expected %X/%08X",
579 : : backup_dirs[i],
580 : : LSN_FORMAT_ARGS(start_lsn),
581 : : LSN_FORMAT_ARGS(check_lsn));
677 rhaas@postgresql.org 582 :CBC 32 : check_tli = previous_tli;
583 : 32 : check_lsn = previous_lsn;
584 : :
585 : : /*
586 : : * The last backup label in the chain needs to be saved for later use,
587 : : * while the others are only needed within this loop.
588 : : */
589 [ + + ]: 32 : if (lastbuf == buf)
590 : 18 : buf = makeStringInfo();
591 : : else
592 : 14 : resetStringInfo(buf);
593 : : }
594 : :
595 : : /* Free memory that we don't need any more. */
596 [ + - ]: 14 : if (lastbuf != buf)
590 dgustafsson@postgres 597 : 14 : destroyStringInfo(buf);
598 : :
599 : : /*
600 : : * Return the data from the first backup_info that we read (which is the
601 : : * backup_label from the last directory specified on the command line).
602 : : */
677 rhaas@postgresql.org 603 : 14 : return lastbuf;
604 : : }
605 : :
606 : : /*
607 : : * Sanity check control files and return system_identifier.
608 : : */
609 : : static uint64
610 : 20 : check_control_files(int n_backups, char **backup_dirs)
611 : : {
612 : : int i;
613 : 20 : uint64 system_identifier = 0; /* placate compiler */
550 614 : 20 : uint32 data_checksum_version = 0; /* placate compiler */
615 : 20 : bool data_checksum_mismatch = false;
616 : :
617 : : /* Try to read each control file in turn, last to first. */
677 618 [ + + ]: 60 : for (i = n_backups - 1; i >= 0; --i)
619 : : {
620 : : ControlFileData *control_file;
621 : : bool crc_ok;
622 : : char *controlpath;
623 : :
203 fujii@postgresql.org 624 : 41 : controlpath = psprintf("%s/%s", backup_dirs[i], XLOG_CONTROL_FILE);
677 rhaas@postgresql.org 625 [ + + ]: 41 : pg_log_debug("reading \"%s\"", controlpath);
593 626 : 41 : control_file = get_controlfile_by_exact_path(controlpath, &crc_ok);
627 : :
628 : : /* Control file contents not meaningful if CRC is bad. */
677 629 [ - + ]: 41 : if (!crc_ok)
670 john.naylor@postgres 630 :UBC 0 : pg_fatal("%s: CRC is incorrect", controlpath);
631 : :
632 : : /* Can't interpret control file if not current version. */
677 rhaas@postgresql.org 633 [ - + ]:CBC 41 : if (control_file->pg_control_version != PG_CONTROL_VERSION)
677 rhaas@postgresql.org 634 :UBC 0 : pg_fatal("%s: unexpected control file version",
635 : : controlpath);
636 : :
637 : : /* System identifiers should all match. */
677 rhaas@postgresql.org 638 [ + + ]:CBC 41 : if (i == n_backups - 1)
639 : 20 : system_identifier = control_file->system_identifier;
640 [ + + ]: 21 : else if (system_identifier != control_file->system_identifier)
212 peter@eisentraut.org 641 : 1 : pg_fatal("%s: expected system identifier %" PRIu64 ", but found %" PRIu64,
642 : : controlpath, system_identifier,
643 : : control_file->system_identifier);
644 : :
645 : : /*
646 : : * Detect checksum mismatches, but only if the last backup in the
647 : : * chain has checksums enabled.
648 : : */
550 rhaas@postgresql.org 649 [ + + ]: 40 : if (i == n_backups - 1)
650 : 20 : data_checksum_version = control_file->data_checksum_version;
651 [ + - ]: 20 : else if (data_checksum_version != 0 &&
652 [ - + ]: 20 : data_checksum_version != control_file->data_checksum_version)
550 rhaas@postgresql.org 653 :UBC 0 : data_checksum_mismatch = true;
654 : :
655 : : /* Release memory. */
677 rhaas@postgresql.org 656 :CBC 40 : pfree(control_file);
657 : 40 : pfree(controlpath);
658 : : }
659 : :
660 : : /*
661 : : * If debug output is enabled, make a note of the system identifier that
662 : : * we found in all of the relevant control files.
663 : : */
212 peter@eisentraut.org 664 [ + + ]: 19 : pg_log_debug("system identifier is %" PRIu64, system_identifier);
665 : :
666 : : /*
667 : : * Warn the user if not all backups are in the same state with regards to
668 : : * checksums.
669 : : */
550 rhaas@postgresql.org 670 [ - + ]: 19 : if (data_checksum_mismatch)
671 : : {
550 rhaas@postgresql.org 672 :UBC 0 : pg_log_warning("only some backups have checksums enabled");
426 peter@eisentraut.org 673 : 0 : pg_log_warning_hint("Disable, and optionally reenable, checksums on the output directory to avoid failures.");
674 : : }
675 : :
593 rhaas@postgresql.org 676 :CBC 19 : return system_identifier;
677 : : }
678 : :
679 : : /*
680 : : * Set default permissions for new files and directories based on the
681 : : * permissions of the given directory. The intent here is that the output
682 : : * directory should use the same permissions scheme as the final input
683 : : * directory.
684 : : */
685 : : static void
677 686 : 13 : check_input_dir_permissions(char *dir)
687 : : {
688 : : struct stat st;
689 : :
690 [ - + ]: 13 : if (stat(dir, &st) != 0)
493 peter@eisentraut.org 691 :UBC 0 : pg_fatal("could not stat file \"%s\": %m", dir);
692 : :
677 rhaas@postgresql.org 693 :CBC 13 : SetDataDirectoryCreatePerm(st.st_mode);
694 : 13 : }
695 : :
696 : : /*
697 : : * Clean up output directories before exiting.
698 : : */
699 : : static void
700 : 13 : cleanup_directories_atexit(void)
701 : : {
702 [ + + ]: 14 : while (cleanup_dir_list != NULL)
703 : : {
704 : 1 : cb_cleanup_dir *dir = cleanup_dir_list;
705 : :
706 [ + - ]: 1 : if (dir->rmtopdir)
707 : : {
708 : 1 : pg_log_info("removing output directory \"%s\"", dir->target_path);
709 [ - + ]: 1 : if (!rmtree(dir->target_path, dir->rmtopdir))
677 rhaas@postgresql.org 710 :UBC 0 : pg_log_error("failed to remove output directory");
711 : : }
712 : : else
713 : : {
714 : 0 : pg_log_info("removing contents of output directory \"%s\"",
715 : : dir->target_path);
716 [ # # ]: 0 : if (!rmtree(dir->target_path, dir->rmtopdir))
717 : 0 : pg_log_error("failed to remove contents of output directory");
718 : : }
719 : :
677 rhaas@postgresql.org 720 :CBC 1 : cleanup_dir_list = cleanup_dir_list->next;
721 : 1 : pfree(dir);
722 : : }
723 : 13 : }
724 : :
725 : : /*
726 : : * Create the named output directory, unless it already exists or we're in
727 : : * dry-run mode. If it already exists but is not empty, that's a fatal error.
728 : : *
729 : : * Adds the created directory to the list of directories to be cleaned up
730 : : * at process exit.
731 : : */
732 : : static void
733 : 14 : create_output_directory(char *dirname, cb_options *opt)
734 : : {
735 [ + - - - : 14 : switch (pg_check_dir(dirname))
- ]
736 : : {
737 : 14 : case 0:
738 [ - + ]: 14 : if (opt->dry_run)
739 : : {
677 rhaas@postgresql.org 740 [ # # ]:UBC 0 : pg_log_debug("would create directory \"%s\"", dirname);
741 : 0 : return;
742 : : }
677 rhaas@postgresql.org 743 [ + + ]:CBC 14 : pg_log_debug("creating directory \"%s\"", dirname);
744 [ - + ]: 14 : if (pg_mkdir_p(dirname, pg_dir_create_mode) == -1)
677 rhaas@postgresql.org 745 :UBC 0 : pg_fatal("could not create directory \"%s\": %m", dirname);
677 rhaas@postgresql.org 746 :CBC 14 : remember_to_cleanup_directory(dirname, true);
747 : 14 : break;
748 : :
677 rhaas@postgresql.org 749 :UBC 0 : case 1:
750 [ # # ]: 0 : pg_log_debug("using existing directory \"%s\"", dirname);
751 : 0 : remember_to_cleanup_directory(dirname, false);
752 : 0 : break;
753 : :
754 : 0 : case 2:
755 : : case 3:
756 : : case 4:
757 : 0 : pg_fatal("directory \"%s\" exists but is not empty", dirname);
758 : :
759 : 0 : case -1:
760 : 0 : pg_fatal("could not access directory \"%s\": %m", dirname);
761 : : }
762 : : }
763 : :
764 : : /*
765 : : * help
766 : : *
767 : : * Prints help page for the program
768 : : *
769 : : * progname: the name of the executed program, such as "pg_combinebackup"
770 : : */
771 : : static void
677 rhaas@postgresql.org 772 :CBC 1 : help(const char *progname)
773 : : {
774 : 1 : printf(_("%s reconstructs full backups from incrementals.\n\n"), progname);
775 : 1 : printf(_("Usage:\n"));
776 : 1 : printf(_(" %s [OPTION]... DIRECTORY...\n"), progname);
777 : 1 : printf(_("\nOptions:\n"));
778 : 1 : printf(_(" -d, --debug generate lots of debugging output\n"));
224 779 : 1 : printf(_(" -k, --link link files instead of copying\n"));
549 780 : 1 : printf(_(" -n, --dry-run do not actually do anything\n"));
677 781 : 1 : printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n"));
494 peter@eisentraut.org 782 : 1 : printf(_(" -o, --output=DIRECTORY output directory\n"));
671 michael@paquier.xyz 783 : 1 : printf(_(" -T, --tablespace-mapping=OLDDIR=NEWDIR\n"
784 : : " relocate tablespace in OLDDIR to NEWDIR\n"));
426 peter@eisentraut.org 785 : 1 : printf(_(" --clone clone (reflink) files instead of copying\n"));
484 tomas.vondra@postgre 786 : 1 : printf(_(" --copy copy files (default)\n"));
426 peter@eisentraut.org 787 : 1 : printf(_(" --copy-file-range copy using copy_file_range() system call\n"));
677 rhaas@postgresql.org 788 : 1 : printf(_(" --manifest-checksums=SHA{224,256,384,512}|CRC32C|NONE\n"
789 : : " use algorithm for manifest checksums\n"));
790 : 1 : printf(_(" --no-manifest suppress generation of backup manifest\n"));
791 : 1 : printf(_(" --sync-method=METHOD set method for syncing files to disk\n"));
551 peter@eisentraut.org 792 : 1 : printf(_(" -V, --version output version information, then exit\n"));
677 rhaas@postgresql.org 793 : 1 : printf(_(" -?, --help show this help, then exit\n"));
794 : :
795 : 1 : printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
796 : 1 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
797 : 1 : }
798 : :
799 : : /*
800 : : * Try to parse a string as a non-zero OID without leading zeroes.
801 : : *
802 : : * If it works, return true and set *result to the answer, else return false.
803 : : */
804 : : static bool
805 : 2 : parse_oid(char *s, Oid *result)
806 : : {
807 : : Oid oid;
808 : : char *ep;
809 : :
810 : 2 : errno = 0;
811 : 2 : oid = strtoul(s, &ep, 10);
812 [ + - + - : 2 : if (errno != 0 || *ep != '\0' || oid < 1 || oid > PG_UINT32_MAX)
- + ]
677 rhaas@postgresql.org 813 :UBC 0 : return false;
814 : :
677 rhaas@postgresql.org 815 :CBC 2 : *result = oid;
816 : 2 : return true;
817 : : }
818 : :
819 : : /*
820 : : * Copy files from the input directory to the output directory, reconstructing
821 : : * full files from incremental files as required.
822 : : *
823 : : * If processing a user-defined tablespace, the tsoid should be the OID
824 : : * of that tablespace and input_directory and output_directory should be the
825 : : * toplevel input and output directories for that tablespace. Otherwise,
826 : : * tsoid should be InvalidOid and input_directory and output_directory should
827 : : * be the main input and output directories.
828 : : *
829 : : * relative_path is the path beneath the given input and output directories
830 : : * that we are currently processing. If NULL, it indicates that we're
831 : : * processing the input and output directories themselves.
832 : : *
833 : : * n_prior_backups is the number of prior backups that we have available.
834 : : * This doesn't count the very last backup, which is referenced by
835 : : * input_directory, just the older ones. prior_backup_dirs is an array of
836 : : * the locations of those previous backups.
837 : : */
838 : : static void
839 : 341 : process_directory_recursively(Oid tsoid,
840 : : char *input_directory,
841 : : char *output_directory,
842 : : char *relative_path,
843 : : int n_prior_backups,
844 : : char **prior_backup_dirs,
845 : : manifest_data **manifests,
846 : : manifest_writer *mwriter,
847 : : cb_options *opt)
848 : : {
849 : : char ifulldir[MAXPGPATH];
850 : : char ofulldir[MAXPGPATH];
851 : : char manifest_prefix[MAXPGPATH];
852 : : DIR *dir;
853 : : struct dirent *de;
557 854 : 341 : bool is_pg_tblspc = false;
855 : 341 : bool is_pg_wal = false;
856 : 341 : bool is_incremental_dir = false;
677 857 : 341 : manifest_data *latest_manifest = manifests[n_prior_backups];
858 : : pg_checksum_type checksum_type;
859 : :
860 : : /*
861 : : * Classify this directory.
862 : : *
863 : : * We set is_pg_tblspc only for the toplevel pg_tblspc directory, because
864 : : * the symlinks in that specific directory require special handling.
865 : : *
866 : : * We set is_pg_wal for the toplevel WAL directory and all of its
867 : : * subdirectories, because those files are not included in the backup
868 : : * manifest and hence need special treatment. (Since incremental backup
869 : : * does not exist in pre-v10 versions, we don't have to worry about the
870 : : * old pg_xlog naming.)
871 : : *
872 : : * We set is_incremental_dir for directories that can contain incremental
873 : : * files requiring reconstruction. If such files occur outside these
874 : : * directories, we want to just copy them straight to the output
875 : : * directory. This is to protect against a user creating a file with a
876 : : * strange name like INCREMENTAL.config and then complaining that
877 : : * incremental backups don't work properly. The test here is a bit tricky:
878 : : * incremental files occur in subdirectories of base, in pg_global itself,
879 : : * and in subdirectories of pg_tblspc only if in-place tablespaces are
880 : : * used.
881 : : */
557 882 [ + + ]: 341 : if (OidIsValid(tsoid))
883 : 3 : is_incremental_dir = true;
884 [ + + ]: 338 : else if (relative_path != NULL)
885 : : {
419 michael@paquier.xyz 886 : 325 : is_pg_tblspc = strcmp(relative_path, PG_TBLSPC_DIR) == 0;
557 rhaas@postgresql.org 887 [ + + ]: 637 : is_pg_wal = (strcmp(relative_path, "pg_wal") == 0 ||
888 [ + + ]: 312 : strncmp(relative_path, "pg_wal/", 7) == 0);
889 : 325 : is_incremental_dir = strncmp(relative_path, "base/", 5) == 0 ||
890 [ + + + + ]: 598 : strcmp(relative_path, "global") == 0 ||
419 michael@paquier.xyz 891 [ - + ]: 273 : strncmp(relative_path, PG_TBLSPC_DIR_SLASH, 10) == 0;
892 : : }
893 : :
894 : : /*
895 : : * If we're under pg_wal, then we don't need checksums, because these
896 : : * files aren't included in the backup manifest. Otherwise use whatever
897 : : * type of checksum is configured.
898 : : */
677 rhaas@postgresql.org 899 [ + + ]: 341 : if (!is_pg_wal)
900 : 302 : checksum_type = opt->manifest_checksums;
901 : : else
902 : 39 : checksum_type = CHECKSUM_TYPE_NONE;
903 : :
904 : : /*
905 : : * Append the relative path to the input and output directories, and
906 : : * figure out the appropriate prefix to add to files in this directory
907 : : * when looking them up in a backup manifest.
908 : : */
909 [ + + ]: 341 : if (relative_path == NULL)
910 : : {
655 911 : 14 : strlcpy(ifulldir, input_directory, MAXPGPATH);
912 : 14 : strlcpy(ofulldir, output_directory, MAXPGPATH);
677 913 [ + + ]: 14 : if (OidIsValid(tsoid))
419 michael@paquier.xyz 914 : 1 : snprintf(manifest_prefix, MAXPGPATH, "%s/%u/", PG_TBLSPC_DIR, tsoid);
915 : : else
677 rhaas@postgresql.org 916 : 13 : manifest_prefix[0] = '\0';
917 : : }
918 : : else
919 : : {
920 : 327 : snprintf(ifulldir, MAXPGPATH, "%s/%s", input_directory,
921 : : relative_path);
922 : 327 : snprintf(ofulldir, MAXPGPATH, "%s/%s", output_directory,
923 : : relative_path);
924 [ + + ]: 327 : if (OidIsValid(tsoid))
419 michael@paquier.xyz 925 : 2 : snprintf(manifest_prefix, MAXPGPATH, "%s/%u/%s/",
926 : : PG_TBLSPC_DIR, tsoid, relative_path);
927 : : else
677 rhaas@postgresql.org 928 : 325 : snprintf(manifest_prefix, MAXPGPATH, "%s/", relative_path);
929 : : }
930 : :
931 : : /*
932 : : * Toplevel output directories have already been created by the time this
933 : : * function is called, but any subdirectories are our responsibility.
934 : : */
935 [ + + ]: 341 : if (relative_path != NULL)
936 : : {
937 [ - + ]: 327 : if (opt->dry_run)
677 rhaas@postgresql.org 938 [ # # ]:UBC 0 : pg_log_debug("would create directory \"%s\"", ofulldir);
939 : : else
940 : : {
677 rhaas@postgresql.org 941 [ + + ]:CBC 327 : pg_log_debug("creating directory \"%s\"", ofulldir);
942 [ - + ]: 327 : if (mkdir(ofulldir, pg_dir_create_mode) == -1)
677 rhaas@postgresql.org 943 :UBC 0 : pg_fatal("could not create directory \"%s\": %m", ofulldir);
944 : : }
945 : : }
946 : :
947 : : /* It's time to scan the directory. */
677 rhaas@postgresql.org 948 [ - + ]:CBC 341 : if ((dir = opendir(ifulldir)) == NULL)
677 rhaas@postgresql.org 949 :UBC 0 : pg_fatal("could not open directory \"%s\": %m", ifulldir);
677 rhaas@postgresql.org 950 [ + + ]:CBC 13946 : while (errno = 0, (de = readdir(dir)) != NULL)
951 : : {
952 : : PGFileType type;
953 : : char ifullpath[MAXPGPATH];
954 : : char ofullpath[MAXPGPATH];
955 : : char manifest_path[MAXPGPATH];
956 : 13608 : Oid oid = InvalidOid;
957 : 13608 : int checksum_length = 0;
958 : 13608 : uint8 *checksum_payload = NULL;
959 : : pg_checksum_context checksum_ctx;
960 : :
961 : : /* Ignore "." and ".." entries. */
962 [ + + ]: 13608 : if (strcmp(de->d_name, ".") == 0 ||
963 [ + + ]: 13269 : strcmp(de->d_name, "..") == 0)
964 : 1030 : continue;
965 : :
966 : : /* Construct input path. */
967 : 12929 : snprintf(ifullpath, MAXPGPATH, "%s/%s", ifulldir, de->d_name);
968 : :
969 : : /* Figure out what kind of directory entry this is. */
970 : 12929 : type = get_dirent_type(ifullpath, de, false, PG_LOG_ERROR);
971 [ - + ]: 12929 : if (type == PGFILETYPE_ERROR)
677 rhaas@postgresql.org 972 :UBC 0 : exit(1);
973 : :
974 : : /*
975 : : * If we're processing pg_tblspc, then check whether the filename
976 : : * looks like it could be a tablespace OID. If so, and if the
977 : : * directory entry is a symbolic link or a directory, skip it.
978 : : *
979 : : * Our goal here is to ignore anything that would have been considered
980 : : * by scan_for_existing_tablespaces to be a tablespace.
981 : : */
677 rhaas@postgresql.org 982 [ + + + - :CBC 12929 : if (is_pg_tblspc && parse_oid(de->d_name, &oid) &&
- + ]
677 rhaas@postgresql.org 983 [ # # ]:UBC 0 : (type == PGFILETYPE_LNK || type == PGFILETYPE_DIR))
677 rhaas@postgresql.org 984 :CBC 1 : continue;
985 : :
986 : : /* If it's a directory, recurse. */
987 [ + + ]: 12928 : if (type == PGFILETYPE_DIR)
988 : 325 : {
989 : : char new_relative_path[MAXPGPATH];
990 : :
991 : : /* Append new pathname component to relative path. */
992 [ + + ]: 327 : if (relative_path == NULL)
655 993 : 212 : strlcpy(new_relative_path, de->d_name, MAXPGPATH);
994 : : else
677 995 : 115 : snprintf(new_relative_path, MAXPGPATH, "%s/%s", relative_path,
996 : 115 : de->d_name);
997 : :
998 : : /* And recurse. */
999 : 327 : process_directory_recursively(tsoid,
1000 : : input_directory, output_directory,
1001 : : new_relative_path,
1002 : : n_prior_backups, prior_backup_dirs,
1003 : : manifests, mwriter, opt);
1004 : 325 : continue;
1005 : : }
1006 : :
1007 : : /* Skip anything that's not a regular file. */
1008 [ - + ]: 12601 : if (type != PGFILETYPE_REG)
1009 : : {
677 rhaas@postgresql.org 1010 [ # # ]:UBC 0 : if (type == PGFILETYPE_LNK)
1011 : 0 : pg_log_warning("skipping symbolic link \"%s\"", ifullpath);
1012 : : else
1013 : 0 : pg_log_warning("skipping special file \"%s\"", ifullpath);
1014 : 0 : continue;
1015 : : }
1016 : :
1017 : : /*
1018 : : * Skip the backup_label and backup_manifest files; they require
1019 : : * special handling and are handled elsewhere.
1020 : : */
677 rhaas@postgresql.org 1021 [ + + ]:CBC 12601 : if (relative_path == NULL &&
1022 [ + + ]: 95 : (strcmp(de->d_name, "backup_label") == 0 ||
1023 [ + + ]: 82 : strcmp(de->d_name, "backup_manifest") == 0))
1024 : 25 : continue;
1025 : :
1026 : : /*
1027 : : * If it's an incremental file, hand it off to the reconstruction
1028 : : * code, which will figure out what to do.
1029 : : */
557 1030 [ + + ]: 12576 : if (is_incremental_dir &&
1031 [ + + ]: 12443 : strncmp(de->d_name, INCREMENTAL_PREFIX,
1032 : : INCREMENTAL_PREFIX_LENGTH) == 0)
1033 : : {
1034 : : /* Output path should not include "INCREMENTAL." prefix. */
677 1035 : 6206 : snprintf(ofullpath, MAXPGPATH, "%s/%s", ofulldir,
1036 : 6206 : de->d_name + INCREMENTAL_PREFIX_LENGTH);
1037 : :
1038 : :
1039 : : /* Manifest path likewise omits incremental prefix. */
1040 : 6206 : snprintf(manifest_path, MAXPGPATH, "%s%s", manifest_prefix,
1041 : 6206 : de->d_name + INCREMENTAL_PREFIX_LENGTH);
1042 : :
1043 : : /* Reconstruction logic will do the rest. */
1044 : 6206 : reconstruct_from_incremental_file(ifullpath, ofullpath,
1045 : : manifest_prefix,
1046 : 6206 : de->d_name + INCREMENTAL_PREFIX_LENGTH,
1047 : : n_prior_backups,
1048 : : prior_backup_dirs,
1049 : : manifests,
1050 : : manifest_path,
1051 : : checksum_type,
1052 : : &checksum_length,
1053 : : &checksum_payload,
1054 : : opt->copy_method,
1055 : 6206 : opt->debug,
1056 : 6206 : opt->dry_run);
1057 : : }
1058 : : else
1059 : : {
1060 : : /* Construct the path that the backup_manifest will use. */
1061 : 6370 : snprintf(manifest_path, MAXPGPATH, "%s%s", manifest_prefix,
1062 : 6370 : de->d_name);
1063 : :
1064 : : /*
1065 : : * It's not an incremental file, so we need to copy the entire
1066 : : * file to the output directory.
1067 : : *
1068 : : * If a checksum of the required type already exists in the
1069 : : * backup_manifest for the final input directory, we can save some
1070 : : * work by reusing that checksum instead of computing a new one.
1071 : : */
1072 [ + + + - ]: 6370 : if (checksum_type != CHECKSUM_TYPE_NONE &&
1073 : : latest_manifest != NULL)
1074 : : {
1075 : : manifest_file *mfile;
1076 : :
1077 : 4422 : mfile = manifest_files_lookup(latest_manifest->files,
1078 : : manifest_path);
1079 [ - + ]: 4422 : if (mfile == NULL)
1080 : : {
1081 : : char *bmpath;
1082 : :
1083 : : /*
1084 : : * The directory is out of sync with the backup_manifest,
1085 : : * so emit a warning.
1086 : : */
677 rhaas@postgresql.org 1087 :UBC 0 : bmpath = psprintf("%s/%s", input_directory,
1088 : : "backup_manifest");
426 peter@eisentraut.org 1089 : 0 : pg_log_warning("manifest file \"%s\" contains no entry for file \"%s\"",
1090 : : bmpath, manifest_path);
677 rhaas@postgresql.org 1091 : 0 : pfree(bmpath);
1092 : : }
677 rhaas@postgresql.org 1093 [ + + ]:CBC 4422 : else if (mfile->checksum_type == checksum_type)
1094 : : {
1095 : 3455 : checksum_length = mfile->checksum_length;
1096 : 3455 : checksum_payload = mfile->checksum_payload;
1097 : : }
1098 : : }
1099 : :
1100 : : /*
1101 : : * If we're reusing a checksum, then we don't need copy_file() to
1102 : : * compute one for us, but otherwise, it needs to compute whatever
1103 : : * type of checksum we need.
1104 : : */
1105 [ + + ]: 6370 : if (checksum_length != 0)
1106 : 3455 : pg_checksum_init(&checksum_ctx, CHECKSUM_TYPE_NONE);
1107 : : else
1108 : 2915 : pg_checksum_init(&checksum_ctx, checksum_type);
1109 : :
1110 : : /* Actually copy the file. */
1111 : 6370 : snprintf(ofullpath, MAXPGPATH, "%s/%s", ofulldir, de->d_name);
570 tomas.vondra@postgre 1112 : 6370 : copy_file(ifullpath, ofullpath, &checksum_ctx,
1113 : 6370 : opt->copy_method, opt->dry_run);
1114 : :
1115 : : /*
1116 : : * If copy_file() performed a checksum calculation for us, then
1117 : : * save the results (except in dry-run mode, when there's no
1118 : : * point).
1119 : : */
677 rhaas@postgresql.org 1120 [ + + + - ]: 6370 : if (checksum_ctx.type != CHECKSUM_TYPE_NONE && !opt->dry_run)
1121 : : {
1122 : 967 : checksum_payload = pg_malloc(PG_CHECKSUM_MAX_LENGTH);
1123 : 967 : checksum_length = pg_checksum_final(&checksum_ctx,
1124 : : checksum_payload);
1125 : : }
1126 : : }
1127 : :
1128 : : /* Generate manifest entry, if needed. */
1129 [ + + ]: 12575 : if (mwriter != NULL)
1130 : : {
1131 : : struct stat sb;
1132 : :
1133 : : /*
1134 : : * In order to generate a manifest entry, we need the file size
1135 : : * and mtime. We have no way to know the correct mtime except to
1136 : : * stat() the file, so just do that and get the size as well.
1137 : : *
1138 : : * If we didn't need the mtime here, we could try to obtain the
1139 : : * file size from the reconstruction or file copy process above,
1140 : : * although that is actually not convenient in all cases. If we
1141 : : * write the file ourselves then clearly we can keep a count of
1142 : : * bytes, but if we use something like CopyFile() then it's
1143 : : * trickier. Since we have to stat() anyway to get the mtime,
1144 : : * there's no point in worrying about it.
1145 : : */
1146 [ - + ]: 11607 : if (stat(ofullpath, &sb) < 0)
677 rhaas@postgresql.org 1147 :UBC 0 : pg_fatal("could not stat file \"%s\": %m", ofullpath);
1148 : :
1149 : : /* OK, now do the work. */
677 rhaas@postgresql.org 1150 :CBC 11607 : add_file_to_manifest(mwriter, manifest_path,
1151 : 11607 : sb.st_size, sb.st_mtime,
1152 : : checksum_type, checksum_length,
1153 : : checksum_payload);
1154 : : }
1155 : :
1156 : : /* Avoid leaking memory. */
1157 [ + + ]: 12575 : if (checksum_payload != NULL)
1158 : 10627 : pfree(checksum_payload);
1159 : : }
1160 : :
1161 : 338 : closedir(dir);
1162 : 338 : }
1163 : :
1164 : : /*
1165 : : * Add a directory to the list of output directories to clean up.
1166 : : */
1167 : : static void
1168 : 14 : remember_to_cleanup_directory(char *target_path, bool rmtopdir)
1169 : : {
1170 : 14 : cb_cleanup_dir *dir = pg_malloc(sizeof(cb_cleanup_dir));
1171 : :
1172 : 14 : dir->target_path = target_path;
1173 : 14 : dir->rmtopdir = rmtopdir;
1174 : 14 : dir->next = cleanup_dir_list;
1175 : 14 : cleanup_dir_list = dir;
1176 : 14 : }
1177 : :
1178 : : /*
1179 : : * Empty out the list of directories scheduled for cleanup at exit.
1180 : : *
1181 : : * We want to remove the output directories only on a failure, so call this
1182 : : * function when we know that the operation has succeeded.
1183 : : *
1184 : : * Since we only expect this to be called when we're about to exit, we could
1185 : : * just set cleanup_dir_list to NULL and be done with it, but we free the
1186 : : * memory to be tidy.
1187 : : */
1188 : : static void
1189 : 12 : reset_directory_cleanup_list(void)
1190 : : {
1191 [ + + ]: 25 : while (cleanup_dir_list != NULL)
1192 : : {
1193 : 13 : cb_cleanup_dir *dir = cleanup_dir_list;
1194 : :
1195 : 13 : cleanup_dir_list = cleanup_dir_list->next;
1196 : 13 : pfree(dir);
1197 : : }
1198 : 12 : }
1199 : :
1200 : : /*
1201 : : * Scan the pg_tblspc directory of the final input backup to get a canonical
1202 : : * list of what tablespaces are part of the backup.
1203 : : *
1204 : : * 'pathname' should be the path to the toplevel backup directory for the
1205 : : * final backup in the backup chain.
1206 : : */
1207 : : static cb_tablespace *
1208 : 13 : scan_for_existing_tablespaces(char *pathname, cb_options *opt)
1209 : : {
1210 : : char pg_tblspc[MAXPGPATH];
1211 : : DIR *dir;
1212 : : struct dirent *de;
1213 : 13 : cb_tablespace *tslist = NULL;
1214 : :
419 michael@paquier.xyz 1215 : 13 : snprintf(pg_tblspc, MAXPGPATH, "%s/%s", pathname, PG_TBLSPC_DIR);
677 rhaas@postgresql.org 1216 [ + + ]: 13 : pg_log_debug("scanning \"%s\"", pg_tblspc);
1217 : :
1218 [ - + ]: 13 : if ((dir = opendir(pg_tblspc)) == NULL)
655 rhaas@postgresql.org 1219 :UBC 0 : pg_fatal("could not open directory \"%s\": %m", pg_tblspc);
1220 : :
677 rhaas@postgresql.org 1221 [ + + ]:CBC 40 : while (errno = 0, (de = readdir(dir)) != NULL)
1222 : : {
1223 : : Oid oid;
1224 : : char tblspcdir[MAXPGPATH];
1225 : : char link_target[MAXPGPATH];
1226 : : int link_length;
1227 : : cb_tablespace *ts;
1228 : : cb_tablespace *otherts;
1229 : : PGFileType type;
1230 : :
1231 : : /* Silently ignore "." and ".." entries. */
1232 [ + + + + ]: 27 : if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
1233 : 26 : continue;
1234 : :
1235 : : /* Construct full pathname. */
1236 : 1 : snprintf(tblspcdir, MAXPGPATH, "%s/%s", pg_tblspc, de->d_name);
1237 : :
1238 : : /* Ignore any file name that doesn't look like a proper OID. */
1239 [ - + ]: 1 : if (!parse_oid(de->d_name, &oid))
1240 : : {
677 rhaas@postgresql.org 1241 [ # # ]:UBC 0 : pg_log_debug("skipping \"%s\" because the filename is not a legal tablespace OID",
1242 : : tblspcdir);
1243 : 0 : continue;
1244 : : }
1245 : :
1246 : : /* Only symbolic links and directories are tablespaces. */
677 rhaas@postgresql.org 1247 :CBC 1 : type = get_dirent_type(tblspcdir, de, false, PG_LOG_ERROR);
1248 [ - + ]: 1 : if (type == PGFILETYPE_ERROR)
677 rhaas@postgresql.org 1249 :UBC 0 : exit(1);
677 rhaas@postgresql.org 1250 [ - + - - ]:CBC 1 : if (type != PGFILETYPE_LNK && type != PGFILETYPE_DIR)
1251 : : {
677 rhaas@postgresql.org 1252 [ # # ]:UBC 0 : pg_log_debug("skipping \"%s\" because it is neither a symbolic link nor a directory",
1253 : : tblspcdir);
1254 : 0 : continue;
1255 : : }
1256 : :
1257 : : /* Create a new tablespace object. */
677 rhaas@postgresql.org 1258 :CBC 1 : ts = pg_malloc0(sizeof(cb_tablespace));
1259 : 1 : ts->oid = oid;
1260 : :
1261 : : /*
1262 : : * If it's a link, it's not an in-place tablespace. Otherwise, it must
1263 : : * be a directory, and thus an in-place tablespace.
1264 : : */
1265 [ + - ]: 1 : if (type == PGFILETYPE_LNK)
1266 : : {
1267 : : cb_tablespace_mapping *tsmap;
1268 : :
1269 : : /* Read the link target. */
1270 : 1 : link_length = readlink(tblspcdir, link_target, sizeof(link_target));
1271 [ - + ]: 1 : if (link_length < 0)
677 rhaas@postgresql.org 1272 :UBC 0 : pg_fatal("could not read symbolic link \"%s\": %m",
1273 : : tblspcdir);
677 rhaas@postgresql.org 1274 [ - + ]:CBC 1 : if (link_length >= sizeof(link_target))
493 peter@eisentraut.org 1275 :UBC 0 : pg_fatal("target of symbolic link \"%s\" is too long", tblspcdir);
677 rhaas@postgresql.org 1276 :CBC 1 : link_target[link_length] = '\0';
1277 [ - + ]: 1 : if (!is_absolute_path(link_target))
493 peter@eisentraut.org 1278 :UBC 0 : pg_fatal("target of symbolic link \"%s\" is relative", tblspcdir);
1279 : :
1280 : : /* Canonicalize the link target. */
677 rhaas@postgresql.org 1281 :CBC 1 : canonicalize_path(link_target);
1282 : :
1283 : : /*
1284 : : * Find the corresponding tablespace mapping and copy the relevant
1285 : : * details into the new tablespace entry.
1286 : : */
1287 [ + - ]: 1 : for (tsmap = opt->tsmappings; tsmap != NULL; tsmap = tsmap->next)
1288 : : {
1289 [ + - ]: 1 : if (strcmp(tsmap->old_dir, link_target) == 0)
1290 : : {
655 1291 : 1 : strlcpy(ts->old_dir, tsmap->old_dir, MAXPGPATH);
1292 : 1 : strlcpy(ts->new_dir, tsmap->new_dir, MAXPGPATH);
677 1293 : 1 : ts->in_place = false;
1294 : 1 : break;
1295 : : }
1296 : : }
1297 : :
1298 : : /* Every non-in-place tablespace must be mapped. */
1299 [ - + ]: 1 : if (tsmap == NULL)
677 rhaas@postgresql.org 1300 :UBC 0 : pg_fatal("tablespace at \"%s\" has no tablespace mapping",
1301 : : link_target);
1302 : : }
1303 : : else
1304 : : {
1305 : : /*
1306 : : * For an in-place tablespace, there's no separate directory, so
1307 : : * we just record the paths within the data directories.
1308 : : */
1309 : 0 : snprintf(ts->old_dir, MAXPGPATH, "%s/%s", pg_tblspc, de->d_name);
419 michael@paquier.xyz 1310 : 0 : snprintf(ts->new_dir, MAXPGPATH, "%s/%s/%s", opt->output,
1311 : 0 : PG_TBLSPC_DIR, de->d_name);
677 rhaas@postgresql.org 1312 : 0 : ts->in_place = true;
1313 : : }
1314 : :
1315 : : /* Tablespaces should not share a directory. */
677 rhaas@postgresql.org 1316 [ - + ]:CBC 1 : for (otherts = tslist; otherts != NULL; otherts = otherts->next)
677 rhaas@postgresql.org 1317 [ # # ]:UBC 0 : if (strcmp(ts->new_dir, otherts->new_dir) == 0)
493 peter@eisentraut.org 1318 : 0 : pg_fatal("tablespaces with OIDs %u and %u both point at directory \"%s\"",
1319 : : otherts->oid, oid, ts->new_dir);
1320 : :
1321 : : /* Add this tablespace to the list. */
677 rhaas@postgresql.org 1322 :CBC 1 : ts->next = tslist;
1323 : 1 : tslist = ts;
1324 : : }
1325 : :
655 1326 [ - + ]: 13 : if (closedir(dir) != 0)
655 rhaas@postgresql.org 1327 :UBC 0 : pg_fatal("could not close directory \"%s\": %m", pg_tblspc);
1328 : :
677 rhaas@postgresql.org 1329 :CBC 13 : return tslist;
1330 : : }
1331 : :
1332 : : /*
1333 : : * Read a file into a StringInfo.
1334 : : *
1335 : : * fd is used for the actual file I/O, filename for error reporting purposes.
1336 : : * A file longer than maxlen is a fatal error.
1337 : : */
1338 : : static void
1339 : 37 : slurp_file(int fd, char *filename, StringInfo buf, int maxlen)
1340 : : {
1341 : : struct stat st;
1342 : : ssize_t rb;
1343 : :
1344 : : /* Check file size, and complain if it's too large. */
1345 [ - + ]: 37 : if (fstat(fd, &st) != 0)
493 peter@eisentraut.org 1346 :UBC 0 : pg_fatal("could not stat file \"%s\": %m", filename);
677 rhaas@postgresql.org 1347 [ - + ]:CBC 37 : if (st.st_size > maxlen)
677 rhaas@postgresql.org 1348 :UBC 0 : pg_fatal("file \"%s\" is too large", filename);
1349 : :
1350 : : /* Make sure we have enough space. */
677 rhaas@postgresql.org 1351 :CBC 37 : enlargeStringInfo(buf, st.st_size);
1352 : :
1353 : : /* Read the data. */
1354 : 37 : rb = read(fd, &buf->data[buf->len], st.st_size);
1355 : :
1356 : : /*
1357 : : * We don't expect any concurrent changes, so we should read exactly the
1358 : : * expected number of bytes.
1359 : : */
1360 [ - + ]: 37 : if (rb != st.st_size)
1361 : : {
677 rhaas@postgresql.org 1362 [ # # ]:UBC 0 : if (rb < 0)
1363 : 0 : pg_fatal("could not read file \"%s\": %m", filename);
1364 : : else
493 peter@eisentraut.org 1365 : 0 : pg_fatal("could not read file \"%s\": read %zd of %lld",
1366 : : filename, rb, (long long int) st.st_size);
1367 : : }
1368 : :
1369 : : /* Adjust buffer length for new data and restore trailing-\0 invariant */
677 rhaas@postgresql.org 1370 :CBC 37 : buf->len += rb;
1371 : 37 : buf->data[buf->len] = '\0';
1372 : 37 : }
|