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