Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * pg_basebackup.c - receive a base backup using streaming replication protocol
4 : *
5 : * Author: Magnus Hagander <magnus@hagander.net>
6 : *
7 : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 : *
9 : * IDENTIFICATION
10 : * src/bin/pg_basebackup/pg_basebackup.c
11 : *-------------------------------------------------------------------------
12 : */
13 :
14 : #include "postgres_fe.h"
15 :
16 : #include <unistd.h>
17 : #include <dirent.h>
18 : #include <limits.h>
19 : #include <sys/select.h>
20 : #include <sys/stat.h>
21 : #include <sys/wait.h>
22 : #include <signal.h>
23 : #include <time.h>
24 : #ifdef HAVE_LIBZ
25 : #include <zlib.h>
26 : #endif
27 :
28 : #include "access/xlog_internal.h"
29 : #include "astreamer_inject.h"
30 : #include "backup/basebackup.h"
31 : #include "common/compression.h"
32 : #include "common/file_perm.h"
33 : #include "common/file_utils.h"
34 : #include "common/logging.h"
35 : #include "fe_utils/option_utils.h"
36 : #include "fe_utils/recovery_gen.h"
37 : #include "getopt_long.h"
38 : #include "receivelog.h"
39 : #include "streamutil.h"
40 :
41 : #define ERRCODE_DATA_CORRUPTED "XX001"
42 :
43 : typedef struct TablespaceListCell
44 : {
45 : struct TablespaceListCell *next;
46 : char old_dir[MAXPGPATH];
47 : char new_dir[MAXPGPATH];
48 : } TablespaceListCell;
49 :
50 : typedef struct TablespaceList
51 : {
52 : TablespaceListCell *head;
53 : TablespaceListCell *tail;
54 : } TablespaceList;
55 :
56 : typedef struct ArchiveStreamState
57 : {
58 : int tablespacenum;
59 : pg_compress_specification *compress;
60 : astreamer *streamer;
61 : astreamer *manifest_inject_streamer;
62 : PQExpBuffer manifest_buffer;
63 : char manifest_filename[MAXPGPATH];
64 : FILE *manifest_file;
65 : } ArchiveStreamState;
66 :
67 : typedef struct WriteTarState
68 : {
69 : int tablespacenum;
70 : astreamer *streamer;
71 : } WriteTarState;
72 :
73 : typedef struct WriteManifestState
74 : {
75 : char filename[MAXPGPATH];
76 : FILE *file;
77 : } WriteManifestState;
78 :
79 : typedef void (*WriteDataCallback) (size_t nbytes, char *buf,
80 : void *callback_data);
81 :
82 : /*
83 : * pg_xlog has been renamed to pg_wal in version 10. This version number
84 : * should be compared with PQserverVersion().
85 : */
86 : #define MINIMUM_VERSION_FOR_PG_WAL 100000
87 :
88 : /*
89 : * Temporary replication slots are supported from version 10.
90 : */
91 : #define MINIMUM_VERSION_FOR_TEMP_SLOTS 100000
92 :
93 : /*
94 : * Backup manifests are supported from version 13.
95 : */
96 : #define MINIMUM_VERSION_FOR_MANIFESTS 130000
97 :
98 : /*
99 : * Before v15, tar files received from the server will be improperly
100 : * terminated.
101 : */
102 : #define MINIMUM_VERSION_FOR_TERMINATED_TARFILE 150000
103 :
104 : /*
105 : * pg_wal/summaries exists beginning with version 17.
106 : */
107 : #define MINIMUM_VERSION_FOR_WAL_SUMMARIES 170000
108 :
109 : /*
110 : * Different ways to include WAL
111 : */
112 : typedef enum
113 : {
114 : NO_WAL,
115 : FETCH_WAL,
116 : STREAM_WAL,
117 : } IncludeWal;
118 :
119 : /*
120 : * Different places to perform compression
121 : */
122 : typedef enum
123 : {
124 : COMPRESS_LOCATION_UNSPECIFIED,
125 : COMPRESS_LOCATION_CLIENT,
126 : COMPRESS_LOCATION_SERVER,
127 : } CompressionLocation;
128 :
129 : /* Global options */
130 : static char *basedir = NULL;
131 : static TablespaceList tablespace_dirs = {NULL, NULL};
132 : static char *xlog_dir = NULL;
133 : static char format = '\0'; /* p(lain)/t(ar) */
134 : static char *label = "pg_basebackup base backup";
135 : static bool noclean = false;
136 : static bool checksum_failure = false;
137 : static bool showprogress = false;
138 : static bool estimatesize = true;
139 : static int verbose = 0;
140 : static IncludeWal includewal = STREAM_WAL;
141 : static bool fastcheckpoint = false;
142 : static bool writerecoveryconf = false;
143 : static bool do_sync = true;
144 : static int standby_message_timeout = 10 * 1000; /* 10 sec = default */
145 : static pg_time_t last_progress_report = 0;
146 : static int32 maxrate = 0; /* no limit by default */
147 : static char *replication_slot = NULL;
148 : static bool temp_replication_slot = true;
149 : static char *backup_target = NULL;
150 : static bool create_slot = false;
151 : static bool no_slot = false;
152 : static bool verify_checksums = true;
153 : static bool manifest = true;
154 : static bool manifest_force_encode = false;
155 : static char *manifest_checksums = NULL;
156 : static DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
157 :
158 : static bool success = false;
159 : static bool made_new_pgdata = false;
160 : static bool found_existing_pgdata = false;
161 : static bool made_new_xlogdir = false;
162 : static bool found_existing_xlogdir = false;
163 : static bool made_tablespace_dirs = false;
164 : static bool found_tablespace_dirs = false;
165 :
166 : /* Progress indicators */
167 : static uint64 totalsize_kb;
168 : static uint64 totaldone;
169 : static int tablespacecount;
170 : static char *progress_filename = NULL;
171 :
172 : /* Pipe to communicate with background wal receiver process */
173 : #ifndef WIN32
174 : static int bgpipe[2] = {-1, -1};
175 : #endif
176 :
177 : /* Handle to child process */
178 : static pid_t bgchild = -1;
179 : static bool in_log_streamer = false;
180 :
181 : /* Flag to indicate if child process exited unexpectedly */
182 : static volatile sig_atomic_t bgchild_exited = false;
183 :
184 : /* End position for xlog streaming, empty string if unknown yet */
185 : static XLogRecPtr xlogendptr;
186 :
187 : #ifndef WIN32
188 : static int has_xlogendptr = 0;
189 : #else
190 : static volatile LONG has_xlogendptr = 0;
191 : #endif
192 :
193 : /* Contents of configuration file to be generated */
194 : static PQExpBuffer recoveryconfcontents = NULL;
195 :
196 : /* Function headers */
197 : static void usage(void);
198 : static void verify_dir_is_empty_or_create(char *dirname, bool *created, bool *found);
199 : static void progress_update_filename(const char *filename);
200 : static void progress_report(int tablespacenum, bool force, bool finished);
201 :
202 : static astreamer *CreateBackupStreamer(char *archive_name, char *spclocation,
203 : astreamer **manifest_inject_streamer_p,
204 : bool is_recovery_guc_supported,
205 : bool expect_unterminated_tarfile,
206 : pg_compress_specification *compress);
207 : static void ReceiveArchiveStreamChunk(size_t r, char *copybuf,
208 : void *callback_data);
209 : static char GetCopyDataByte(size_t r, char *copybuf, size_t *cursor);
210 : static char *GetCopyDataString(size_t r, char *copybuf, size_t *cursor);
211 : static uint64 GetCopyDataUInt64(size_t r, char *copybuf, size_t *cursor);
212 : static void GetCopyDataEnd(size_t r, char *copybuf, size_t cursor);
213 : static void ReportCopyDataParseError(size_t r, char *copybuf);
214 : static void ReceiveTarFile(PGconn *conn, char *archive_name, char *spclocation,
215 : bool tablespacenum, pg_compress_specification *compress);
216 : static void ReceiveTarCopyChunk(size_t r, char *copybuf, void *callback_data);
217 : static void ReceiveBackupManifest(PGconn *conn);
218 : static void ReceiveBackupManifestChunk(size_t r, char *copybuf,
219 : void *callback_data);
220 : static void ReceiveBackupManifestInMemory(PGconn *conn, PQExpBuffer buf);
221 : static void ReceiveBackupManifestInMemoryChunk(size_t r, char *copybuf,
222 : void *callback_data);
223 : static void BaseBackup(char *compression_algorithm, char *compression_detail,
224 : CompressionLocation compressloc,
225 : pg_compress_specification *client_compress,
226 : char *incremental_manifest);
227 :
228 : static bool reached_end_position(XLogRecPtr segendpos, uint32 timeline,
229 : bool segment_finished);
230 :
231 : static const char *get_tablespace_mapping(const char *dir);
232 : static void tablespace_list_append(const char *arg);
233 :
234 :
235 : static void
236 682 : cleanup_directories_atexit(void)
237 : {
238 682 : if (success || in_log_streamer)
239 568 : return;
240 :
241 114 : if (!noclean && !checksum_failure)
242 : {
243 106 : if (made_new_pgdata)
244 : {
245 38 : pg_log_info("removing data directory \"%s\"", basedir);
246 38 : if (!rmtree(basedir, true))
247 0 : pg_log_error("failed to remove data directory");
248 : }
249 68 : else if (found_existing_pgdata)
250 : {
251 0 : pg_log_info("removing contents of data directory \"%s\"", basedir);
252 0 : if (!rmtree(basedir, false))
253 0 : pg_log_error("failed to remove contents of data directory");
254 : }
255 :
256 106 : if (made_new_xlogdir)
257 : {
258 0 : pg_log_info("removing WAL directory \"%s\"", xlog_dir);
259 0 : if (!rmtree(xlog_dir, true))
260 0 : pg_log_error("failed to remove WAL directory");
261 : }
262 106 : else if (found_existing_xlogdir)
263 : {
264 0 : pg_log_info("removing contents of WAL directory \"%s\"", xlog_dir);
265 0 : if (!rmtree(xlog_dir, false))
266 0 : pg_log_error("failed to remove contents of WAL directory");
267 : }
268 : }
269 : else
270 : {
271 8 : if ((made_new_pgdata || found_existing_pgdata) && !checksum_failure)
272 0 : pg_log_info("data directory \"%s\" not removed at user's request", basedir);
273 :
274 8 : if (made_new_xlogdir || found_existing_xlogdir)
275 0 : pg_log_info("WAL directory \"%s\" not removed at user's request", xlog_dir);
276 : }
277 :
278 114 : if ((made_tablespace_dirs || found_tablespace_dirs) && !checksum_failure)
279 0 : pg_log_info("changes to tablespace directories will not be undone");
280 : }
281 :
282 : static void
283 622 : disconnect_atexit(void)
284 : {
285 622 : if (conn != NULL)
286 318 : PQfinish(conn);
287 622 : }
288 :
289 : #ifndef WIN32
290 : /*
291 : * If the bgchild exits prematurely and raises a SIGCHLD signal, we can abort
292 : * processing rather than wait until the backup has finished and error out at
293 : * that time. On Windows, we use a background thread which can communicate
294 : * without the need for a signal handler.
295 : */
296 : static void
297 264 : sigchld_handler(SIGNAL_ARGS)
298 : {
299 264 : bgchild_exited = true;
300 264 : }
301 :
302 : /*
303 : * On windows, our background thread dies along with the process. But on
304 : * Unix, if we have started a subprocess, we want to kill it off so it
305 : * doesn't remain running trying to stream data.
306 : */
307 : static void
308 272 : kill_bgchild_atexit(void)
309 : {
310 272 : if (bgchild > 0 && !bgchild_exited)
311 8 : kill(bgchild, SIGTERM);
312 272 : }
313 : #endif
314 :
315 : /*
316 : * Split argument into old_dir and new_dir and append to tablespace mapping
317 : * list.
318 : */
319 : static void
320 44 : tablespace_list_append(const char *arg)
321 : {
322 44 : TablespaceListCell *cell = (TablespaceListCell *) pg_malloc0(sizeof(TablespaceListCell));
323 : char *dst;
324 : char *dst_ptr;
325 : const char *arg_ptr;
326 :
327 44 : dst_ptr = dst = cell->old_dir;
328 1528 : for (arg_ptr = arg; *arg_ptr; arg_ptr++)
329 : {
330 1486 : if (dst_ptr - dst >= MAXPGPATH)
331 0 : pg_fatal("directory name too long");
332 :
333 1486 : if (*arg_ptr == '\\' && *(arg_ptr + 1) == '=')
334 : ; /* skip backslash escaping = */
335 1482 : else if (*arg_ptr == '=' && (arg_ptr == arg || *(arg_ptr - 1) != '\\'))
336 : {
337 44 : if (*cell->new_dir)
338 2 : pg_fatal("multiple \"=\" signs in tablespace mapping");
339 : else
340 42 : dst = dst_ptr = cell->new_dir;
341 : }
342 : else
343 1438 : *dst_ptr++ = *arg_ptr;
344 : }
345 :
346 42 : if (!*cell->old_dir || !*cell->new_dir)
347 6 : pg_fatal("invalid tablespace mapping format \"%s\", must be \"OLDDIR=NEWDIR\"", arg);
348 :
349 : /*
350 : * All tablespaces are created with absolute directories, so specifying a
351 : * non-absolute path here would just never match, possibly confusing
352 : * users. Since we don't know whether the remote side is Windows or not,
353 : * and it might be different than the local side, permit any path that
354 : * could be absolute under either set of rules.
355 : *
356 : * (There is little practical risk of confusion here, because someone
357 : * running entirely on Linux isn't likely to have a relative path that
358 : * begins with a backslash or something that looks like a drive
359 : * specification. If they do, and they also incorrectly believe that a
360 : * relative path is acceptable here, we'll silently fail to warn them of
361 : * their mistake, and the -T option will just not get applied, same as if
362 : * they'd specified -T for a nonexistent tablespace.)
363 : */
364 36 : if (!is_nonwindows_absolute_path(cell->old_dir) &&
365 2 : !is_windows_absolute_path(cell->old_dir))
366 2 : pg_fatal("old directory is not an absolute path in tablespace mapping: %s",
367 : cell->old_dir);
368 :
369 34 : if (!is_absolute_path(cell->new_dir))
370 2 : pg_fatal("new directory is not an absolute path in tablespace mapping: %s",
371 : cell->new_dir);
372 :
373 : /*
374 : * Comparisons done with these values should involve similarly
375 : * canonicalized path values. This is particularly sensitive on Windows
376 : * where path values may not necessarily use Unix slashes.
377 : */
378 32 : canonicalize_path(cell->old_dir);
379 32 : canonicalize_path(cell->new_dir);
380 :
381 32 : if (tablespace_dirs.tail)
382 0 : tablespace_dirs.tail->next = cell;
383 : else
384 32 : tablespace_dirs.head = cell;
385 32 : tablespace_dirs.tail = cell;
386 32 : }
387 :
388 :
389 : static void
390 2 : usage(void)
391 : {
392 2 : printf(_("%s takes a base backup of a running PostgreSQL server.\n\n"),
393 : progname);
394 2 : printf(_("Usage:\n"));
395 2 : printf(_(" %s [OPTION]...\n"), progname);
396 2 : printf(_("\nOptions controlling the output:\n"));
397 2 : printf(_(" -D, --pgdata=DIRECTORY receive base backup into directory\n"));
398 2 : printf(_(" -F, --format=p|t output format (plain (default), tar)\n"));
399 2 : printf(_(" -i, --incremental=OLDMANIFEST\n"
400 : " take incremental backup\n"));
401 2 : printf(_(" -r, --max-rate=RATE maximum transfer rate to transfer data directory\n"
402 : " (in kB/s, or use suffix \"k\" or \"M\")\n"));
403 2 : printf(_(" -R, --write-recovery-conf\n"
404 : " write configuration for replication\n"));
405 2 : printf(_(" -t, --target=TARGET[:DETAIL]\n"
406 : " backup target (if other than client)\n"));
407 2 : printf(_(" -T, --tablespace-mapping=OLDDIR=NEWDIR\n"
408 : " relocate tablespace in OLDDIR to NEWDIR\n"));
409 2 : printf(_(" --waldir=WALDIR location for the write-ahead log directory\n"));
410 2 : printf(_(" -X, --wal-method=none|fetch|stream\n"
411 : " include required WAL files with specified method\n"));
412 2 : printf(_(" -z, --gzip compress tar output\n"));
413 2 : printf(_(" -Z, --compress=[{client|server}-]METHOD[:DETAIL]\n"
414 : " compress on client or server as specified\n"));
415 2 : printf(_(" -Z, --compress=none do not compress tar output\n"));
416 2 : printf(_("\nGeneral options:\n"));
417 2 : printf(_(" -c, --checkpoint=fast|spread\n"
418 : " set fast or spread (default) checkpointing\n"));
419 2 : printf(_(" -C, --create-slot create replication slot\n"));
420 2 : printf(_(" -l, --label=LABEL set backup label\n"));
421 2 : printf(_(" -n, --no-clean do not clean up after errors\n"));
422 2 : printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n"));
423 2 : printf(_(" -P, --progress show progress information\n"));
424 2 : printf(_(" -S, --slot=SLOTNAME replication slot to use\n"));
425 2 : printf(_(" -v, --verbose output verbose messages\n"));
426 2 : printf(_(" -V, --version output version information, then exit\n"));
427 2 : printf(_(" --manifest-checksums=SHA{224,256,384,512}|CRC32C|NONE\n"
428 : " use algorithm for manifest checksums\n"));
429 2 : printf(_(" --manifest-force-encode\n"
430 : " hex encode all file names in manifest\n"));
431 2 : printf(_(" --no-estimate-size do not estimate backup size in server side\n"));
432 2 : printf(_(" --no-manifest suppress generation of backup manifest\n"));
433 2 : printf(_(" --no-slot prevent creation of temporary replication slot\n"));
434 2 : printf(_(" --no-verify-checksums\n"
435 : " do not verify checksums\n"));
436 2 : printf(_(" --sync-method=METHOD\n"
437 : " set method for syncing files to disk\n"));
438 2 : printf(_(" -?, --help show this help, then exit\n"));
439 2 : printf(_("\nConnection options:\n"));
440 2 : printf(_(" -d, --dbname=CONNSTR connection string\n"));
441 2 : printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
442 2 : printf(_(" -p, --port=PORT database server port number\n"));
443 2 : printf(_(" -s, --status-interval=INTERVAL\n"
444 : " time between status packets sent to server (in seconds)\n"));
445 2 : printf(_(" -U, --username=NAME connect as specified database user\n"));
446 2 : printf(_(" -w, --no-password never prompt for password\n"));
447 2 : printf(_(" -W, --password force password prompt (should happen automatically)\n"));
448 2 : printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
449 2 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
450 2 : }
451 :
452 :
453 : /*
454 : * Called in the background process every time data is received.
455 : * On Unix, we check to see if there is any data on our pipe
456 : * (which would mean we have a stop position), and if it is, check if
457 : * it is time to stop.
458 : * On Windows, we are in a single process, so we can just check if it's
459 : * time to stop.
460 : */
461 : static bool
462 11268 : reached_end_position(XLogRecPtr segendpos, uint32 timeline,
463 : bool segment_finished)
464 : {
465 11268 : if (!has_xlogendptr)
466 : {
467 : #ifndef WIN32
468 : fd_set fds;
469 10932 : struct timeval tv = {0};
470 : int r;
471 :
472 : /*
473 : * Don't have the end pointer yet - check our pipe to see if it has
474 : * been sent yet.
475 : */
476 10932 : FD_ZERO(&fds);
477 10932 : FD_SET(bgpipe[0], &fds);
478 :
479 10932 : r = select(bgpipe[0] + 1, &fds, NULL, NULL, &tv);
480 10932 : if (r == 1)
481 : {
482 260 : char xlogend[64] = {0};
483 : uint32 hi,
484 : lo;
485 :
486 260 : r = read(bgpipe[0], xlogend, sizeof(xlogend) - 1);
487 260 : if (r < 0)
488 0 : pg_fatal("could not read from ready pipe: %m");
489 :
490 260 : if (sscanf(xlogend, "%X/%X", &hi, &lo) != 2)
491 0 : pg_fatal("could not parse write-ahead log location \"%s\"",
492 : xlogend);
493 260 : xlogendptr = ((uint64) hi) << 32 | lo;
494 260 : has_xlogendptr = 1;
495 :
496 : /*
497 : * Fall through to check if we've reached the point further
498 : * already.
499 : */
500 : }
501 : else
502 : {
503 : /*
504 : * No data received on the pipe means we don't know the end
505 : * position yet - so just say it's not time to stop yet.
506 : */
507 10672 : return false;
508 : }
509 : #else
510 :
511 : /*
512 : * On win32, has_xlogendptr is set by the main thread, so if it's not
513 : * set here, we just go back and wait until it shows up.
514 : */
515 : return false;
516 : #endif
517 : }
518 :
519 : /*
520 : * At this point we have an end pointer, so compare it to the current
521 : * position to figure out if it's time to stop.
522 : */
523 596 : if (segendpos >= xlogendptr)
524 520 : return true;
525 :
526 : /*
527 : * Have end pointer, but haven't reached it yet - so tell the caller to
528 : * keep streaming.
529 : */
530 76 : return false;
531 : }
532 :
533 : typedef struct
534 : {
535 : PGconn *bgconn;
536 : XLogRecPtr startptr;
537 : char xlog[MAXPGPATH]; /* directory or tarfile depending on mode */
538 : char *sysidentifier;
539 : int timeline;
540 : pg_compress_algorithm wal_compress_algorithm;
541 : int wal_compress_level;
542 : } logstreamer_param;
543 :
544 : static int
545 264 : LogStreamerMain(logstreamer_param *param)
546 : {
547 264 : StreamCtl stream = {0};
548 :
549 264 : in_log_streamer = true;
550 :
551 264 : stream.startpos = param->startptr;
552 264 : stream.timeline = param->timeline;
553 264 : stream.sysidentifier = param->sysidentifier;
554 264 : stream.stream_stop = reached_end_position;
555 : #ifndef WIN32
556 264 : stream.stop_socket = bgpipe[0];
557 : #else
558 : stream.stop_socket = PGINVALID_SOCKET;
559 : #endif
560 264 : stream.standby_message_timeout = standby_message_timeout;
561 264 : stream.synchronous = false;
562 : /* fsync happens at the end of pg_basebackup for all data */
563 264 : stream.do_sync = false;
564 264 : stream.mark_done = true;
565 264 : stream.partial_suffix = NULL;
566 264 : stream.replication_slot = replication_slot;
567 264 : if (format == 'p')
568 238 : stream.walmethod = CreateWalDirectoryMethod(param->xlog,
569 : PG_COMPRESSION_NONE, 0,
570 238 : stream.do_sync);
571 : else
572 26 : stream.walmethod = CreateWalTarMethod(param->xlog,
573 : param->wal_compress_algorithm,
574 : param->wal_compress_level,
575 26 : stream.do_sync);
576 :
577 264 : if (!ReceiveXlogStream(param->bgconn, &stream))
578 : {
579 : /*
580 : * Any errors will already have been reported in the function process,
581 : * but we need to tell the parent that we didn't shutdown in a nice
582 : * way.
583 : */
584 : #ifdef WIN32
585 : /*
586 : * In order to signal the main thread of an ungraceful exit we set the
587 : * same flag that we use on Unix to signal SIGCHLD.
588 : */
589 : bgchild_exited = true;
590 : #endif
591 4 : return 1;
592 : }
593 :
594 260 : if (!stream.walmethod->ops->finish(stream.walmethod))
595 : {
596 0 : pg_log_error("could not finish writing WAL files: %m");
597 : #ifdef WIN32
598 : bgchild_exited = true;
599 : #endif
600 0 : return 1;
601 : }
602 :
603 260 : PQfinish(param->bgconn);
604 :
605 260 : stream.walmethod->ops->free(stream.walmethod);
606 :
607 260 : return 0;
608 : }
609 :
610 : /*
611 : * Initiate background process for receiving xlog during the backup.
612 : * The background stream will use its own database connection so we can
613 : * stream the logfile in parallel with the backups.
614 : */
615 : static void
616 274 : StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier,
617 : pg_compress_algorithm wal_compress_algorithm,
618 : int wal_compress_level)
619 : {
620 : logstreamer_param *param;
621 : uint32 hi,
622 : lo;
623 : char statusdir[MAXPGPATH];
624 :
625 274 : param = pg_malloc0(sizeof(logstreamer_param));
626 274 : param->timeline = timeline;
627 274 : param->sysidentifier = sysidentifier;
628 274 : param->wal_compress_algorithm = wal_compress_algorithm;
629 274 : param->wal_compress_level = wal_compress_level;
630 :
631 : /* Convert the starting position */
632 274 : if (sscanf(startpos, "%X/%X", &hi, &lo) != 2)
633 0 : pg_fatal("could not parse write-ahead log location \"%s\"",
634 : startpos);
635 274 : param->startptr = ((uint64) hi) << 32 | lo;
636 : /* Round off to even segment position */
637 274 : param->startptr -= XLogSegmentOffset(param->startptr, WalSegSz);
638 :
639 : #ifndef WIN32
640 : /* Create our background pipe */
641 274 : if (pipe(bgpipe) < 0)
642 0 : pg_fatal("could not create pipe for background process: %m");
643 : #endif
644 :
645 : /* Get a second connection */
646 274 : param->bgconn = GetConnection();
647 274 : if (!param->bgconn)
648 : /* Error message already written in GetConnection() */
649 0 : exit(1);
650 :
651 : /* In post-10 cluster, pg_xlog has been renamed to pg_wal */
652 274 : snprintf(param->xlog, sizeof(param->xlog), "%s/%s",
653 : basedir,
654 274 : PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ?
655 : "pg_xlog" : "pg_wal");
656 :
657 : /* Temporary replication slots are only supported in 10 and newer */
658 274 : if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_TEMP_SLOTS)
659 0 : temp_replication_slot = false;
660 :
661 : /*
662 : * Create replication slot if requested
663 : */
664 274 : if (temp_replication_slot && !replication_slot)
665 262 : replication_slot = psprintf("pg_basebackup_%u",
666 262 : (unsigned int) PQbackendPID(param->bgconn));
667 274 : if (temp_replication_slot || create_slot)
668 : {
669 266 : if (!CreateReplicationSlot(param->bgconn, replication_slot, NULL,
670 : temp_replication_slot, true, true, false,
671 : false, false))
672 2 : exit(1);
673 :
674 264 : if (verbose)
675 : {
676 0 : if (temp_replication_slot)
677 0 : pg_log_info("created temporary replication slot \"%s\"",
678 : replication_slot);
679 : else
680 0 : pg_log_info("created replication slot \"%s\"",
681 : replication_slot);
682 : }
683 : }
684 :
685 272 : if (format == 'p')
686 : {
687 : /*
688 : * Create pg_wal/archive_status or pg_xlog/archive_status (and thus
689 : * pg_wal or pg_xlog) depending on the target server so we can write
690 : * to basedir/pg_wal or basedir/pg_xlog as the directory entry in the
691 : * tar file may arrive later.
692 : */
693 244 : snprintf(statusdir, sizeof(statusdir), "%s/%s/archive_status",
694 : basedir,
695 244 : PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ?
696 : "pg_xlog" : "pg_wal");
697 :
698 244 : if (pg_mkdir_p(statusdir, pg_dir_create_mode) != 0 && errno != EEXIST)
699 0 : pg_fatal("could not create directory \"%s\": %m", statusdir);
700 :
701 : /*
702 : * For newer server versions, likewise create pg_wal/summaries
703 : */
704 244 : if (PQserverVersion(conn) >= MINIMUM_VERSION_FOR_WAL_SUMMARIES)
705 : {
706 : char summarydir[MAXPGPATH];
707 :
708 244 : snprintf(summarydir, sizeof(summarydir), "%s/%s/summaries",
709 : basedir, "pg_wal");
710 :
711 244 : if (pg_mkdir_p(summarydir, pg_dir_create_mode) != 0 &&
712 0 : errno != EEXIST)
713 0 : pg_fatal("could not create directory \"%s\": %m", summarydir);
714 : }
715 : }
716 :
717 : /*
718 : * Start a child process and tell it to start streaming. On Unix, this is
719 : * a fork(). On Windows, we create a thread.
720 : */
721 : #ifndef WIN32
722 272 : bgchild = fork();
723 536 : if (bgchild == 0)
724 : {
725 : /* in child process */
726 264 : exit(LogStreamerMain(param));
727 : }
728 272 : else if (bgchild < 0)
729 0 : pg_fatal("could not create background process: %m");
730 :
731 : /*
732 : * Else we are in the parent process and all is well.
733 : */
734 272 : atexit(kill_bgchild_atexit);
735 : #else /* WIN32 */
736 : bgchild = _beginthreadex(NULL, 0, (void *) LogStreamerMain, param, 0, NULL);
737 : if (bgchild == 0)
738 : pg_fatal("could not create background thread: %m");
739 : #endif
740 272 : }
741 :
742 : /*
743 : * Verify that the given directory exists and is empty. If it does not
744 : * exist, it is created. If it exists but is not empty, an error will
745 : * be given and the process ended.
746 : */
747 : static void
748 396 : verify_dir_is_empty_or_create(char *dirname, bool *created, bool *found)
749 : {
750 396 : switch (pg_check_dir(dirname))
751 : {
752 366 : case 0:
753 :
754 : /*
755 : * Does not exist, so create
756 : */
757 366 : if (pg_mkdir_p(dirname, pg_dir_create_mode) == -1)
758 0 : pg_fatal("could not create directory \"%s\": %m", dirname);
759 366 : if (created)
760 366 : *created = true;
761 366 : return;
762 28 : case 1:
763 :
764 : /*
765 : * Exists, empty
766 : */
767 28 : if (found)
768 28 : *found = true;
769 28 : return;
770 2 : case 2:
771 : case 3:
772 : case 4:
773 :
774 : /*
775 : * Exists, not empty
776 : */
777 2 : pg_fatal("directory \"%s\" exists but is not empty", dirname);
778 0 : case -1:
779 :
780 : /*
781 : * Access problem
782 : */
783 0 : pg_fatal("could not access directory \"%s\": %m", dirname);
784 : }
785 : }
786 :
787 : /*
788 : * Callback to update our notion of the current filename.
789 : *
790 : * No other code should modify progress_filename!
791 : */
792 : static void
793 269788 : progress_update_filename(const char *filename)
794 : {
795 : /* We needn't maintain this variable if not doing verbose reports. */
796 269788 : if (showprogress && verbose)
797 : {
798 0 : free(progress_filename);
799 0 : if (filename)
800 0 : progress_filename = pg_strdup(filename);
801 : else
802 0 : progress_filename = NULL;
803 : }
804 269788 : }
805 :
806 : /*
807 : * Print a progress report based on the global variables. If verbose output
808 : * is enabled, also print the current file name.
809 : *
810 : * Progress report is written at maximum once per second, unless the force
811 : * parameter is set to true.
812 : *
813 : * If finished is set to true, this is the last progress report. The cursor
814 : * is moved to the next line.
815 : */
816 : static void
817 476 : progress_report(int tablespacenum, bool force, bool finished)
818 : {
819 : int percent;
820 : char totaldone_str[32];
821 : char totalsize_str[32];
822 : pg_time_t now;
823 :
824 476 : if (!showprogress)
825 476 : return;
826 :
827 0 : now = time(NULL);
828 0 : if (now == last_progress_report && !force && !finished)
829 0 : return; /* Max once per second */
830 :
831 0 : last_progress_report = now;
832 0 : percent = totalsize_kb ? (int) ((totaldone / 1024) * 100 / totalsize_kb) : 0;
833 :
834 : /*
835 : * Avoid overflowing past 100% or the full size. This may make the total
836 : * size number change as we approach the end of the backup (the estimate
837 : * will always be wrong if WAL is included), but that's better than having
838 : * the done column be bigger than the total.
839 : */
840 0 : if (percent > 100)
841 0 : percent = 100;
842 0 : if (totaldone / 1024 > totalsize_kb)
843 0 : totalsize_kb = totaldone / 1024;
844 :
845 0 : snprintf(totaldone_str, sizeof(totaldone_str), UINT64_FORMAT,
846 : totaldone / 1024);
847 0 : snprintf(totalsize_str, sizeof(totalsize_str), UINT64_FORMAT, totalsize_kb);
848 :
849 : #define VERBOSE_FILENAME_LENGTH 35
850 0 : if (verbose)
851 : {
852 0 : if (!progress_filename)
853 :
854 : /*
855 : * No filename given, so clear the status line (used for last
856 : * call)
857 : */
858 0 : fprintf(stderr,
859 0 : ngettext("%*s/%s kB (100%%), %d/%d tablespace %*s",
860 : "%*s/%s kB (100%%), %d/%d tablespaces %*s",
861 : tablespacecount),
862 0 : (int) strlen(totalsize_str),
863 : totaldone_str, totalsize_str,
864 : tablespacenum, tablespacecount,
865 : VERBOSE_FILENAME_LENGTH + 5, "");
866 : else
867 : {
868 0 : bool truncate = (strlen(progress_filename) > VERBOSE_FILENAME_LENGTH);
869 :
870 0 : fprintf(stderr,
871 0 : ngettext("%*s/%s kB (%d%%), %d/%d tablespace (%s%-*.*s)",
872 : "%*s/%s kB (%d%%), %d/%d tablespaces (%s%-*.*s)",
873 : tablespacecount),
874 0 : (int) strlen(totalsize_str),
875 : totaldone_str, totalsize_str, percent,
876 : tablespacenum, tablespacecount,
877 : /* Prefix with "..." if we do leading truncation */
878 : truncate ? "..." : "",
879 : truncate ? VERBOSE_FILENAME_LENGTH - 3 : VERBOSE_FILENAME_LENGTH,
880 : truncate ? VERBOSE_FILENAME_LENGTH - 3 : VERBOSE_FILENAME_LENGTH,
881 : /* Truncate filename at beginning if it's too long */
882 0 : truncate ? progress_filename + strlen(progress_filename) - VERBOSE_FILENAME_LENGTH + 3 : progress_filename);
883 : }
884 : }
885 : else
886 0 : fprintf(stderr,
887 0 : ngettext("%*s/%s kB (%d%%), %d/%d tablespace",
888 : "%*s/%s kB (%d%%), %d/%d tablespaces",
889 : tablespacecount),
890 0 : (int) strlen(totalsize_str),
891 : totaldone_str, totalsize_str, percent,
892 : tablespacenum, tablespacecount);
893 :
894 : /*
895 : * Stay on the same line if reporting to a terminal and we're not done
896 : * yet.
897 : */
898 0 : fputc((!finished && isatty(fileno(stderr))) ? '\r' : '\n', stderr);
899 : }
900 :
901 : static int32
902 2 : parse_max_rate(char *src)
903 : {
904 : double result;
905 : char *after_num;
906 2 : char *suffix = NULL;
907 :
908 2 : errno = 0;
909 2 : result = strtod(src, &after_num);
910 2 : if (src == after_num)
911 0 : pg_fatal("transfer rate \"%s\" is not a valid value", src);
912 2 : if (errno != 0)
913 0 : pg_fatal("invalid transfer rate \"%s\": %m", src);
914 :
915 2 : if (result <= 0)
916 : {
917 : /*
918 : * Reject obviously wrong values here.
919 : */
920 0 : pg_fatal("transfer rate must be greater than zero");
921 : }
922 :
923 : /*
924 : * Evaluate suffix, after skipping over possible whitespace. Lack of
925 : * suffix means kilobytes.
926 : */
927 2 : while (*after_num != '\0' && isspace((unsigned char) *after_num))
928 0 : after_num++;
929 :
930 2 : if (*after_num != '\0')
931 : {
932 0 : suffix = after_num;
933 0 : if (*after_num == 'k')
934 : {
935 : /* kilobyte is the expected unit. */
936 0 : after_num++;
937 : }
938 0 : else if (*after_num == 'M')
939 : {
940 0 : after_num++;
941 0 : result *= 1024.0;
942 : }
943 : }
944 :
945 : /* The rest can only consist of white space. */
946 2 : while (*after_num != '\0' && isspace((unsigned char) *after_num))
947 0 : after_num++;
948 :
949 2 : if (*after_num != '\0')
950 0 : pg_fatal("invalid --max-rate unit: \"%s\"", suffix);
951 :
952 : /* Valid integer? */
953 2 : if ((uint64) result != (uint64) ((uint32) result))
954 0 : pg_fatal("transfer rate \"%s\" exceeds integer range", src);
955 :
956 : /*
957 : * The range is checked on the server side too, but avoid the server
958 : * connection if a nonsensical value was passed.
959 : */
960 2 : if (result < MAX_RATE_LOWER || result > MAX_RATE_UPPER)
961 0 : pg_fatal("transfer rate \"%s\" is out of range", src);
962 :
963 2 : return (int32) result;
964 : }
965 :
966 : /*
967 : * Basic parsing of a value specified for -Z/--compress.
968 : *
969 : * We're not concerned here with understanding exactly what behavior the
970 : * user wants, but we do need to know whether the user is requesting client
971 : * or server side compression or leaving it unspecified, and we need to
972 : * separate the name of the compression algorithm from the detail string.
973 : *
974 : * For instance, if the user writes --compress client-lz4:6, we want to
975 : * separate that into (a) client-side compression, (b) algorithm "lz4",
976 : * and (c) detail "6". Note, however, that all the client/server prefix is
977 : * optional, and so is the detail. The algorithm name is required, unless
978 : * the whole string is an integer, in which case we assume "gzip" as the
979 : * algorithm and use the integer as the detail.
980 : *
981 : * We're not concerned with validation at this stage, so if the user writes
982 : * --compress client-turkey:sandwich, the requested algorithm is "turkey"
983 : * and the detail string is "sandwich". We'll sort out whether that's legal
984 : * at a later stage.
985 : */
986 : static void
987 60 : backup_parse_compress_options(char *option, char **algorithm, char **detail,
988 : CompressionLocation *locationres)
989 : {
990 : /*
991 : * Strip off any "client-" or "server-" prefix, calculating the location.
992 : */
993 60 : if (strncmp(option, "server-", 7) == 0)
994 : {
995 28 : *locationres = COMPRESS_LOCATION_SERVER;
996 28 : option += 7;
997 : }
998 32 : else if (strncmp(option, "client-", 7) == 0)
999 : {
1000 4 : *locationres = COMPRESS_LOCATION_CLIENT;
1001 4 : option += 7;
1002 : }
1003 : else
1004 28 : *locationres = COMPRESS_LOCATION_UNSPECIFIED;
1005 :
1006 : /* fallback to the common parsing for the algorithm and detail */
1007 60 : parse_compress_options(option, algorithm, detail);
1008 60 : }
1009 :
1010 : /*
1011 : * Read a stream of COPY data and invoke the provided callback for each
1012 : * chunk.
1013 : */
1014 : static void
1015 316 : ReceiveCopyData(PGconn *conn, WriteDataCallback callback,
1016 : void *callback_data)
1017 : {
1018 : PGresult *res;
1019 :
1020 : /* Get the COPY data stream. */
1021 316 : res = PQgetResult(conn);
1022 316 : if (PQresultStatus(res) != PGRES_COPY_OUT)
1023 0 : pg_fatal("could not get COPY data stream: %s",
1024 : PQerrorMessage(conn));
1025 316 : PQclear(res);
1026 :
1027 : /* Loop over chunks until done. */
1028 : while (1)
1029 675034 : {
1030 : int r;
1031 : char *copybuf;
1032 :
1033 675350 : r = PQgetCopyData(conn, ©buf, 0);
1034 675350 : if (r == -1)
1035 : {
1036 : /* End of chunk. */
1037 312 : break;
1038 : }
1039 675038 : else if (r == -2)
1040 0 : pg_fatal("could not read COPY data: %s",
1041 : PQerrorMessage(conn));
1042 :
1043 675038 : if (bgchild_exited)
1044 4 : pg_fatal("background process terminated unexpectedly");
1045 :
1046 675034 : (*callback) (r, copybuf, callback_data);
1047 :
1048 675034 : PQfreemem(copybuf);
1049 : }
1050 312 : }
1051 :
1052 : /*
1053 : * Figure out what to do with an archive received from the server based on
1054 : * the options selected by the user. We may just write the results directly
1055 : * to a file, or we might compress first, or we might extract the tar file
1056 : * and write each member separately. This function doesn't do any of that
1057 : * directly, but it works out what kind of astreamer we need to create so
1058 : * that the right stuff happens when, down the road, we actually receive
1059 : * the data.
1060 : */
1061 : static astreamer *
1062 366 : CreateBackupStreamer(char *archive_name, char *spclocation,
1063 : astreamer **manifest_inject_streamer_p,
1064 : bool is_recovery_guc_supported,
1065 : bool expect_unterminated_tarfile,
1066 : pg_compress_specification *compress)
1067 : {
1068 366 : astreamer *streamer = NULL;
1069 366 : astreamer *manifest_inject_streamer = NULL;
1070 : bool inject_manifest;
1071 : bool is_tar,
1072 : is_tar_gz,
1073 : is_tar_lz4,
1074 : is_tar_zstd,
1075 : is_compressed_tar;
1076 : bool must_parse_archive;
1077 366 : int archive_name_len = strlen(archive_name);
1078 :
1079 : /*
1080 : * Normally, we emit the backup manifest as a separate file, but when
1081 : * we're writing a tarfile to stdout, we don't have that option, so
1082 : * include it in the one tarfile we've got.
1083 : */
1084 366 : inject_manifest = (format == 't' && strcmp(basedir, "-") == 0 && manifest);
1085 :
1086 : /* Is this a tar archive? */
1087 732 : is_tar = (archive_name_len > 4 &&
1088 366 : strcmp(archive_name + archive_name_len - 4, ".tar") == 0);
1089 :
1090 : /* Is this a .tar.gz archive? */
1091 732 : is_tar_gz = (archive_name_len > 7 &&
1092 366 : strcmp(archive_name + archive_name_len - 7, ".tar.gz") == 0);
1093 :
1094 : /* Is this a .tar.lz4 archive? */
1095 436 : is_tar_lz4 = (archive_name_len > 8 &&
1096 70 : strcmp(archive_name + archive_name_len - 8, ".tar.lz4") == 0);
1097 :
1098 : /* Is this a .tar.zst archive? */
1099 436 : is_tar_zstd = (archive_name_len > 8 &&
1100 70 : strcmp(archive_name + archive_name_len - 8, ".tar.zst") == 0);
1101 :
1102 : /* Is this any kind of compressed tar? */
1103 366 : is_compressed_tar = is_tar_gz || is_tar_lz4 || is_tar_zstd;
1104 :
1105 : /*
1106 : * Injecting the manifest into a compressed tar file would be possible if
1107 : * we decompressed it, parsed the tarfile, generated a new tarfile, and
1108 : * recompressed it, but compressing and decompressing multiple times just
1109 : * to inject the manifest seems inefficient enough that it's probably not
1110 : * what the user wants. So, instead, reject the request and tell the user
1111 : * to specify something more reasonable.
1112 : */
1113 366 : if (inject_manifest && is_compressed_tar)
1114 : {
1115 0 : pg_log_error("cannot inject manifest into a compressed tar file");
1116 0 : pg_log_error_hint("Use client-side compression, send the output to a directory rather than standard output, or use %s.",
1117 : "--no-manifest");
1118 0 : exit(1);
1119 : }
1120 :
1121 : /*
1122 : * We have to parse the archive if (1) we're suppose to extract it, or if
1123 : * (2) we need to inject backup_manifest or recovery configuration into
1124 : * it. However, we only know how to parse tar archives.
1125 : */
1126 402 : must_parse_archive = (format == 'p' || inject_manifest ||
1127 36 : (spclocation == NULL && writerecoveryconf));
1128 :
1129 : /* At present, we only know how to parse tar archives. */
1130 366 : if (must_parse_archive && !is_tar && !is_compressed_tar)
1131 : {
1132 0 : pg_log_error("cannot parse archive \"%s\"", archive_name);
1133 0 : pg_log_error_detail("Only tar archives can be parsed.");
1134 0 : if (format == 'p')
1135 0 : pg_log_error_detail("Plain format requires pg_basebackup to parse the archive.");
1136 0 : if (inject_manifest)
1137 0 : pg_log_error_detail("Using - as the output directory requires pg_basebackup to parse the archive.");
1138 0 : if (writerecoveryconf)
1139 0 : pg_log_error_detail("The -R option requires pg_basebackup to parse the archive.");
1140 0 : exit(1);
1141 : }
1142 :
1143 366 : if (format == 'p')
1144 : {
1145 : const char *directory;
1146 :
1147 : /*
1148 : * In plain format, we must extract the archive. The data for the main
1149 : * tablespace will be written to the base directory, and the data for
1150 : * other tablespaces will be written to the directory where they're
1151 : * located on the server, after applying any user-specified tablespace
1152 : * mappings.
1153 : *
1154 : * In the case of an in-place tablespace, spclocation will be a
1155 : * relative path. We just convert it to an absolute path by prepending
1156 : * basedir.
1157 : */
1158 324 : if (spclocation == NULL)
1159 264 : directory = basedir;
1160 60 : else if (!is_absolute_path(spclocation))
1161 28 : directory = psprintf("%s/%s", basedir, spclocation);
1162 : else
1163 32 : directory = get_tablespace_mapping(spclocation);
1164 324 : streamer = astreamer_extractor_new(directory,
1165 : get_tablespace_mapping,
1166 : progress_update_filename);
1167 : }
1168 : else
1169 : {
1170 : FILE *archive_file;
1171 : char archive_filename[MAXPGPATH];
1172 :
1173 : /*
1174 : * In tar format, we just write the archive without extracting it.
1175 : * Normally, we write it to the archive name provided by the caller,
1176 : * but when the base directory is "-" that means we need to write to
1177 : * standard output.
1178 : */
1179 42 : if (strcmp(basedir, "-") == 0)
1180 : {
1181 0 : snprintf(archive_filename, sizeof(archive_filename), "-");
1182 0 : archive_file = stdout;
1183 : }
1184 : else
1185 : {
1186 42 : snprintf(archive_filename, sizeof(archive_filename),
1187 : "%s/%s", basedir, archive_name);
1188 42 : archive_file = NULL;
1189 : }
1190 :
1191 42 : if (compress->algorithm == PG_COMPRESSION_NONE)
1192 32 : streamer = astreamer_plain_writer_new(archive_filename,
1193 : archive_file);
1194 10 : else if (compress->algorithm == PG_COMPRESSION_GZIP)
1195 : {
1196 8 : strlcat(archive_filename, ".gz", sizeof(archive_filename));
1197 8 : streamer = astreamer_gzip_writer_new(archive_filename,
1198 : archive_file, compress);
1199 : }
1200 2 : else if (compress->algorithm == PG_COMPRESSION_LZ4)
1201 : {
1202 2 : strlcat(archive_filename, ".lz4", sizeof(archive_filename));
1203 2 : streamer = astreamer_plain_writer_new(archive_filename,
1204 : archive_file);
1205 2 : streamer = astreamer_lz4_compressor_new(streamer, compress);
1206 : }
1207 0 : else if (compress->algorithm == PG_COMPRESSION_ZSTD)
1208 : {
1209 0 : strlcat(archive_filename, ".zst", sizeof(archive_filename));
1210 0 : streamer = astreamer_plain_writer_new(archive_filename,
1211 : archive_file);
1212 0 : streamer = astreamer_zstd_compressor_new(streamer, compress);
1213 : }
1214 : else
1215 : {
1216 : Assert(false); /* not reachable */
1217 : }
1218 :
1219 : /*
1220 : * If we need to parse the archive for whatever reason, then we'll
1221 : * also need to re-archive, because, if the output format is tar, the
1222 : * only point of parsing the archive is to be able to inject stuff
1223 : * into it.
1224 : */
1225 42 : if (must_parse_archive)
1226 0 : streamer = astreamer_tar_archiver_new(streamer);
1227 42 : progress_update_filename(archive_filename);
1228 : }
1229 :
1230 : /*
1231 : * If we're supposed to inject the backup manifest into the results, it
1232 : * should be done here, so that the file content can be injected directly,
1233 : * without worrying about the details of the tar format.
1234 : */
1235 366 : if (inject_manifest)
1236 0 : manifest_inject_streamer = streamer;
1237 :
1238 : /*
1239 : * If this is the main tablespace and we're supposed to write recovery
1240 : * information, arrange to do that.
1241 : */
1242 366 : if (spclocation == NULL && writerecoveryconf)
1243 : {
1244 : Assert(must_parse_archive);
1245 6 : streamer = astreamer_recovery_injector_new(streamer,
1246 : is_recovery_guc_supported,
1247 : recoveryconfcontents);
1248 : }
1249 :
1250 : /*
1251 : * If we're doing anything that involves understanding the contents of the
1252 : * archive, we'll need to parse it. If not, we can skip parsing it, but
1253 : * old versions of the server send improperly terminated tarfiles, so if
1254 : * we're talking to such a server we'll need to add the terminator here.
1255 : */
1256 366 : if (must_parse_archive)
1257 324 : streamer = astreamer_tar_parser_new(streamer);
1258 42 : else if (expect_unterminated_tarfile)
1259 0 : streamer = astreamer_tar_terminator_new(streamer);
1260 :
1261 : /*
1262 : * If the user has requested a server compressed archive along with
1263 : * archive extraction at client then we need to decompress it.
1264 : */
1265 366 : if (format == 'p')
1266 : {
1267 324 : if (is_tar_gz)
1268 2 : streamer = astreamer_gzip_decompressor_new(streamer);
1269 322 : else if (is_tar_lz4)
1270 2 : streamer = astreamer_lz4_decompressor_new(streamer);
1271 320 : else if (is_tar_zstd)
1272 0 : streamer = astreamer_zstd_decompressor_new(streamer);
1273 : }
1274 :
1275 : /* Return the results. */
1276 366 : *manifest_inject_streamer_p = manifest_inject_streamer;
1277 366 : return streamer;
1278 : }
1279 :
1280 : /*
1281 : * Receive all of the archives the server wants to send - and the backup
1282 : * manifest if present - as a single COPY stream.
1283 : */
1284 : static void
1285 316 : ReceiveArchiveStream(PGconn *conn, pg_compress_specification *compress)
1286 : {
1287 : ArchiveStreamState state;
1288 :
1289 : /* Set up initial state. */
1290 316 : memset(&state, 0, sizeof(state));
1291 316 : state.tablespacenum = -1;
1292 316 : state.compress = compress;
1293 :
1294 : /* All the real work happens in ReceiveArchiveStreamChunk. */
1295 316 : ReceiveCopyData(conn, ReceiveArchiveStreamChunk, &state);
1296 :
1297 : /* If we wrote the backup manifest to a file, close the file. */
1298 312 : if (state.manifest_file !=NULL)
1299 : {
1300 292 : fclose(state.manifest_file);
1301 292 : state.manifest_file = NULL;
1302 : }
1303 :
1304 : /*
1305 : * If we buffered the backup manifest in order to inject it into the
1306 : * output tarfile, do that now.
1307 : */
1308 312 : if (state.manifest_inject_streamer != NULL &&
1309 0 : state.manifest_buffer != NULL)
1310 : {
1311 0 : astreamer_inject_file(state.manifest_inject_streamer,
1312 : "backup_manifest",
1313 0 : state.manifest_buffer->data,
1314 0 : state.manifest_buffer->len);
1315 0 : destroyPQExpBuffer(state.manifest_buffer);
1316 0 : state.manifest_buffer = NULL;
1317 : }
1318 :
1319 : /* If there's still an archive in progress, end processing. */
1320 312 : if (state.streamer != NULL)
1321 : {
1322 296 : astreamer_finalize(state.streamer);
1323 296 : astreamer_free(state.streamer);
1324 296 : state.streamer = NULL;
1325 : }
1326 312 : }
1327 :
1328 : /*
1329 : * Receive one chunk of data sent by the server as part of a single COPY
1330 : * stream that includes all archives and the manifest.
1331 : */
1332 : static void
1333 675034 : ReceiveArchiveStreamChunk(size_t r, char *copybuf, void *callback_data)
1334 : {
1335 675034 : ArchiveStreamState *state = callback_data;
1336 675034 : size_t cursor = 0;
1337 :
1338 : /* Each CopyData message begins with a type byte. */
1339 675034 : switch (GetCopyDataByte(r, copybuf, &cursor))
1340 : {
1341 388 : case 'n':
1342 : {
1343 : /* New archive. */
1344 : char *archive_name;
1345 : char *spclocation;
1346 :
1347 : /*
1348 : * We force a progress report at the end of each tablespace. A
1349 : * new tablespace starts when the previous one ends, except in
1350 : * the case of the very first one.
1351 : */
1352 388 : if (++state->tablespacenum > 0)
1353 72 : progress_report(state->tablespacenum, true, false);
1354 :
1355 : /* Sanity check. */
1356 388 : if (state->manifest_buffer != NULL ||
1357 388 : state->manifest_file !=NULL)
1358 0 : pg_fatal("archives must precede manifest");
1359 :
1360 : /* Parse the rest of the CopyData message. */
1361 388 : archive_name = GetCopyDataString(r, copybuf, &cursor);
1362 388 : spclocation = GetCopyDataString(r, copybuf, &cursor);
1363 388 : GetCopyDataEnd(r, copybuf, cursor);
1364 :
1365 : /*
1366 : * Basic sanity checks on the archive name: it shouldn't be
1367 : * empty, it shouldn't start with a dot, and it shouldn't
1368 : * contain a path separator.
1369 : */
1370 388 : if (archive_name[0] == '\0' || archive_name[0] == '.' ||
1371 388 : strchr(archive_name, '/') != NULL ||
1372 388 : strchr(archive_name, '\\') != NULL)
1373 0 : pg_fatal("invalid archive name: \"%s\"",
1374 : archive_name);
1375 :
1376 : /*
1377 : * An empty spclocation is treated as NULL. We expect this
1378 : * case to occur for the data directory itself, but not for
1379 : * any archives that correspond to tablespaces.
1380 : */
1381 388 : if (spclocation[0] == '\0')
1382 316 : spclocation = NULL;
1383 :
1384 : /* End processing of any prior archive. */
1385 388 : if (state->streamer != NULL)
1386 : {
1387 66 : astreamer_finalize(state->streamer);
1388 66 : astreamer_free(state->streamer);
1389 66 : state->streamer = NULL;
1390 : }
1391 :
1392 : /*
1393 : * Create an appropriate backup streamer, unless a backup
1394 : * target was specified. In that case, it's up to the server
1395 : * to put the backup wherever it needs to go.
1396 : */
1397 388 : if (backup_target == NULL)
1398 : {
1399 : /*
1400 : * We know that recovery GUCs are supported, because this
1401 : * protocol can only be used on v15+.
1402 : */
1403 366 : state->streamer =
1404 366 : CreateBackupStreamer(archive_name,
1405 : spclocation,
1406 : &state->manifest_inject_streamer,
1407 : true, false,
1408 : state->compress);
1409 : }
1410 388 : break;
1411 : }
1412 :
1413 673934 : case 'd':
1414 : {
1415 : /* Archive or manifest data. */
1416 673934 : if (state->manifest_buffer != NULL)
1417 : {
1418 : /* Manifest data, buffer in memory. */
1419 0 : appendPQExpBuffer(state->manifest_buffer, copybuf + 1,
1420 : r - 1);
1421 : }
1422 673934 : else if (state->manifest_file !=NULL)
1423 : {
1424 : /* Manifest data, write to disk. */
1425 1512 : if (fwrite(copybuf + 1, r - 1, 1,
1426 : state->manifest_file) != 1)
1427 : {
1428 : /*
1429 : * If fwrite() didn't set errno, assume that the
1430 : * problem is that we're out of disk space.
1431 : */
1432 0 : if (errno == 0)
1433 0 : errno = ENOSPC;
1434 0 : pg_fatal("could not write to file \"%s\": %m",
1435 : state->manifest_filename);
1436 : }
1437 : }
1438 672422 : else if (state->streamer != NULL)
1439 : {
1440 : /* Archive data. */
1441 672422 : astreamer_content(state->streamer, NULL, copybuf + 1,
1442 672422 : r - 1, ASTREAMER_UNKNOWN);
1443 : }
1444 : else
1445 0 : pg_fatal("unexpected payload data");
1446 673934 : break;
1447 : }
1448 :
1449 404 : case 'p':
1450 : {
1451 : /*
1452 : * Progress report.
1453 : *
1454 : * The remainder of the message is expected to be an 8-byte
1455 : * count of bytes completed.
1456 : */
1457 404 : totaldone = GetCopyDataUInt64(r, copybuf, &cursor);
1458 404 : GetCopyDataEnd(r, copybuf, cursor);
1459 :
1460 : /*
1461 : * The server shouldn't send progress report messages too
1462 : * often, so we force an update each time we receive one.
1463 : */
1464 404 : progress_report(state->tablespacenum, true, false);
1465 404 : break;
1466 : }
1467 :
1468 308 : case 'm':
1469 : {
1470 : /*
1471 : * Manifest data will be sent next. This message is not
1472 : * expected to have any further payload data.
1473 : */
1474 308 : GetCopyDataEnd(r, copybuf, cursor);
1475 :
1476 : /*
1477 : * If a backup target was specified, figuring out where to put
1478 : * the manifest is the server's problem. Otherwise, we need to
1479 : * deal with it.
1480 : */
1481 308 : if (backup_target == NULL)
1482 : {
1483 : /*
1484 : * If we're supposed inject the manifest into the archive,
1485 : * we prepare to buffer it in memory; otherwise, we
1486 : * prepare to write it to a temporary file.
1487 : */
1488 292 : if (state->manifest_inject_streamer != NULL)
1489 0 : state->manifest_buffer = createPQExpBuffer();
1490 : else
1491 : {
1492 292 : snprintf(state->manifest_filename,
1493 : sizeof(state->manifest_filename),
1494 : "%s/backup_manifest.tmp", basedir);
1495 292 : state->manifest_file =
1496 292 : fopen(state->manifest_filename, "wb");
1497 292 : if (state->manifest_file == NULL)
1498 0 : pg_fatal("could not create file \"%s\": %m",
1499 : state->manifest_filename);
1500 : }
1501 : }
1502 308 : break;
1503 : }
1504 :
1505 0 : default:
1506 0 : ReportCopyDataParseError(r, copybuf);
1507 0 : break;
1508 : }
1509 675034 : }
1510 :
1511 : /*
1512 : * Get a single byte from a CopyData message.
1513 : *
1514 : * Bail out if none remain.
1515 : */
1516 : static char
1517 675034 : GetCopyDataByte(size_t r, char *copybuf, size_t *cursor)
1518 : {
1519 675034 : if (*cursor >= r)
1520 0 : ReportCopyDataParseError(r, copybuf);
1521 :
1522 675034 : return copybuf[(*cursor)++];
1523 : }
1524 :
1525 : /*
1526 : * Get a NUL-terminated string from a CopyData message.
1527 : *
1528 : * Bail out if the terminating NUL cannot be found.
1529 : */
1530 : static char *
1531 776 : GetCopyDataString(size_t r, char *copybuf, size_t *cursor)
1532 : {
1533 776 : size_t startpos = *cursor;
1534 776 : size_t endpos = startpos;
1535 :
1536 : while (1)
1537 : {
1538 5430 : if (endpos >= r)
1539 0 : ReportCopyDataParseError(r, copybuf);
1540 5430 : if (copybuf[endpos] == '\0')
1541 776 : break;
1542 4654 : ++endpos;
1543 : }
1544 :
1545 776 : *cursor = endpos + 1;
1546 776 : return ©buf[startpos];
1547 : }
1548 :
1549 : /*
1550 : * Get an unsigned 64-bit integer from a CopyData message.
1551 : *
1552 : * Bail out if there are not at least 8 bytes remaining.
1553 : */
1554 : static uint64
1555 404 : GetCopyDataUInt64(size_t r, char *copybuf, size_t *cursor)
1556 : {
1557 : uint64 result;
1558 :
1559 404 : if (*cursor + sizeof(uint64) > r)
1560 0 : ReportCopyDataParseError(r, copybuf);
1561 404 : memcpy(&result, ©buf[*cursor], sizeof(uint64));
1562 404 : *cursor += sizeof(uint64);
1563 404 : return pg_ntoh64(result);
1564 : }
1565 :
1566 : /*
1567 : * Bail out if we didn't parse the whole message.
1568 : */
1569 : static void
1570 1100 : GetCopyDataEnd(size_t r, char *copybuf, size_t cursor)
1571 : {
1572 1100 : if (r != cursor)
1573 0 : ReportCopyDataParseError(r, copybuf);
1574 1100 : }
1575 :
1576 : /*
1577 : * Report failure to parse a CopyData message from the server. Then exit.
1578 : *
1579 : * As a debugging aid, we try to give some hint about what kind of message
1580 : * provoked the failure. Perhaps this is not detailed enough, but it's not
1581 : * clear that it's worth expending any more code on what should be a
1582 : * can't-happen case.
1583 : */
1584 : static void
1585 0 : ReportCopyDataParseError(size_t r, char *copybuf)
1586 : {
1587 0 : if (r == 0)
1588 0 : pg_fatal("empty COPY message");
1589 : else
1590 0 : pg_fatal("malformed COPY message of type %d, length %zu",
1591 : copybuf[0], r);
1592 : }
1593 :
1594 : /*
1595 : * Receive raw tar data from the server, and stream it to the appropriate
1596 : * location. If we're writing a single tarfile to standard output, also
1597 : * receive the backup manifest and inject it into that tarfile.
1598 : */
1599 : static void
1600 0 : ReceiveTarFile(PGconn *conn, char *archive_name, char *spclocation,
1601 : bool tablespacenum, pg_compress_specification *compress)
1602 : {
1603 : WriteTarState state;
1604 : astreamer *manifest_inject_streamer;
1605 : bool is_recovery_guc_supported;
1606 : bool expect_unterminated_tarfile;
1607 :
1608 : /* Pass all COPY data through to the backup streamer. */
1609 0 : memset(&state, 0, sizeof(state));
1610 0 : is_recovery_guc_supported =
1611 0 : PQserverVersion(conn) >= MINIMUM_VERSION_FOR_RECOVERY_GUC;
1612 0 : expect_unterminated_tarfile =
1613 0 : PQserverVersion(conn) < MINIMUM_VERSION_FOR_TERMINATED_TARFILE;
1614 0 : state.streamer = CreateBackupStreamer(archive_name, spclocation,
1615 : &manifest_inject_streamer,
1616 : is_recovery_guc_supported,
1617 : expect_unterminated_tarfile,
1618 : compress);
1619 0 : state.tablespacenum = tablespacenum;
1620 0 : ReceiveCopyData(conn, ReceiveTarCopyChunk, &state);
1621 0 : progress_update_filename(NULL);
1622 :
1623 : /*
1624 : * The decision as to whether we need to inject the backup manifest into
1625 : * the output at this stage is made by CreateBackupStreamer; if that is
1626 : * needed, manifest_inject_streamer will be non-NULL; otherwise, it will
1627 : * be NULL.
1628 : */
1629 0 : if (manifest_inject_streamer != NULL)
1630 : {
1631 : PQExpBufferData buf;
1632 :
1633 : /* Slurp the entire backup manifest into a buffer. */
1634 0 : initPQExpBuffer(&buf);
1635 0 : ReceiveBackupManifestInMemory(conn, &buf);
1636 0 : if (PQExpBufferDataBroken(buf))
1637 0 : pg_fatal("out of memory");
1638 :
1639 : /* Inject it into the output tarfile. */
1640 0 : astreamer_inject_file(manifest_inject_streamer, "backup_manifest",
1641 0 : buf.data, buf.len);
1642 :
1643 : /* Free memory. */
1644 0 : termPQExpBuffer(&buf);
1645 : }
1646 :
1647 : /* Cleanup. */
1648 0 : astreamer_finalize(state.streamer);
1649 0 : astreamer_free(state.streamer);
1650 :
1651 0 : progress_report(tablespacenum, true, false);
1652 :
1653 : /*
1654 : * Do not sync the resulting tar file yet, all files are synced once at
1655 : * the end.
1656 : */
1657 0 : }
1658 :
1659 : /*
1660 : * Receive one chunk of tar-format data from the server.
1661 : */
1662 : static void
1663 0 : ReceiveTarCopyChunk(size_t r, char *copybuf, void *callback_data)
1664 : {
1665 0 : WriteTarState *state = callback_data;
1666 :
1667 0 : astreamer_content(state->streamer, NULL, copybuf, r, ASTREAMER_UNKNOWN);
1668 :
1669 0 : totaldone += r;
1670 0 : progress_report(state->tablespacenum, false, false);
1671 0 : }
1672 :
1673 :
1674 : /*
1675 : * Retrieve tablespace path, either relocated or original depending on whether
1676 : * -T was passed or not.
1677 : */
1678 : static const char *
1679 98 : get_tablespace_mapping(const char *dir)
1680 : {
1681 : TablespaceListCell *cell;
1682 : char canon_dir[MAXPGPATH];
1683 :
1684 : /* Canonicalize path for comparison consistency */
1685 98 : strlcpy(canon_dir, dir, sizeof(canon_dir));
1686 98 : canonicalize_path(canon_dir);
1687 :
1688 98 : for (cell = tablespace_dirs.head; cell; cell = cell->next)
1689 96 : if (strcmp(canon_dir, cell->old_dir) == 0)
1690 96 : return cell->new_dir;
1691 :
1692 2 : return dir;
1693 : }
1694 :
1695 : /*
1696 : * Receive the backup manifest file and write it out to a file.
1697 : */
1698 : static void
1699 0 : ReceiveBackupManifest(PGconn *conn)
1700 : {
1701 : WriteManifestState state;
1702 :
1703 0 : snprintf(state.filename, sizeof(state.filename),
1704 : "%s/backup_manifest.tmp", basedir);
1705 0 : state.file = fopen(state.filename, "wb");
1706 0 : if (state.file == NULL)
1707 0 : pg_fatal("could not create file \"%s\": %m", state.filename);
1708 :
1709 0 : ReceiveCopyData(conn, ReceiveBackupManifestChunk, &state);
1710 :
1711 0 : fclose(state.file);
1712 0 : }
1713 :
1714 : /*
1715 : * Receive one chunk of the backup manifest file and write it out to a file.
1716 : */
1717 : static void
1718 0 : ReceiveBackupManifestChunk(size_t r, char *copybuf, void *callback_data)
1719 : {
1720 0 : WriteManifestState *state = callback_data;
1721 :
1722 0 : errno = 0;
1723 0 : if (fwrite(copybuf, r, 1, state->file) != 1)
1724 : {
1725 : /* if write didn't set errno, assume problem is no disk space */
1726 0 : if (errno == 0)
1727 0 : errno = ENOSPC;
1728 0 : pg_fatal("could not write to file \"%s\": %m", state->filename);
1729 : }
1730 0 : }
1731 :
1732 : /*
1733 : * Receive the backup manifest file and write it out to a file.
1734 : */
1735 : static void
1736 0 : ReceiveBackupManifestInMemory(PGconn *conn, PQExpBuffer buf)
1737 : {
1738 0 : ReceiveCopyData(conn, ReceiveBackupManifestInMemoryChunk, buf);
1739 0 : }
1740 :
1741 : /*
1742 : * Receive one chunk of the backup manifest file and write it out to a file.
1743 : */
1744 : static void
1745 0 : ReceiveBackupManifestInMemoryChunk(size_t r, char *copybuf,
1746 : void *callback_data)
1747 : {
1748 0 : PQExpBuffer buf = callback_data;
1749 :
1750 0 : appendPQExpBuffer(buf, copybuf, r);
1751 0 : }
1752 :
1753 : static void
1754 358 : BaseBackup(char *compression_algorithm, char *compression_detail,
1755 : CompressionLocation compressloc,
1756 : pg_compress_specification *client_compress,
1757 : char *incremental_manifest)
1758 : {
1759 : PGresult *res;
1760 : char *sysidentifier;
1761 : TimeLineID latesttli;
1762 : TimeLineID starttli;
1763 : char *basebkp;
1764 : int i;
1765 : char xlogstart[64];
1766 358 : char xlogend[64] = {0};
1767 : int minServerMajor,
1768 : maxServerMajor;
1769 : int serverVersion,
1770 : serverMajor;
1771 : int writing_to_stdout;
1772 358 : bool use_new_option_syntax = false;
1773 : PQExpBufferData buf;
1774 :
1775 : Assert(conn != NULL);
1776 358 : initPQExpBuffer(&buf);
1777 :
1778 : /*
1779 : * Check server version. BASE_BACKUP command was introduced in 9.1, so we
1780 : * can't work with servers older than 9.1.
1781 : */
1782 358 : minServerMajor = 901;
1783 358 : maxServerMajor = PG_VERSION_NUM / 100;
1784 358 : serverVersion = PQserverVersion(conn);
1785 358 : serverMajor = serverVersion / 100;
1786 358 : if (serverMajor < minServerMajor || serverMajor > maxServerMajor)
1787 : {
1788 0 : const char *serverver = PQparameterStatus(conn, "server_version");
1789 :
1790 0 : pg_fatal("incompatible server version %s",
1791 : serverver ? serverver : "'unknown'");
1792 : }
1793 358 : if (serverMajor >= 1500)
1794 358 : use_new_option_syntax = true;
1795 :
1796 : /*
1797 : * If WAL streaming was requested, also check that the server is new
1798 : * enough for that.
1799 : */
1800 358 : if (includewal == STREAM_WAL && !CheckServerVersionForStreaming(conn))
1801 : {
1802 : /*
1803 : * Error message already written in CheckServerVersionForStreaming(),
1804 : * but add a hint about using -X none.
1805 : */
1806 0 : pg_log_error_hint("Use -X none or -X fetch to disable log streaming.");
1807 0 : exit(1);
1808 : }
1809 :
1810 : /*
1811 : * Build contents of configuration file if requested.
1812 : *
1813 : * Note that we don't use the dbname from key-value pair in conn as that
1814 : * would have been filled by the default dbname (dbname=replication) in
1815 : * case the user didn't specify the one. The dbname written in the config
1816 : * file as part of primary_conninfo would be used by slotsync worker which
1817 : * doesn't use a replication connection so the default won't work for it.
1818 : */
1819 358 : if (writerecoveryconf)
1820 6 : recoveryconfcontents = GenerateRecoveryConfig(conn,
1821 : replication_slot,
1822 : GetDbnameFromConnectionOptions(connection_string));
1823 :
1824 : /*
1825 : * Run IDENTIFY_SYSTEM so we can get the timeline
1826 : */
1827 358 : if (!RunIdentifySystem(conn, &sysidentifier, &latesttli, NULL, NULL))
1828 0 : exit(1);
1829 :
1830 : /*
1831 : * If the user wants an incremental backup, we must upload the manifest
1832 : * for the previous backup upon which it is to be based.
1833 : */
1834 358 : if (incremental_manifest != NULL)
1835 : {
1836 : int fd;
1837 : char mbuf[65536];
1838 : int nbytes;
1839 :
1840 : /* Reject if server is too old. */
1841 24 : if (serverVersion < MINIMUM_VERSION_FOR_WAL_SUMMARIES)
1842 0 : pg_fatal("server does not support incremental backup");
1843 :
1844 : /* Open the file. */
1845 24 : fd = open(incremental_manifest, O_RDONLY | PG_BINARY, 0);
1846 24 : if (fd < 0)
1847 0 : pg_fatal("could not open file \"%s\": %m", incremental_manifest);
1848 :
1849 : /* Tell the server what we want to do. */
1850 24 : if (PQsendQuery(conn, "UPLOAD_MANIFEST") == 0)
1851 0 : pg_fatal("could not send replication command \"%s\": %s",
1852 : "UPLOAD_MANIFEST", PQerrorMessage(conn));
1853 24 : res = PQgetResult(conn);
1854 24 : if (PQresultStatus(res) != PGRES_COPY_IN)
1855 : {
1856 0 : if (PQresultStatus(res) == PGRES_FATAL_ERROR)
1857 0 : pg_fatal("could not upload manifest: %s",
1858 : PQerrorMessage(conn));
1859 : else
1860 0 : pg_fatal("could not upload manifest: unexpected status %s",
1861 : PQresStatus(PQresultStatus(res)));
1862 : }
1863 :
1864 : /* Loop, reading from the file and sending the data to the server. */
1865 96 : while ((nbytes = read(fd, mbuf, sizeof mbuf)) > 0)
1866 : {
1867 72 : if (PQputCopyData(conn, mbuf, nbytes) < 0)
1868 0 : pg_fatal("could not send COPY data: %s",
1869 : PQerrorMessage(conn));
1870 : }
1871 :
1872 : /* Bail out if we exited the loop due to an error. */
1873 24 : if (nbytes < 0)
1874 0 : pg_fatal("could not read file \"%s\": %m", incremental_manifest);
1875 :
1876 : /* End the COPY operation. */
1877 24 : if (PQputCopyEnd(conn, NULL) < 0)
1878 0 : pg_fatal("could not send end-of-COPY: %s",
1879 : PQerrorMessage(conn));
1880 :
1881 : /* See whether the server is happy with what we sent. */
1882 24 : res = PQgetResult(conn);
1883 24 : if (PQresultStatus(res) == PGRES_FATAL_ERROR)
1884 2 : pg_fatal("could not upload manifest: %s",
1885 : PQerrorMessage(conn));
1886 22 : else if (PQresultStatus(res) != PGRES_COMMAND_OK)
1887 0 : pg_fatal("could not upload manifest: unexpected status %s",
1888 : PQresStatus(PQresultStatus(res)));
1889 :
1890 : /* Consume ReadyForQuery message from server. */
1891 22 : res = PQgetResult(conn);
1892 22 : if (res != NULL)
1893 0 : pg_fatal("unexpected extra result while sending manifest");
1894 :
1895 : /* Add INCREMENTAL option to BASE_BACKUP command. */
1896 22 : AppendPlainCommandOption(&buf, use_new_option_syntax, "INCREMENTAL");
1897 : }
1898 :
1899 : /*
1900 : * Continue building up the options list for the BASE_BACKUP command.
1901 : */
1902 356 : AppendStringCommandOption(&buf, use_new_option_syntax, "LABEL", label);
1903 356 : if (estimatesize)
1904 356 : AppendPlainCommandOption(&buf, use_new_option_syntax, "PROGRESS");
1905 356 : if (includewal == FETCH_WAL)
1906 34 : AppendPlainCommandOption(&buf, use_new_option_syntax, "WAL");
1907 356 : if (fastcheckpoint)
1908 : {
1909 336 : if (use_new_option_syntax)
1910 336 : AppendStringCommandOption(&buf, use_new_option_syntax,
1911 : "CHECKPOINT", "fast");
1912 : else
1913 0 : AppendPlainCommandOption(&buf, use_new_option_syntax, "FAST");
1914 : }
1915 356 : if (includewal != NO_WAL)
1916 : {
1917 336 : if (use_new_option_syntax)
1918 336 : AppendIntegerCommandOption(&buf, use_new_option_syntax, "WAIT", 0);
1919 : else
1920 0 : AppendPlainCommandOption(&buf, use_new_option_syntax, "NOWAIT");
1921 : }
1922 356 : if (maxrate > 0)
1923 2 : AppendIntegerCommandOption(&buf, use_new_option_syntax, "MAX_RATE",
1924 : maxrate);
1925 356 : if (format == 't')
1926 38 : AppendPlainCommandOption(&buf, use_new_option_syntax, "TABLESPACE_MAP");
1927 356 : if (!verify_checksums)
1928 : {
1929 2 : if (use_new_option_syntax)
1930 2 : AppendIntegerCommandOption(&buf, use_new_option_syntax,
1931 : "VERIFY_CHECKSUMS", 0);
1932 : else
1933 0 : AppendPlainCommandOption(&buf, use_new_option_syntax,
1934 : "NOVERIFY_CHECKSUMS");
1935 : }
1936 :
1937 356 : if (manifest)
1938 : {
1939 354 : AppendStringCommandOption(&buf, use_new_option_syntax, "MANIFEST",
1940 354 : manifest_force_encode ? "force-encode" : "yes");
1941 354 : if (manifest_checksums != NULL)
1942 28 : AppendStringCommandOption(&buf, use_new_option_syntax,
1943 : "MANIFEST_CHECKSUMS", manifest_checksums);
1944 : }
1945 :
1946 356 : if (backup_target != NULL)
1947 : {
1948 : char *colon;
1949 :
1950 26 : if (serverMajor < 1500)
1951 0 : pg_fatal("backup targets are not supported by this server version");
1952 :
1953 26 : if (writerecoveryconf)
1954 0 : pg_fatal("recovery configuration cannot be written when a backup target is used");
1955 :
1956 26 : AppendPlainCommandOption(&buf, use_new_option_syntax, "TABLESPACE_MAP");
1957 :
1958 26 : if ((colon = strchr(backup_target, ':')) == NULL)
1959 : {
1960 12 : AppendStringCommandOption(&buf, use_new_option_syntax,
1961 : "TARGET", backup_target);
1962 : }
1963 : else
1964 : {
1965 : char *target;
1966 :
1967 14 : target = pnstrdup(backup_target, colon - backup_target);
1968 14 : AppendStringCommandOption(&buf, use_new_option_syntax,
1969 : "TARGET", target);
1970 14 : AppendStringCommandOption(&buf, use_new_option_syntax,
1971 : "TARGET_DETAIL", colon + 1);
1972 : }
1973 : }
1974 330 : else if (serverMajor >= 1500)
1975 330 : AppendStringCommandOption(&buf, use_new_option_syntax,
1976 : "TARGET", "client");
1977 :
1978 356 : if (compressloc == COMPRESS_LOCATION_SERVER)
1979 : {
1980 50 : if (!use_new_option_syntax)
1981 0 : pg_fatal("server does not support server-side compression");
1982 50 : AppendStringCommandOption(&buf, use_new_option_syntax,
1983 : "COMPRESSION", compression_algorithm);
1984 50 : if (compression_detail != NULL)
1985 22 : AppendStringCommandOption(&buf, use_new_option_syntax,
1986 : "COMPRESSION_DETAIL",
1987 : compression_detail);
1988 : }
1989 :
1990 356 : if (verbose)
1991 0 : pg_log_info("initiating base backup, waiting for checkpoint to complete");
1992 :
1993 356 : if (showprogress && !verbose)
1994 : {
1995 0 : fprintf(stderr, _("waiting for checkpoint"));
1996 0 : if (isatty(fileno(stderr)))
1997 0 : fprintf(stderr, "\r");
1998 : else
1999 0 : fprintf(stderr, "\n");
2000 : }
2001 :
2002 356 : if (use_new_option_syntax && buf.len > 0)
2003 356 : basebkp = psprintf("BASE_BACKUP (%s)", buf.data);
2004 : else
2005 0 : basebkp = psprintf("BASE_BACKUP %s", buf.data);
2006 :
2007 : /* OK, try to start the backup. */
2008 356 : if (PQsendQuery(conn, basebkp) == 0)
2009 0 : pg_fatal("could not send replication command \"%s\": %s",
2010 : "BASE_BACKUP", PQerrorMessage(conn));
2011 :
2012 : /*
2013 : * Get the starting WAL location
2014 : */
2015 356 : res = PQgetResult(conn);
2016 356 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
2017 36 : pg_fatal("could not initiate base backup: %s",
2018 : PQerrorMessage(conn));
2019 320 : if (PQntuples(res) != 1)
2020 0 : pg_fatal("server returned unexpected response to BASE_BACKUP command; got %d rows and %d fields, expected %d rows and %d fields",
2021 : PQntuples(res), PQnfields(res), 1, 2);
2022 :
2023 320 : strlcpy(xlogstart, PQgetvalue(res, 0, 0), sizeof(xlogstart));
2024 :
2025 320 : if (verbose)
2026 0 : pg_log_info("checkpoint completed");
2027 :
2028 : /*
2029 : * 9.3 and later sends the TLI of the starting point. With older servers,
2030 : * assume it's the same as the latest timeline reported by
2031 : * IDENTIFY_SYSTEM.
2032 : */
2033 320 : if (PQnfields(res) >= 2)
2034 320 : starttli = atoi(PQgetvalue(res, 0, 1));
2035 : else
2036 0 : starttli = latesttli;
2037 320 : PQclear(res);
2038 :
2039 320 : if (verbose && includewal != NO_WAL)
2040 0 : pg_log_info("write-ahead log start point: %s on timeline %u",
2041 : xlogstart, starttli);
2042 :
2043 : /*
2044 : * Get the header
2045 : */
2046 320 : res = PQgetResult(conn);
2047 320 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
2048 0 : pg_fatal("could not get backup header: %s",
2049 : PQerrorMessage(conn));
2050 320 : if (PQntuples(res) < 1)
2051 0 : pg_fatal("no data returned from server");
2052 :
2053 : /*
2054 : * Sum up the total size, for progress reporting
2055 : */
2056 320 : totalsize_kb = totaldone = 0;
2057 320 : tablespacecount = PQntuples(res);
2058 710 : for (i = 0; i < PQntuples(res); i++)
2059 : {
2060 392 : totalsize_kb += atoll(PQgetvalue(res, i, 2));
2061 :
2062 : /*
2063 : * Verify tablespace directories are empty. Don't bother with the
2064 : * first once since it can be relocated, and it will be checked before
2065 : * we do anything anyway.
2066 : *
2067 : * Note that this is skipped for tar format backups and backups that
2068 : * the server is storing to a target location, since in that case we
2069 : * won't be storing anything into these directories and thus should
2070 : * not create them.
2071 : */
2072 392 : if (backup_target == NULL && format == 'p' && !PQgetisnull(res, i, 1))
2073 : {
2074 62 : char *path = PQgetvalue(res, i, 1);
2075 :
2076 62 : if (is_absolute_path(path))
2077 34 : path = unconstify(char *, get_tablespace_mapping(path));
2078 : else
2079 : {
2080 : /* This is an in-place tablespace, so prepend basedir. */
2081 28 : path = psprintf("%s/%s", basedir, path);
2082 : }
2083 :
2084 62 : verify_dir_is_empty_or_create(path, &made_tablespace_dirs, &found_tablespace_dirs);
2085 : }
2086 : }
2087 :
2088 : /*
2089 : * When writing to stdout, require a single tablespace
2090 : */
2091 354 : writing_to_stdout = format == 't' && basedir != NULL &&
2092 36 : strcmp(basedir, "-") == 0;
2093 318 : if (writing_to_stdout && PQntuples(res) > 1)
2094 0 : pg_fatal("can only write single tablespace to stdout, database has %d",
2095 : PQntuples(res));
2096 :
2097 : /*
2098 : * If we're streaming WAL, start the streaming session before we start
2099 : * receiving the actual data chunks.
2100 : */
2101 318 : if (includewal == STREAM_WAL)
2102 : {
2103 : pg_compress_algorithm wal_compress_algorithm;
2104 : int wal_compress_level;
2105 :
2106 274 : if (verbose)
2107 0 : pg_log_info("starting background WAL receiver");
2108 :
2109 274 : if (client_compress->algorithm == PG_COMPRESSION_GZIP)
2110 : {
2111 6 : wal_compress_algorithm = PG_COMPRESSION_GZIP;
2112 6 : wal_compress_level = client_compress->level;
2113 : }
2114 : else
2115 : {
2116 268 : wal_compress_algorithm = PG_COMPRESSION_NONE;
2117 268 : wal_compress_level = 0;
2118 : }
2119 :
2120 274 : StartLogStreamer(xlogstart, starttli, sysidentifier,
2121 : wal_compress_algorithm,
2122 : wal_compress_level);
2123 : }
2124 :
2125 316 : if (serverMajor >= 1500)
2126 : {
2127 : /* Receive a single tar stream with everything. */
2128 316 : ReceiveArchiveStream(conn, client_compress);
2129 : }
2130 : else
2131 : {
2132 : /* Receive a tar file for each tablespace in turn */
2133 0 : for (i = 0; i < PQntuples(res); i++)
2134 : {
2135 : char archive_name[MAXPGPATH];
2136 : char *spclocation;
2137 :
2138 : /*
2139 : * If we write the data out to a tar file, it will be named
2140 : * base.tar if it's the main data directory or <tablespaceoid>.tar
2141 : * if it's for another tablespace. CreateBackupStreamer() will
2142 : * arrange to add an extension to the archive name if
2143 : * pg_basebackup is performing compression, depending on the
2144 : * compression type.
2145 : */
2146 0 : if (PQgetisnull(res, i, 0))
2147 : {
2148 0 : strlcpy(archive_name, "base.tar", sizeof(archive_name));
2149 0 : spclocation = NULL;
2150 : }
2151 : else
2152 : {
2153 0 : snprintf(archive_name, sizeof(archive_name),
2154 : "%s.tar", PQgetvalue(res, i, 0));
2155 0 : spclocation = PQgetvalue(res, i, 1);
2156 : }
2157 :
2158 0 : ReceiveTarFile(conn, archive_name, spclocation, i,
2159 : client_compress);
2160 : }
2161 :
2162 : /*
2163 : * Now receive backup manifest, if appropriate.
2164 : *
2165 : * If we're writing a tarfile to stdout, ReceiveTarFile will have
2166 : * already processed the backup manifest and included it in the output
2167 : * tarfile. Such a configuration doesn't allow for writing multiple
2168 : * files.
2169 : *
2170 : * If we're talking to an older server, it won't send a backup
2171 : * manifest, so don't try to receive one.
2172 : */
2173 0 : if (!writing_to_stdout && manifest)
2174 0 : ReceiveBackupManifest(conn);
2175 : }
2176 :
2177 312 : if (showprogress)
2178 : {
2179 0 : progress_update_filename(NULL);
2180 0 : progress_report(PQntuples(res), true, true);
2181 : }
2182 :
2183 312 : PQclear(res);
2184 :
2185 : /*
2186 : * Get the stop position
2187 : */
2188 312 : res = PQgetResult(conn);
2189 312 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
2190 2 : pg_fatal("backup failed: %s",
2191 : PQerrorMessage(conn));
2192 310 : if (PQntuples(res) != 1)
2193 0 : pg_fatal("no write-ahead log end position returned from server");
2194 310 : strlcpy(xlogend, PQgetvalue(res, 0, 0), sizeof(xlogend));
2195 310 : if (verbose && includewal != NO_WAL)
2196 0 : pg_log_info("write-ahead log end point: %s", xlogend);
2197 310 : PQclear(res);
2198 :
2199 310 : res = PQgetResult(conn);
2200 310 : if (PQresultStatus(res) != PGRES_COMMAND_OK)
2201 : {
2202 6 : const char *sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
2203 :
2204 6 : if (sqlstate &&
2205 6 : strcmp(sqlstate, ERRCODE_DATA_CORRUPTED) == 0)
2206 : {
2207 6 : pg_log_error("checksum error occurred");
2208 6 : checksum_failure = true;
2209 : }
2210 : else
2211 : {
2212 0 : pg_log_error("final receive failed: %s",
2213 : PQerrorMessage(conn));
2214 : }
2215 6 : exit(1);
2216 : }
2217 :
2218 304 : if (bgchild > 0)
2219 : {
2220 : #ifndef WIN32
2221 : int status;
2222 : pid_t r;
2223 : #else
2224 : DWORD status;
2225 :
2226 : /*
2227 : * get a pointer sized version of bgchild to avoid warnings about
2228 : * casting to a different size on WIN64.
2229 : */
2230 : intptr_t bgchild_handle = bgchild;
2231 : uint32 hi,
2232 : lo;
2233 : #endif
2234 :
2235 260 : if (verbose)
2236 0 : pg_log_info("waiting for background process to finish streaming ...");
2237 :
2238 : #ifndef WIN32
2239 260 : if (write(bgpipe[1], xlogend, strlen(xlogend)) != strlen(xlogend))
2240 0 : pg_fatal("could not send command to background pipe: %m");
2241 :
2242 : /* Just wait for the background process to exit */
2243 260 : r = waitpid(bgchild, &status, 0);
2244 260 : if (r == (pid_t) -1)
2245 0 : pg_fatal("could not wait for child process: %m");
2246 260 : if (r != bgchild)
2247 0 : pg_fatal("child %d died, expected %d", (int) r, (int) bgchild);
2248 260 : if (status != 0)
2249 0 : pg_fatal("%s", wait_result_to_str(status));
2250 : /* Exited normally, we're happy! */
2251 : #else /* WIN32 */
2252 :
2253 : /*
2254 : * On Windows, since we are in the same process, we can just store the
2255 : * value directly in the variable, and then set the flag that says
2256 : * it's there.
2257 : */
2258 : if (sscanf(xlogend, "%X/%X", &hi, &lo) != 2)
2259 : pg_fatal("could not parse write-ahead log location \"%s\"",
2260 : xlogend);
2261 : xlogendptr = ((uint64) hi) << 32 | lo;
2262 : InterlockedIncrement(&has_xlogendptr);
2263 :
2264 : /* First wait for the thread to exit */
2265 : if (WaitForSingleObjectEx((HANDLE) bgchild_handle, INFINITE, FALSE) !=
2266 : WAIT_OBJECT_0)
2267 : {
2268 : _dosmaperr(GetLastError());
2269 : pg_fatal("could not wait for child thread: %m");
2270 : }
2271 : if (GetExitCodeThread((HANDLE) bgchild_handle, &status) == 0)
2272 : {
2273 : _dosmaperr(GetLastError());
2274 : pg_fatal("could not get child thread exit status: %m");
2275 : }
2276 : if (status != 0)
2277 : pg_fatal("child thread exited with error %u",
2278 : (unsigned int) status);
2279 : /* Exited normally, we're happy */
2280 : #endif
2281 : }
2282 :
2283 : /* Free the configuration file contents */
2284 304 : destroyPQExpBuffer(recoveryconfcontents);
2285 :
2286 : /*
2287 : * End of copy data. Final result is already checked inside the loop.
2288 : */
2289 304 : PQclear(res);
2290 304 : PQfinish(conn);
2291 304 : conn = NULL;
2292 :
2293 : /*
2294 : * Make data persistent on disk once backup is completed. For tar format
2295 : * sync the parent directory and all its contents as each tar file was not
2296 : * synced after being completed. In plain format, all the data of the
2297 : * base directory is synced, taking into account all the tablespaces.
2298 : * Errors are not considered fatal.
2299 : *
2300 : * If, however, there's a backup target, we're not writing anything
2301 : * locally, so in that case we skip this step.
2302 : */
2303 304 : if (do_sync && backup_target == NULL)
2304 : {
2305 0 : if (verbose)
2306 0 : pg_log_info("syncing data to disk ...");
2307 0 : if (format == 't')
2308 : {
2309 0 : if (strcmp(basedir, "-") != 0)
2310 0 : (void) sync_dir_recurse(basedir, sync_method);
2311 : }
2312 : else
2313 : {
2314 0 : (void) sync_pgdata(basedir, serverVersion, sync_method, true);
2315 : }
2316 : }
2317 :
2318 : /*
2319 : * After synchronizing data to disk, perform a durable rename of
2320 : * backup_manifest.tmp to backup_manifest, if we wrote such a file. This
2321 : * way, a failure or system crash before we reach this point will leave us
2322 : * without a backup_manifest file, decreasing the chances that a directory
2323 : * we leave behind will be mistaken for a valid backup.
2324 : */
2325 304 : if (!writing_to_stdout && manifest && backup_target == NULL)
2326 : {
2327 : char tmp_filename[MAXPGPATH];
2328 : char filename[MAXPGPATH];
2329 :
2330 286 : if (verbose)
2331 0 : pg_log_info("renaming backup_manifest.tmp to backup_manifest");
2332 :
2333 286 : snprintf(tmp_filename, MAXPGPATH, "%s/backup_manifest.tmp", basedir);
2334 286 : snprintf(filename, MAXPGPATH, "%s/backup_manifest", basedir);
2335 :
2336 286 : if (do_sync)
2337 : {
2338 : /* durable_rename emits its own log message in case of failure */
2339 0 : if (durable_rename(tmp_filename, filename) != 0)
2340 0 : exit(1);
2341 : }
2342 : else
2343 : {
2344 286 : if (rename(tmp_filename, filename) != 0)
2345 0 : pg_fatal("could not rename file \"%s\" to \"%s\": %m",
2346 : tmp_filename, filename);
2347 : }
2348 : }
2349 :
2350 304 : if (verbose)
2351 0 : pg_log_info("base backup completed");
2352 304 : }
2353 :
2354 :
2355 : int
2356 422 : main(int argc, char **argv)
2357 : {
2358 : static struct option long_options[] = {
2359 : {"help", no_argument, NULL, '?'},
2360 : {"version", no_argument, NULL, 'V'},
2361 : {"pgdata", required_argument, NULL, 'D'},
2362 : {"format", required_argument, NULL, 'F'},
2363 : {"incremental", required_argument, NULL, 'i'},
2364 : {"checkpoint", required_argument, NULL, 'c'},
2365 : {"create-slot", no_argument, NULL, 'C'},
2366 : {"max-rate", required_argument, NULL, 'r'},
2367 : {"write-recovery-conf", no_argument, NULL, 'R'},
2368 : {"slot", required_argument, NULL, 'S'},
2369 : {"target", required_argument, NULL, 't'},
2370 : {"tablespace-mapping", required_argument, NULL, 'T'},
2371 : {"wal-method", required_argument, NULL, 'X'},
2372 : {"gzip", no_argument, NULL, 'z'},
2373 : {"compress", required_argument, NULL, 'Z'},
2374 : {"label", required_argument, NULL, 'l'},
2375 : {"no-clean", no_argument, NULL, 'n'},
2376 : {"no-sync", no_argument, NULL, 'N'},
2377 : {"dbname", required_argument, NULL, 'd'},
2378 : {"host", required_argument, NULL, 'h'},
2379 : {"port", required_argument, NULL, 'p'},
2380 : {"username", required_argument, NULL, 'U'},
2381 : {"no-password", no_argument, NULL, 'w'},
2382 : {"password", no_argument, NULL, 'W'},
2383 : {"status-interval", required_argument, NULL, 's'},
2384 : {"verbose", no_argument, NULL, 'v'},
2385 : {"progress", no_argument, NULL, 'P'},
2386 : {"waldir", required_argument, NULL, 1},
2387 : {"no-slot", no_argument, NULL, 2},
2388 : {"no-verify-checksums", no_argument, NULL, 3},
2389 : {"no-estimate-size", no_argument, NULL, 4},
2390 : {"no-manifest", no_argument, NULL, 5},
2391 : {"manifest-force-encode", no_argument, NULL, 6},
2392 : {"manifest-checksums", required_argument, NULL, 7},
2393 : {"sync-method", required_argument, NULL, 8},
2394 : {NULL, 0, NULL, 0}
2395 : };
2396 : int c;
2397 :
2398 : int option_index;
2399 422 : char *compression_algorithm = "none";
2400 422 : char *compression_detail = NULL;
2401 422 : char *incremental_manifest = NULL;
2402 422 : CompressionLocation compressloc = COMPRESS_LOCATION_UNSPECIFIED;
2403 : pg_compress_specification client_compress;
2404 :
2405 422 : pg_logging_init(argv[0]);
2406 422 : progname = get_progname(argv[0]);
2407 422 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_basebackup"));
2408 :
2409 422 : if (argc > 1)
2410 : {
2411 420 : if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
2412 : {
2413 2 : usage();
2414 2 : exit(0);
2415 : }
2416 418 : else if (strcmp(argv[1], "-V") == 0
2417 418 : || strcmp(argv[1], "--version") == 0)
2418 : {
2419 2 : puts("pg_basebackup (PostgreSQL) " PG_VERSION);
2420 2 : exit(0);
2421 : }
2422 : }
2423 :
2424 418 : atexit(cleanup_directories_atexit);
2425 :
2426 2194 : while ((c = getopt_long(argc, argv, "c:Cd:D:F:h:i:l:nNp:Pr:Rs:S:t:T:U:vwWX:zZ:",
2427 : long_options, &option_index)) != -1)
2428 : {
2429 1790 : switch (c)
2430 : {
2431 370 : case 'c':
2432 370 : if (pg_strcasecmp(optarg, "fast") == 0)
2433 370 : fastcheckpoint = true;
2434 0 : else if (pg_strcasecmp(optarg, "spread") == 0)
2435 0 : fastcheckpoint = false;
2436 : else
2437 0 : pg_fatal("invalid checkpoint argument \"%s\", must be \"fast\" or \"spread\"",
2438 : optarg);
2439 370 : break;
2440 8 : case 'C':
2441 8 : create_slot = true;
2442 8 : break;
2443 4 : case 'd':
2444 4 : connection_string = pg_strdup(optarg);
2445 4 : break;
2446 382 : case 'D':
2447 382 : basedir = pg_strdup(optarg);
2448 382 : break;
2449 64 : case 'F':
2450 64 : if (strcmp(optarg, "p") == 0 || strcmp(optarg, "plain") == 0)
2451 24 : format = 'p';
2452 40 : else if (strcmp(optarg, "t") == 0 || strcmp(optarg, "tar") == 0)
2453 40 : format = 't';
2454 : else
2455 0 : pg_fatal("invalid output format \"%s\", must be \"plain\" or \"tar\"",
2456 : optarg);
2457 64 : break;
2458 134 : case 'h':
2459 134 : dbhost = pg_strdup(optarg);
2460 134 : break;
2461 24 : case 'i':
2462 24 : incremental_manifest = pg_strdup(optarg);
2463 24 : break;
2464 0 : case 'l':
2465 0 : label = pg_strdup(optarg);
2466 0 : break;
2467 2 : case 'n':
2468 2 : noclean = true;
2469 2 : break;
2470 370 : case 'N':
2471 370 : do_sync = false;
2472 370 : break;
2473 134 : case 'p':
2474 134 : dbport = pg_strdup(optarg);
2475 134 : break;
2476 0 : case 'P':
2477 0 : showprogress = true;
2478 0 : break;
2479 2 : case 'r':
2480 2 : maxrate = parse_max_rate(optarg);
2481 2 : break;
2482 6 : case 'R':
2483 6 : writerecoveryconf = true;
2484 6 : break;
2485 0 : case 's':
2486 0 : if (!option_parse_int(optarg, "-s/--status-interval", 0,
2487 : INT_MAX / 1000,
2488 : &standby_message_timeout))
2489 0 : exit(1);
2490 0 : standby_message_timeout *= 1000;
2491 0 : break;
2492 14 : case 'S':
2493 :
2494 : /*
2495 : * When specifying replication slot name, use a permanent
2496 : * slot.
2497 : */
2498 14 : replication_slot = pg_strdup(optarg);
2499 14 : temp_replication_slot = false;
2500 14 : break;
2501 36 : case 't':
2502 36 : backup_target = pg_strdup(optarg);
2503 36 : break;
2504 44 : case 'T':
2505 44 : tablespace_list_append(optarg);
2506 32 : break;
2507 14 : case 'U':
2508 14 : dbuser = pg_strdup(optarg);
2509 14 : break;
2510 0 : case 'v':
2511 0 : verbose++;
2512 0 : break;
2513 0 : case 'w':
2514 0 : dbgetpassword = -1;
2515 0 : break;
2516 0 : case 'W':
2517 0 : dbgetpassword = 1;
2518 0 : break;
2519 78 : case 'X':
2520 78 : if (strcmp(optarg, "n") == 0 ||
2521 78 : strcmp(optarg, "none") == 0)
2522 : {
2523 26 : includewal = NO_WAL;
2524 : }
2525 52 : else if (strcmp(optarg, "f") == 0 ||
2526 52 : strcmp(optarg, "fetch") == 0)
2527 : {
2528 34 : includewal = FETCH_WAL;
2529 : }
2530 18 : else if (strcmp(optarg, "s") == 0 ||
2531 18 : strcmp(optarg, "stream") == 0)
2532 : {
2533 18 : includewal = STREAM_WAL;
2534 : }
2535 : else
2536 0 : pg_fatal("invalid wal-method option \"%s\", must be \"fetch\", \"stream\", or \"none\"",
2537 : optarg);
2538 78 : break;
2539 2 : case 'z':
2540 2 : compression_algorithm = "gzip";
2541 2 : compression_detail = NULL;
2542 2 : compressloc = COMPRESS_LOCATION_UNSPECIFIED;
2543 2 : break;
2544 60 : case 'Z':
2545 60 : backup_parse_compress_options(optarg, &compression_algorithm,
2546 : &compression_detail, &compressloc);
2547 60 : break;
2548 2 : case 1:
2549 2 : xlog_dir = pg_strdup(optarg);
2550 2 : break;
2551 4 : case 2:
2552 4 : no_slot = true;
2553 4 : break;
2554 2 : case 3:
2555 2 : verify_checksums = false;
2556 2 : break;
2557 0 : case 4:
2558 0 : estimatesize = false;
2559 0 : break;
2560 2 : case 5:
2561 2 : manifest = false;
2562 2 : break;
2563 2 : case 6:
2564 2 : manifest_force_encode = true;
2565 2 : break;
2566 28 : case 7:
2567 28 : manifest_checksums = pg_strdup(optarg);
2568 28 : break;
2569 0 : case 8:
2570 0 : if (!parse_sync_method(optarg, &sync_method))
2571 0 : exit(1);
2572 0 : break;
2573 2 : default:
2574 : /* getopt_long already emitted a complaint */
2575 2 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2576 2 : exit(1);
2577 : }
2578 : }
2579 :
2580 : /*
2581 : * Any non-option arguments?
2582 : */
2583 404 : if (optind < argc)
2584 : {
2585 0 : pg_log_error("too many command-line arguments (first is \"%s\")",
2586 : argv[optind]);
2587 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2588 0 : exit(1);
2589 : }
2590 :
2591 : /*
2592 : * Setting the backup target to 'client' is equivalent to leaving out the
2593 : * option. This logic allows us to assume elsewhere that the backup is
2594 : * being stored locally if and only if backup_target == NULL.
2595 : */
2596 404 : if (backup_target != NULL && strcmp(backup_target, "client") == 0)
2597 : {
2598 0 : pg_free(backup_target);
2599 0 : backup_target = NULL;
2600 : }
2601 :
2602 : /*
2603 : * Can't use --format with --target. Without --target, default format is
2604 : * tar.
2605 : */
2606 404 : if (backup_target != NULL && format != '\0')
2607 : {
2608 2 : pg_log_error("cannot specify both format and backup target");
2609 2 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2610 2 : exit(1);
2611 : }
2612 402 : if (format == '\0')
2613 352 : format = 'p';
2614 :
2615 : /*
2616 : * Either directory or backup target should be specified, but not both
2617 : */
2618 402 : if (basedir == NULL && backup_target == NULL)
2619 : {
2620 2 : pg_log_error("must specify output directory or backup target");
2621 2 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2622 2 : exit(1);
2623 : }
2624 400 : if (basedir != NULL && backup_target != NULL)
2625 : {
2626 4 : pg_log_error("cannot specify both output directory and backup target");
2627 4 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2628 4 : exit(1);
2629 : }
2630 :
2631 : /*
2632 : * If the user has not specified where to perform backup compression,
2633 : * default to the client, unless the user specified --target, in which
2634 : * case the server is the only choice.
2635 : */
2636 396 : if (compressloc == COMPRESS_LOCATION_UNSPECIFIED)
2637 : {
2638 364 : if (backup_target == NULL)
2639 338 : compressloc = COMPRESS_LOCATION_CLIENT;
2640 : else
2641 26 : compressloc = COMPRESS_LOCATION_SERVER;
2642 : }
2643 :
2644 : /*
2645 : * If any compression that we're doing is happening on the client side, we
2646 : * must try to parse the compression algorithm and detail, but if it's all
2647 : * on the server side, then we're just going to pass through whatever was
2648 : * requested and let the server decide what to do.
2649 : */
2650 396 : if (compressloc == COMPRESS_LOCATION_CLIENT)
2651 : {
2652 : pg_compress_algorithm alg;
2653 : char *error_detail;
2654 :
2655 342 : if (!parse_compress_algorithm(compression_algorithm, &alg))
2656 4 : pg_fatal("unrecognized compression algorithm: \"%s\"",
2657 : compression_algorithm);
2658 :
2659 338 : parse_compress_specification(alg, compression_detail, &client_compress);
2660 338 : error_detail = validate_compress_specification(&client_compress);
2661 338 : if (error_detail != NULL)
2662 20 : pg_fatal("invalid compression specification: %s",
2663 : error_detail);
2664 : }
2665 : else
2666 : {
2667 : Assert(compressloc == COMPRESS_LOCATION_SERVER);
2668 54 : client_compress.algorithm = PG_COMPRESSION_NONE;
2669 54 : client_compress.options = 0;
2670 : }
2671 :
2672 : /*
2673 : * Can't perform client-side compression if the backup is not being sent
2674 : * to the client.
2675 : */
2676 372 : if (backup_target != NULL && compressloc == COMPRESS_LOCATION_CLIENT)
2677 : {
2678 0 : pg_log_error("client-side compression is not possible when a backup target is specified");
2679 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2680 0 : exit(1);
2681 : }
2682 :
2683 : /*
2684 : * Client-side compression doesn't make sense unless tar format is in use.
2685 : */
2686 372 : if (format == 'p' && compressloc == COMPRESS_LOCATION_CLIENT &&
2687 280 : client_compress.algorithm != PG_COMPRESSION_NONE)
2688 : {
2689 0 : pg_log_error("only tar mode backups can be compressed");
2690 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2691 0 : exit(1);
2692 : }
2693 :
2694 : /*
2695 : * Sanity checks for WAL method.
2696 : */
2697 372 : if (backup_target != NULL && includewal == STREAM_WAL)
2698 : {
2699 4 : pg_log_error("WAL cannot be streamed when a backup target is specified");
2700 4 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2701 4 : exit(1);
2702 : }
2703 368 : if (format == 't' && includewal == STREAM_WAL && strcmp(basedir, "-") == 0)
2704 : {
2705 0 : pg_log_error("cannot stream write-ahead logs in tar mode to stdout");
2706 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2707 0 : exit(1);
2708 : }
2709 :
2710 368 : if (replication_slot && includewal != STREAM_WAL)
2711 : {
2712 2 : pg_log_error("replication slots can only be used with WAL streaming");
2713 2 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2714 2 : exit(1);
2715 : }
2716 :
2717 : /*
2718 : * Sanity checks for replication slot options.
2719 : */
2720 366 : if (no_slot)
2721 : {
2722 4 : if (replication_slot)
2723 : {
2724 2 : pg_log_error("--no-slot cannot be used with slot name");
2725 2 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2726 2 : exit(1);
2727 : }
2728 2 : temp_replication_slot = false;
2729 : }
2730 :
2731 364 : if (create_slot)
2732 : {
2733 6 : if (!replication_slot)
2734 : {
2735 2 : pg_log_error("%s needs a slot to be specified using --slot",
2736 : "--create-slot");
2737 2 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2738 2 : exit(1);
2739 : }
2740 :
2741 4 : if (no_slot)
2742 : {
2743 0 : pg_log_error("%s and %s are incompatible options",
2744 : "--create-slot", "--no-slot");
2745 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2746 0 : exit(1);
2747 : }
2748 : }
2749 :
2750 : /*
2751 : * Sanity checks on WAL directory.
2752 : */
2753 362 : if (xlog_dir)
2754 : {
2755 2 : if (backup_target != NULL)
2756 : {
2757 0 : pg_log_error("WAL directory location cannot be specified along with a backup target");
2758 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2759 0 : exit(1);
2760 : }
2761 2 : if (format != 'p')
2762 : {
2763 0 : pg_log_error("WAL directory location can only be specified in plain mode");
2764 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2765 0 : exit(1);
2766 : }
2767 :
2768 : /* clean up xlog directory name, check it's absolute */
2769 2 : canonicalize_path(xlog_dir);
2770 2 : if (!is_absolute_path(xlog_dir))
2771 : {
2772 0 : pg_log_error("WAL directory location must be an absolute path");
2773 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2774 0 : exit(1);
2775 : }
2776 : }
2777 :
2778 : /*
2779 : * Sanity checks for progress reporting options.
2780 : */
2781 362 : if (showprogress && !estimatesize)
2782 : {
2783 0 : pg_log_error("%s and %s are incompatible options",
2784 : "--progress", "--no-estimate-size");
2785 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2786 0 : exit(1);
2787 : }
2788 :
2789 : /*
2790 : * Sanity checks for backup manifest options.
2791 : */
2792 362 : if (!manifest && manifest_checksums != NULL)
2793 : {
2794 0 : pg_log_error("%s and %s are incompatible options",
2795 : "--no-manifest", "--manifest-checksums");
2796 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2797 0 : exit(1);
2798 : }
2799 :
2800 362 : if (!manifest && manifest_force_encode)
2801 : {
2802 0 : pg_log_error("%s and %s are incompatible options",
2803 : "--no-manifest", "--manifest-force-encode");
2804 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2805 0 : exit(1);
2806 : }
2807 :
2808 : /* connection in replication mode to server */
2809 362 : conn = GetConnection();
2810 362 : if (!conn)
2811 : {
2812 : /* Error message already written in GetConnection() */
2813 4 : exit(1);
2814 : }
2815 358 : atexit(disconnect_atexit);
2816 :
2817 : #ifndef WIN32
2818 :
2819 : /*
2820 : * Trap SIGCHLD to be able to handle the WAL stream process exiting. There
2821 : * is no SIGCHLD on Windows, there we rely on the background thread
2822 : * setting the signal variable on unexpected but graceful exit. If the WAL
2823 : * stream thread crashes on Windows it will bring down the entire process
2824 : * as it's a thread, so there is nothing to catch should that happen. A
2825 : * crash on UNIX will be caught by the signal handler.
2826 : */
2827 358 : pqsignal(SIGCHLD, sigchld_handler);
2828 : #endif
2829 :
2830 : /*
2831 : * Set umask so that directories/files are created with the same
2832 : * permissions as directories/files in the source data directory.
2833 : *
2834 : * pg_mode_mask is set to owner-only by default and then updated in
2835 : * GetConnection() where we get the mode from the server-side with
2836 : * RetrieveDataDirCreatePerm() and then call SetDataDirectoryCreatePerm().
2837 : */
2838 358 : umask(pg_mode_mask);
2839 :
2840 : /* Backup manifests are supported in 13 and newer versions */
2841 358 : if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_MANIFESTS)
2842 0 : manifest = false;
2843 :
2844 : /*
2845 : * If an output directory was specified, verify that it exists, or create
2846 : * it. Note that for a tar backup, an output directory of "-" means we are
2847 : * writing to stdout, so do nothing in that case.
2848 : */
2849 358 : if (basedir != NULL && (format == 'p' || strcmp(basedir, "-") != 0))
2850 332 : verify_dir_is_empty_or_create(basedir, &made_new_pgdata, &found_existing_pgdata);
2851 :
2852 : /* determine remote server's xlog segment size */
2853 358 : if (!RetrieveWalSegSize(conn))
2854 0 : exit(1);
2855 :
2856 : /* Create pg_wal symlink, if required */
2857 358 : if (xlog_dir)
2858 : {
2859 : char *linkloc;
2860 :
2861 2 : verify_dir_is_empty_or_create(xlog_dir, &made_new_xlogdir, &found_existing_xlogdir);
2862 :
2863 : /*
2864 : * Form name of the place where the symlink must go. pg_xlog has been
2865 : * renamed to pg_wal in post-10 clusters.
2866 : */
2867 2 : linkloc = psprintf("%s/%s", basedir,
2868 2 : PQserverVersion(conn) < MINIMUM_VERSION_FOR_PG_WAL ?
2869 : "pg_xlog" : "pg_wal");
2870 :
2871 2 : if (symlink(xlog_dir, linkloc) != 0)
2872 0 : pg_fatal("could not create symbolic link \"%s\": %m", linkloc);
2873 2 : free(linkloc);
2874 : }
2875 :
2876 358 : BaseBackup(compression_algorithm, compression_detail, compressloc,
2877 : &client_compress, incremental_manifest);
2878 :
2879 304 : success = true;
2880 304 : return 0;
2881 : }
|