Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * pg_restore.c
4 : * pg_restore is an utility extracting postgres database definitions
5 : * from a backup archive created by pg_dump/pg_dumpall using the archiver
6 : * interface.
7 : *
8 : * pg_restore will read the backup archive and
9 : * dump out a script that reproduces
10 : * the schema of the database in terms of
11 : * user-defined types
12 : * user-defined functions
13 : * tables
14 : * indexes
15 : * aggregates
16 : * operators
17 : * ACL - grant/revoke
18 : *
19 : * the output script is SQL that is understood by PostgreSQL
20 : *
21 : * Basic process in a restore operation is:
22 : *
23 : * Open the Archive and read the TOC.
24 : * Set flags in TOC entries, and *maybe* reorder them.
25 : * Generate script to stdout
26 : * Exit
27 : *
28 : * Copyright (c) 2000, Philip Warner
29 : * Rights are granted to use this software in any way so long
30 : * as this notice is not removed.
31 : *
32 : * The author is not responsible for loss or damages that may
33 : * result from its use.
34 : *
35 : *
36 : * IDENTIFICATION
37 : * src/bin/pg_dump/pg_restore.c
38 : *
39 : *-------------------------------------------------------------------------
40 : */
41 : #include "postgres_fe.h"
42 :
43 : #include <ctype.h>
44 : #include <sys/stat.h>
45 : #ifdef HAVE_TERMIOS_H
46 : #include <termios.h>
47 : #endif
48 :
49 : #include "common/string.h"
50 : #include "connectdb.h"
51 : #include "dumputils.h"
52 : #include "fe_utils/option_utils.h"
53 : #include "fe_utils/string_utils.h"
54 : #include "filter.h"
55 : #include "getopt_long.h"
56 : #include "parallel.h"
57 : #include "pg_backup_utils.h"
58 :
59 : static void usage(const char *progname);
60 : static void read_restore_filters(const char *filename, RestoreOptions *opts);
61 : static bool file_exists_in_directory(const char *dir, const char *filename);
62 : static int restore_one_database(const char *inputFileSpec, RestoreOptions *opts,
63 : int numWorkers, bool append_data);
64 : static int restore_global_objects(const char *inputFileSpec, RestoreOptions *opts);
65 :
66 : static int restore_all_databases(const char *inputFileSpec,
67 : SimpleStringList db_exclude_patterns, RestoreOptions *opts, int numWorkers);
68 : static int get_dbnames_list_to_restore(PGconn *conn,
69 : SimplePtrList *dbname_oid_list,
70 : SimpleStringList db_exclude_patterns);
71 : static int get_dbname_oid_list_from_mfile(char *dumpdirpath,
72 : SimplePtrList *dbname_oid_list);
73 :
74 : /*
75 : * Stores a database OID and the corresponding name.
76 : */
77 : typedef struct DbOidName
78 : {
79 : Oid oid;
80 : char str[FLEXIBLE_ARRAY_MEMBER]; /* null-terminated string here */
81 : } DbOidName;
82 :
83 :
84 : int
85 128 : main(int argc, char **argv)
86 : {
87 : RestoreOptions *opts;
88 : int c;
89 128 : int numWorkers = 1;
90 : char *inputFileSpec;
91 128 : bool data_only = false;
92 128 : bool schema_only = false;
93 128 : int n_errors = 0;
94 128 : bool globals_only = false;
95 128 : SimpleStringList db_exclude_patterns = {NULL, NULL};
96 : static int disable_triggers = 0;
97 : static int enable_row_security = 0;
98 : static int if_exists = 0;
99 : static int no_data_for_failed_tables = 0;
100 : static int outputNoTableAm = 0;
101 : static int outputNoTablespaces = 0;
102 : static int use_setsessauth = 0;
103 : static int no_comments = 0;
104 : static int no_data = 0;
105 : static int no_policies = 0;
106 : static int no_publications = 0;
107 : static int no_schema = 0;
108 : static int no_security_labels = 0;
109 : static int no_statistics = 0;
110 : static int no_subscriptions = 0;
111 : static int strict_names = 0;
112 : static int statistics_only = 0;
113 : static int with_statistics = 0;
114 :
115 128 : struct option cmdopts[] = {
116 : {"clean", 0, NULL, 'c'},
117 : {"create", 0, NULL, 'C'},
118 : {"data-only", 0, NULL, 'a'},
119 : {"globals-only", 0, NULL, 'g'},
120 : {"dbname", 1, NULL, 'd'},
121 : {"exit-on-error", 0, NULL, 'e'},
122 : {"exclude-schema", 1, NULL, 'N'},
123 : {"file", 1, NULL, 'f'},
124 : {"format", 1, NULL, 'F'},
125 : {"function", 1, NULL, 'P'},
126 : {"host", 1, NULL, 'h'},
127 : {"index", 1, NULL, 'I'},
128 : {"jobs", 1, NULL, 'j'},
129 : {"list", 0, NULL, 'l'},
130 : {"no-privileges", 0, NULL, 'x'},
131 : {"no-acl", 0, NULL, 'x'},
132 : {"no-owner", 0, NULL, 'O'},
133 : {"no-reconnect", 0, NULL, 'R'},
134 : {"port", 1, NULL, 'p'},
135 : {"no-password", 0, NULL, 'w'},
136 : {"password", 0, NULL, 'W'},
137 : {"schema", 1, NULL, 'n'},
138 : {"schema-only", 0, NULL, 's'},
139 : {"superuser", 1, NULL, 'S'},
140 : {"table", 1, NULL, 't'},
141 : {"trigger", 1, NULL, 'T'},
142 : {"use-list", 1, NULL, 'L'},
143 : {"username", 1, NULL, 'U'},
144 : {"verbose", 0, NULL, 'v'},
145 : {"single-transaction", 0, NULL, '1'},
146 :
147 : /*
148 : * the following options don't have an equivalent short option letter
149 : */
150 : {"disable-triggers", no_argument, &disable_triggers, 1},
151 : {"enable-row-security", no_argument, &enable_row_security, 1},
152 : {"if-exists", no_argument, &if_exists, 1},
153 : {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
154 : {"no-table-access-method", no_argument, &outputNoTableAm, 1},
155 : {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
156 : {"role", required_argument, NULL, 2},
157 : {"section", required_argument, NULL, 3},
158 : {"strict-names", no_argument, &strict_names, 1},
159 : {"transaction-size", required_argument, NULL, 5},
160 : {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
161 : {"no-comments", no_argument, &no_comments, 1},
162 : {"no-data", no_argument, &no_data, 1},
163 : {"no-policies", no_argument, &no_policies, 1},
164 : {"no-publications", no_argument, &no_publications, 1},
165 : {"no-schema", no_argument, &no_schema, 1},
166 : {"no-security-labels", no_argument, &no_security_labels, 1},
167 : {"no-subscriptions", no_argument, &no_subscriptions, 1},
168 : {"no-statistics", no_argument, &no_statistics, 1},
169 : {"statistics", no_argument, &with_statistics, 1},
170 : {"statistics-only", no_argument, &statistics_only, 1},
171 : {"filter", required_argument, NULL, 4},
172 : {"restrict-key", required_argument, NULL, 6},
173 : {"exclude-database", required_argument, NULL, 7},
174 :
175 : {NULL, 0, NULL, 0}
176 : };
177 :
178 128 : pg_logging_init(argv[0]);
179 128 : pg_logging_set_level(PG_LOG_WARNING);
180 128 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
181 :
182 128 : init_parallel_dump_utils();
183 :
184 128 : opts = NewRestoreOptions();
185 :
186 128 : progname = get_progname(argv[0]);
187 :
188 128 : if (argc > 1)
189 : {
190 127 : if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
191 : {
192 1 : usage(progname);
193 1 : exit_nicely(0);
194 : }
195 126 : if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
196 : {
197 19 : puts("pg_restore (PostgreSQL) " PG_VERSION);
198 19 : exit_nicely(0);
199 : }
200 : }
201 :
202 613 : while ((c = getopt_long(argc, argv, "acCd:ef:F:gh:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1",
203 613 : cmdopts, NULL)) != -1)
204 : {
205 511 : switch (c)
206 : {
207 5 : case 'a': /* Dump data only */
208 5 : data_only = true;
209 5 : break;
210 21 : case 'c': /* clean (i.e., drop) schema prior to create */
211 21 : opts->dropSchema = 1;
212 21 : break;
213 58 : case 'C':
214 58 : opts->createDB = 1;
215 58 : break;
216 42 : case 'd':
217 42 : opts->cparams.dbname = pg_strdup(optarg);
218 42 : break;
219 31 : case 'e':
220 31 : opts->exit_on_error = true;
221 31 : break;
222 59 : case 'f': /* output file name */
223 59 : opts->filename = pg_strdup(optarg);
224 59 : break;
225 40 : case 'F':
226 40 : if (strlen(optarg) != 0)
227 40 : opts->formatName = pg_strdup(optarg);
228 40 : break;
229 14 : case 'g':
230 : /* restore only global sql commands. */
231 14 : globals_only = true;
232 14 : break;
233 39 : case 'h':
234 39 : if (strlen(optarg) != 0)
235 39 : opts->cparams.pghost = pg_strdup(optarg);
236 39 : break;
237 7 : case 'j': /* number of restore jobs */
238 7 : if (!option_parse_int(optarg, "-j/--jobs", 1,
239 : PG_MAX_JOBS,
240 : &numWorkers))
241 1 : exit(1);
242 6 : break;
243 :
244 5 : case 'l': /* Dump the TOC summary */
245 5 : opts->tocSummary = 1;
246 5 : break;
247 :
248 1 : case 'L': /* input TOC summary file name */
249 1 : opts->tocFile = pg_strdup(optarg);
250 1 : break;
251 :
252 0 : case 'n': /* Dump data for this schema only */
253 0 : simple_string_list_append(&opts->schemaNames, optarg);
254 0 : break;
255 :
256 0 : case 'N': /* Do not dump data for this schema */
257 0 : simple_string_list_append(&opts->schemaExcludeNames, optarg);
258 0 : break;
259 :
260 0 : case 'O':
261 0 : opts->noOwner = 1;
262 0 : break;
263 :
264 49 : case 'p':
265 49 : if (strlen(optarg) != 0)
266 49 : opts->cparams.pgport = pg_strdup(optarg);
267 49 : break;
268 0 : case 'R':
269 : /* no-op, still accepted for backwards compatibility */
270 0 : break;
271 0 : case 'P': /* Function */
272 0 : opts->selTypes = 1;
273 0 : opts->selFunction = 1;
274 0 : simple_string_list_append(&opts->functionNames, optarg);
275 0 : break;
276 0 : case 'I': /* Index */
277 0 : opts->selTypes = 1;
278 0 : opts->selIndex = 1;
279 0 : simple_string_list_append(&opts->indexNames, optarg);
280 0 : break;
281 0 : case 'T': /* Trigger */
282 0 : opts->selTypes = 1;
283 0 : opts->selTrigger = 1;
284 0 : simple_string_list_append(&opts->triggerNames, optarg);
285 0 : break;
286 3 : case 's': /* dump schema only */
287 3 : schema_only = true;
288 3 : break;
289 0 : case 'S': /* Superuser username */
290 0 : if (strlen(optarg) != 0)
291 0 : opts->superuser = pg_strdup(optarg);
292 0 : break;
293 0 : case 't': /* Dump specified table(s) only */
294 0 : opts->selTypes = 1;
295 0 : opts->selTable = 1;
296 0 : simple_string_list_append(&opts->tableNames, optarg);
297 0 : break;
298 :
299 32 : case 'U':
300 32 : opts->cparams.username = pg_strdup(optarg);
301 32 : break;
302 :
303 35 : case 'v': /* verbose */
304 35 : opts->verbose = 1;
305 35 : pg_logging_increase_verbosity();
306 35 : break;
307 :
308 0 : case 'w':
309 0 : opts->cparams.promptPassword = TRI_NO;
310 0 : break;
311 :
312 0 : case 'W':
313 0 : opts->cparams.promptPassword = TRI_YES;
314 0 : break;
315 :
316 0 : case 'x': /* skip ACL dump */
317 0 : opts->aclsSkip = 1;
318 0 : break;
319 :
320 3 : case '1': /* Restore data in a single transaction */
321 3 : opts->single_txn = true;
322 3 : opts->exit_on_error = true;
323 3 : break;
324 :
325 21 : case 0:
326 :
327 : /*
328 : * This covers the long options without a short equivalent.
329 : */
330 21 : break;
331 :
332 0 : case 2: /* SET ROLE */
333 0 : opts->use_role = pg_strdup(optarg);
334 0 : break;
335 :
336 1 : case 3: /* section */
337 1 : set_dump_section(optarg, &(opts->dumpSections));
338 1 : break;
339 :
340 10 : case 4: /* filter */
341 10 : read_restore_filters(optarg, opts);
342 6 : break;
343 :
344 31 : case 5: /* transaction-size */
345 31 : if (!option_parse_int(optarg, "--transaction-size",
346 : 1, INT_MAX,
347 : &opts->txn_size))
348 0 : exit(1);
349 31 : opts->exit_on_error = true;
350 31 : break;
351 :
352 0 : case 6:
353 0 : opts->restrict_key = pg_strdup(optarg);
354 0 : break;
355 :
356 3 : case 7: /* database patterns to skip */
357 3 : simple_string_list_append(&db_exclude_patterns, optarg);
358 3 : break;
359 :
360 1 : default:
361 : /* getopt_long already emitted a complaint */
362 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
363 1 : exit_nicely(1);
364 : }
365 : }
366 :
367 : /* Get file name from command line */
368 102 : if (optind < argc)
369 90 : inputFileSpec = argv[optind++];
370 : else
371 12 : inputFileSpec = NULL;
372 :
373 : /* Complain if any arguments remain */
374 102 : if (optind < argc)
375 : {
376 1 : pg_log_error("too many command-line arguments (first is \"%s\")",
377 : argv[optind]);
378 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
379 1 : exit_nicely(1);
380 : }
381 :
382 : /* Complain if neither -f nor -d was specified (except if dumping TOC) */
383 101 : if (!opts->cparams.dbname && !opts->filename && !opts->tocSummary)
384 1 : pg_fatal("one of -d/--dbname and -f/--file must be specified");
385 :
386 100 : if (db_exclude_patterns.head != NULL && globals_only)
387 : {
388 1 : pg_log_error("option %s cannot be used together with %s",
389 : "--exclude-database", "-g/--globals-only");
390 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
391 1 : exit_nicely(1);
392 : }
393 :
394 : /* Should get at most one of -d and -f, else user is confused */
395 99 : if (opts->cparams.dbname)
396 : {
397 41 : if (opts->filename)
398 : {
399 1 : pg_log_error("options %s and %s cannot be used together",
400 : "-d/--dbname", "-f/--file");
401 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
402 1 : exit_nicely(1);
403 : }
404 :
405 40 : if (opts->restrict_key)
406 0 : pg_fatal("options %s and %s cannot be used together",
407 : "-d/--dbname", "--restrict-key");
408 :
409 40 : opts->useDB = 1;
410 : }
411 : else
412 : {
413 : /*
414 : * If you don't provide a restrict key, one will be appointed for you.
415 : */
416 58 : if (!opts->restrict_key)
417 58 : opts->restrict_key = generate_restrict_key();
418 58 : if (!opts->restrict_key)
419 0 : pg_fatal("could not generate restrict key");
420 58 : if (!valid_restrict_key(opts->restrict_key))
421 0 : pg_fatal("invalid restrict key");
422 : }
423 :
424 : /* reject conflicting "-only" options */
425 98 : if (data_only && schema_only)
426 1 : pg_fatal("options %s and %s cannot be used together",
427 : "-s/--schema-only", "-a/--data-only");
428 97 : if (schema_only && statistics_only)
429 0 : pg_fatal("options %s and %s cannot be used together",
430 : "-s/--schema-only", "--statistics-only");
431 97 : if (data_only && statistics_only)
432 0 : pg_fatal("options %s and %s cannot be used together",
433 : "-a/--data-only", "--statistics-only");
434 :
435 : /* reject conflicting "-only" and "no-" options */
436 97 : if (data_only && no_data)
437 0 : pg_fatal("options %s and %s cannot be used together",
438 : "-a/--data-only", "--no-data");
439 97 : if (schema_only && no_schema)
440 0 : pg_fatal("options %s and %s cannot be used together",
441 : "-s/--schema-only", "--no-schema");
442 97 : if (statistics_only && no_statistics)
443 0 : pg_fatal("options %s and %s cannot be used together",
444 : "--statistics-only", "--no-statistics");
445 :
446 : /* reject conflicting "no-" options */
447 97 : if (with_statistics && no_statistics)
448 0 : pg_fatal("options %s and %s cannot be used together",
449 : "--statistics", "--no-statistics");
450 :
451 : /* reject conflicting "only-" options */
452 97 : if (data_only && with_statistics)
453 0 : pg_fatal("options %s and %s cannot be used together",
454 : "-a/--data-only", "--statistics");
455 97 : if (schema_only && with_statistics)
456 0 : pg_fatal("options %s and %s cannot be used together",
457 : "-s/--schema-only", "--statistics");
458 :
459 97 : if (data_only && opts->dropSchema)
460 1 : pg_fatal("options %s and %s cannot be used together",
461 : "-c/--clean", "-a/--data-only");
462 :
463 96 : if (opts->single_txn && opts->txn_size > 0)
464 0 : pg_fatal("options %s and %s cannot be used together",
465 : "-1/--single-transaction", "--transaction-size");
466 :
467 96 : if (opts->single_txn && globals_only)
468 1 : pg_fatal("options %s and %s cannot be used together when restoring an archive created by pg_dumpall",
469 : "--single-transaction", "-g/--globals-only");
470 :
471 95 : if (opts->txn_size && globals_only)
472 1 : pg_fatal("options %s and %s cannot be used together when restoring an archive created by pg_dumpall",
473 : "--transaction-size", "-g/--globals-only");
474 :
475 94 : if (opts->exit_on_error && globals_only)
476 1 : pg_fatal("options %s and %s cannot be used together when restoring an archive created by pg_dumpall",
477 : "--exit-on-error", "-g/--globals-only");
478 :
479 93 : if (data_only && globals_only)
480 2 : pg_fatal("options %s and %s cannot be used together",
481 : "-a/--data-only", "-g/--globals-only");
482 91 : if (schema_only && globals_only)
483 2 : pg_fatal("options %s and %s cannot be used together",
484 : "-s/--schema-only", "-g/--globals-only");
485 89 : if (statistics_only && globals_only)
486 2 : pg_fatal("options %s and %s cannot be used together",
487 : "--statistics-only", "-g/--globals-only");
488 87 : if (with_statistics && globals_only)
489 1 : pg_fatal("options %s and %s cannot be used together",
490 : "--statistics", "-g/--globals-only");
491 :
492 : /*
493 : * -C is not compatible with -1, because we can't create a database inside
494 : * a transaction block.
495 : */
496 86 : if (opts->createDB && opts->single_txn)
497 1 : pg_fatal("options %s and %s cannot be used together",
498 : "-C/--create", "-1/--single-transaction");
499 :
500 : /* Can't do single-txn mode with multiple connections */
501 85 : if (opts->single_txn && numWorkers > 1)
502 1 : pg_fatal("cannot specify both --single-transaction and multiple jobs");
503 :
504 : /*
505 : * Set derivative flags. Ambiguous or nonsensical combinations, e.g.
506 : * "--schema-only --no-schema", will have already caused an error in one
507 : * of the checks above.
508 : */
509 84 : opts->dumpData = ((opts->dumpData && !schema_only && !statistics_only) ||
510 168 : data_only) && !no_data;
511 84 : opts->dumpSchema = ((opts->dumpSchema && !data_only && !statistics_only) ||
512 168 : schema_only) && !no_schema;
513 84 : opts->dumpStatistics = ((opts->dumpStatistics && !schema_only && !data_only) ||
514 168 : (statistics_only || with_statistics)) && !no_statistics;
515 :
516 84 : opts->disable_triggers = disable_triggers;
517 84 : opts->enable_row_security = enable_row_security;
518 84 : opts->noDataForFailedTables = no_data_for_failed_tables;
519 84 : opts->noTableAm = outputNoTableAm;
520 84 : opts->noTablespace = outputNoTablespaces;
521 84 : opts->use_setsessauth = use_setsessauth;
522 84 : opts->no_comments = no_comments;
523 84 : opts->no_policies = no_policies;
524 84 : opts->no_publications = no_publications;
525 84 : opts->no_security_labels = no_security_labels;
526 84 : opts->no_subscriptions = no_subscriptions;
527 :
528 84 : if (if_exists && !opts->dropSchema)
529 1 : pg_fatal("option %s requires option %s",
530 : "--if-exists", "-c/--clean");
531 83 : opts->if_exists = if_exists;
532 83 : opts->strict_names = strict_names;
533 :
534 83 : if (opts->formatName)
535 : {
536 66 : if (pg_strcasecmp(opts->formatName, "c") == 0 ||
537 33 : pg_strcasecmp(opts->formatName, "custom") == 0)
538 22 : opts->format = archCustom;
539 22 : else if (pg_strcasecmp(opts->formatName, "d") == 0 ||
540 11 : pg_strcasecmp(opts->formatName, "directory") == 0)
541 8 : opts->format = archDirectory;
542 6 : else if (pg_strcasecmp(opts->formatName, "t") == 0 ||
543 3 : pg_strcasecmp(opts->formatName, "tar") == 0)
544 2 : opts->format = archTar;
545 2 : else if (pg_strcasecmp(opts->formatName, "p") == 0 ||
546 1 : pg_strcasecmp(opts->formatName, "plain") == 0)
547 : {
548 : /* recognize this for consistency with pg_dump */
549 0 : pg_fatal("archive format \"%s\" is not supported; please use psql",
550 : opts->formatName);
551 : }
552 : else
553 1 : pg_fatal("unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
554 : opts->formatName);
555 : }
556 :
557 : /*
558 : * If toc.glo file is present, then restore all the databases from
559 : * map.dat, but skip restoring those matching --exclude-database patterns.
560 : */
561 164 : if (inputFileSpec != NULL &&
562 82 : (file_exists_in_directory(inputFileSpec, "toc.glo")))
563 10 : {
564 : char global_path[MAXPGPATH];
565 20 : RestoreOptions *tmpopts = pg_malloc0_object(RestoreOptions);
566 :
567 20 : opts->format = archUnknown;
568 :
569 20 : memcpy(tmpopts, opts, sizeof(RestoreOptions));
570 :
571 : /*
572 : * Can only use --list or --use-list options with a single database
573 : * dump.
574 : */
575 20 : if (opts->tocSummary)
576 1 : pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
577 : "-l/--list");
578 19 : if (opts->tocFile)
579 1 : pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
580 : "-L/--use-list");
581 :
582 18 : if (opts->strict_names)
583 1 : pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
584 : "--strict-names");
585 17 : if (globals_only && opts->dropSchema)
586 1 : pg_fatal("options %s and %s cannot be used together when restoring an archive created by pg_dumpall",
587 : "--clean", "-g/--globals-only");
588 :
589 : /*
590 : * For pg_dumpall archives, --clean implies --if-exists since global
591 : * objects may not exist in the target cluster.
592 : */
593 16 : if (opts->dropSchema && !opts->if_exists)
594 : {
595 1 : opts->if_exists = 1;
596 1 : pg_log_info("--if-exists is implied by --clean for pg_dumpall archives");
597 : }
598 :
599 16 : if (no_schema)
600 1 : pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
601 : "--no-schema");
602 :
603 15 : if (data_only)
604 1 : pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
605 : "-a/--data-only");
606 :
607 14 : if (statistics_only)
608 1 : pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
609 : "--statistics-only");
610 :
611 13 : if (!(opts->dumpSections & DUMP_PRE_DATA))
612 1 : pg_fatal("option %s cannot exclude %s when restoring a pg_dumpall archive",
613 : "--section", "--pre-data");
614 :
615 : /*
616 : * To restore from a pg_dumpall archive, -C (create database) option
617 : * must be specified unless we are only restoring globals.
618 : */
619 12 : if (!globals_only && opts->createDB != 1)
620 : {
621 1 : pg_log_error("option %s must be specified when restoring an archive created by pg_dumpall",
622 : "-C/--create");
623 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
624 1 : pg_log_error_hint("Individual databases can be restored using their specific archives.");
625 1 : exit_nicely(1);
626 : }
627 :
628 : /*
629 : * Always restore global objects, even if --exclude-database results
630 : * in zero databases to process. If 'globals-only' is set, exit
631 : * immediately.
632 : */
633 11 : snprintf(global_path, MAXPGPATH, "%s/toc.glo", inputFileSpec);
634 :
635 11 : n_errors = restore_global_objects(global_path, tmpopts);
636 :
637 10 : if (globals_only)
638 1 : pg_log_info("database restoring skipped because option %s was specified",
639 : "-g/--globals-only");
640 : else
641 : {
642 : /* Now restore all the databases from map.dat */
643 9 : n_errors = n_errors + restore_all_databases(inputFileSpec, db_exclude_patterns,
644 : opts, numWorkers);
645 : }
646 :
647 : /* Free db pattern list. */
648 10 : simple_string_list_destroy(&db_exclude_patterns);
649 : }
650 : else
651 : {
652 62 : if (db_exclude_patterns.head != NULL)
653 : {
654 1 : simple_string_list_destroy(&db_exclude_patterns);
655 1 : pg_fatal("option %s can be used only when restoring an archive created by pg_dumpall",
656 : "--exclude-database");
657 : }
658 :
659 61 : if (globals_only)
660 1 : pg_fatal("option %s can be used only when restoring an archive created by pg_dumpall",
661 : "-g/--globals-only");
662 :
663 : /* Process if toc.glo file does not exist. */
664 60 : n_errors = restore_one_database(inputFileSpec, opts, numWorkers, false);
665 : }
666 :
667 : /* Done, print a summary of ignored errors during restore. */
668 70 : if (n_errors)
669 : {
670 0 : pg_log_warning("errors ignored on restore: %d", n_errors);
671 0 : return 1;
672 : }
673 :
674 70 : return 0;
675 : }
676 :
677 : /*
678 : * restore_global_objects
679 : *
680 : * This restore all global objects.
681 : */
682 : static int
683 11 : restore_global_objects(const char *inputFileSpec, RestoreOptions *opts)
684 : {
685 : Archive *AH;
686 11 : int nerror = 0;
687 :
688 : /* Set format as custom so that toc.glo file can be read. */
689 11 : opts->format = archCustom;
690 11 : opts->txn_size = 0;
691 :
692 11 : AH = OpenArchive(inputFileSpec, opts->format);
693 :
694 11 : SetArchiveOptions(AH, NULL, opts);
695 :
696 11 : on_exit_close_archive(AH);
697 :
698 : /* Let the archiver know how noisy to be */
699 11 : AH->verbose = opts->verbose;
700 :
701 : /* Don't output TOC entry comments when restoring globals */
702 11 : ((ArchiveHandle *) AH)->noTocComments = 1;
703 :
704 11 : AH->exit_on_error = false;
705 :
706 : /* Parallel execution is not supported for global object restoration. */
707 11 : AH->numWorkers = 1;
708 :
709 11 : ProcessArchiveRestoreOptions(AH);
710 11 : RestoreArchive(AH, false);
711 :
712 10 : nerror = AH->n_errors;
713 :
714 : /* AH may be freed in CloseArchive? */
715 10 : CloseArchive(AH);
716 :
717 10 : return nerror;
718 : }
719 :
720 : /*
721 : * restore_one_database
722 : *
723 : * This will restore one database using toc.dat file.
724 : *
725 : * returns the number of errors while doing restore.
726 : */
727 : static int
728 129 : restore_one_database(const char *inputFileSpec, RestoreOptions *opts,
729 : int numWorkers, bool append_data)
730 : {
731 : Archive *AH;
732 : int n_errors;
733 :
734 129 : AH = OpenArchive(inputFileSpec, opts->format);
735 :
736 129 : SetArchiveOptions(AH, NULL, opts);
737 :
738 : /*
739 : * We don't have a connection yet but that doesn't matter. The connection
740 : * is initialized to NULL and if we terminate through exit_nicely() while
741 : * it's still NULL, the cleanup function will just be a no-op. If we are
742 : * restoring multiple databases, then only update AX handle for cleanup as
743 : * the previous entry was already in the array and we had closed previous
744 : * connection, so we can use the same array slot.
745 : */
746 129 : if (!append_data)
747 60 : on_exit_close_archive(AH);
748 : else
749 69 : replace_on_exit_close_archive(AH);
750 :
751 : /* Let the archiver know how noisy to be */
752 129 : AH->verbose = opts->verbose;
753 :
754 : /*
755 : * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
756 : */
757 129 : AH->exit_on_error = opts->exit_on_error;
758 :
759 129 : if (opts->tocFile)
760 0 : SortTocFromFile(AH);
761 :
762 129 : AH->numWorkers = numWorkers;
763 :
764 129 : if (opts->tocSummary)
765 4 : PrintTOCSummary(AH);
766 : else
767 : {
768 125 : ProcessArchiveRestoreOptions(AH);
769 125 : RestoreArchive(AH, append_data);
770 : }
771 :
772 129 : n_errors = AH->n_errors;
773 :
774 : /* AH may be freed in CloseArchive? */
775 129 : CloseArchive(AH);
776 :
777 129 : return n_errors;
778 : }
779 :
780 : static void
781 1 : usage(const char *progname)
782 : {
783 1 : printf(_("%s restores PostgreSQL databases from archives created by pg_dump or pg_dumpall.\n\n"), progname);
784 1 : printf(_("Usage:\n"));
785 1 : printf(_(" %s [OPTION]... [FILE]\n"), progname);
786 :
787 1 : printf(_("\nGeneral options:\n"));
788 1 : printf(_(" -d, --dbname=NAME connect to database name\n"));
789 1 : printf(_(" -f, --file=FILENAME output file name (- for stdout)\n"));
790 1 : printf(_(" -F, --format=c|d|t backup file format (should be automatic)\n"));
791 1 : printf(_(" -l, --list print summarized TOC of the archive\n"));
792 1 : printf(_(" -v, --verbose verbose mode\n"));
793 1 : printf(_(" -V, --version output version information, then exit\n"));
794 1 : printf(_(" -?, --help show this help, then exit\n"));
795 :
796 1 : printf(_("\nOptions controlling the restore:\n"));
797 1 : printf(_(" -a, --data-only restore only the data, no schema\n"));
798 1 : printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
799 1 : printf(_(" -C, --create create the target database\n"));
800 1 : printf(_(" -e, --exit-on-error exit on error, default is to continue\n"));
801 1 : printf(_(" -g, --globals-only restore only global objects, no databases\n"));
802 1 : printf(_(" -I, --index=NAME restore named index\n"));
803 1 : printf(_(" -j, --jobs=NUM use this many parallel jobs to restore\n"));
804 1 : printf(_(" -L, --use-list=FILENAME use table of contents from this file for\n"
805 : " selecting/ordering output\n"));
806 1 : printf(_(" -n, --schema=NAME restore only objects in this schema\n"));
807 1 : printf(_(" -N, --exclude-schema=NAME do not restore objects in this schema\n"));
808 1 : printf(_(" -O, --no-owner skip restoration of object ownership\n"));
809 1 : printf(_(" -P, --function=NAME(args) restore named function\n"));
810 1 : printf(_(" -s, --schema-only restore only the schema, no data\n"));
811 1 : printf(_(" -S, --superuser=NAME superuser user name to use for disabling triggers\n"));
812 1 : printf(_(" -t, --table=NAME restore named relation (table, view, etc.)\n"));
813 1 : printf(_(" -T, --trigger=NAME restore named trigger\n"));
814 1 : printf(_(" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
815 1 : printf(_(" -1, --single-transaction restore as a single transaction\n"));
816 1 : printf(_(" --disable-triggers disable triggers during data-only restore\n"));
817 1 : printf(_(" --enable-row-security enable row security\n"));
818 1 : printf(_(" --exclude-database=PATTERN do not restore the specified database(s)\n"));
819 1 : printf(_(" --filter=FILENAME restore or skip objects based on expressions\n"
820 : " in FILENAME\n"));
821 1 : printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
822 1 : printf(_(" --no-comments do not restore comment commands\n"));
823 1 : printf(_(" --no-data do not restore data\n"));
824 1 : printf(_(" --no-data-for-failed-tables do not restore data of tables that could not be\n"
825 : " created\n"));
826 1 : printf(_(" --no-policies do not restore row security policies\n"));
827 1 : printf(_(" --no-publications do not restore publications\n"));
828 1 : printf(_(" --no-schema do not restore schema\n"));
829 1 : printf(_(" --no-security-labels do not restore security labels\n"));
830 1 : printf(_(" --no-statistics do not restore statistics\n"));
831 1 : printf(_(" --no-subscriptions do not restore subscriptions\n"));
832 1 : printf(_(" --no-table-access-method do not restore table access methods\n"));
833 1 : printf(_(" --no-tablespaces do not restore tablespace assignments\n"));
834 1 : printf(_(" --restrict-key=RESTRICT_KEY use provided string as psql \\restrict key\n"));
835 1 : printf(_(" --section=SECTION restore named section (pre-data, data, or post-data)\n"));
836 1 : printf(_(" --statistics restore the statistics\n"));
837 1 : printf(_(" --statistics-only restore only the statistics, not schema or data\n"));
838 1 : printf(_(" --strict-names require table and/or schema include patterns to\n"
839 : " match at least one entity each\n"));
840 1 : printf(_(" --transaction-size=N commit after every N objects\n"));
841 1 : printf(_(" --use-set-session-authorization\n"
842 : " use SET SESSION AUTHORIZATION commands instead of\n"
843 : " ALTER OWNER commands to set ownership\n"));
844 :
845 1 : printf(_("\nConnection options:\n"));
846 1 : printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
847 1 : printf(_(" -p, --port=PORT database server port number\n"));
848 1 : printf(_(" -U, --username=NAME connect as specified database user\n"));
849 1 : printf(_(" -w, --no-password never prompt for password\n"));
850 1 : printf(_(" -W, --password force password prompt (should happen automatically)\n"));
851 1 : printf(_(" --role=ROLENAME do SET ROLE before restore\n"));
852 :
853 1 : printf(_("\n"
854 : "The options -I, -n, -N, -P, -t, -T, --section, and --exclude-database can be\n"
855 : "combined and specified multiple times to select multiple objects.\n"));
856 1 : printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
857 1 : printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
858 1 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
859 1 : }
860 :
861 : /*
862 : * read_restore_filters - retrieve object identifier patterns from file
863 : *
864 : * Parse the specified filter file for include and exclude patterns, and add
865 : * them to the relevant lists. If the filename is "-" then filters will be
866 : * read from STDIN rather than a file.
867 : */
868 : static void
869 10 : read_restore_filters(const char *filename, RestoreOptions *opts)
870 : {
871 : FilterStateData fstate;
872 : char *objname;
873 : FilterCommandType comtype;
874 : FilterObjectType objtype;
875 :
876 10 : filter_init(&fstate, filename, exit_nicely);
877 :
878 27 : while (filter_read_item(&fstate, &objname, &comtype, &objtype))
879 : {
880 11 : if (comtype == FILTER_COMMAND_TYPE_INCLUDE)
881 : {
882 8 : switch (objtype)
883 : {
884 0 : case FILTER_OBJECT_TYPE_NONE:
885 0 : break;
886 2 : case FILTER_OBJECT_TYPE_TABLE_DATA:
887 : case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
888 : case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
889 : case FILTER_OBJECT_TYPE_DATABASE:
890 : case FILTER_OBJECT_TYPE_EXTENSION:
891 : case FILTER_OBJECT_TYPE_FOREIGN_DATA:
892 2 : pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
893 : "include",
894 : filter_object_type_name(objtype));
895 2 : exit_nicely(1);
896 :
897 2 : case FILTER_OBJECT_TYPE_FUNCTION:
898 2 : opts->selTypes = 1;
899 2 : opts->selFunction = 1;
900 2 : simple_string_list_append(&opts->functionNames, objname);
901 2 : break;
902 1 : case FILTER_OBJECT_TYPE_INDEX:
903 1 : opts->selTypes = 1;
904 1 : opts->selIndex = 1;
905 1 : simple_string_list_append(&opts->indexNames, objname);
906 1 : break;
907 1 : case FILTER_OBJECT_TYPE_SCHEMA:
908 1 : simple_string_list_append(&opts->schemaNames, objname);
909 1 : break;
910 1 : case FILTER_OBJECT_TYPE_TABLE:
911 1 : opts->selTypes = 1;
912 1 : opts->selTable = 1;
913 1 : simple_string_list_append(&opts->tableNames, objname);
914 1 : break;
915 1 : case FILTER_OBJECT_TYPE_TRIGGER:
916 1 : opts->selTypes = 1;
917 1 : opts->selTrigger = 1;
918 1 : simple_string_list_append(&opts->triggerNames, objname);
919 1 : break;
920 : }
921 : }
922 3 : else if (comtype == FILTER_COMMAND_TYPE_EXCLUDE)
923 : {
924 3 : switch (objtype)
925 : {
926 0 : case FILTER_OBJECT_TYPE_NONE:
927 0 : break;
928 2 : case FILTER_OBJECT_TYPE_TABLE_DATA:
929 : case FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN:
930 : case FILTER_OBJECT_TYPE_DATABASE:
931 : case FILTER_OBJECT_TYPE_EXTENSION:
932 : case FILTER_OBJECT_TYPE_FOREIGN_DATA:
933 : case FILTER_OBJECT_TYPE_FUNCTION:
934 : case FILTER_OBJECT_TYPE_INDEX:
935 : case FILTER_OBJECT_TYPE_TABLE:
936 : case FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN:
937 : case FILTER_OBJECT_TYPE_TRIGGER:
938 2 : pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
939 : "exclude",
940 : filter_object_type_name(objtype));
941 2 : exit_nicely(1);
942 :
943 1 : case FILTER_OBJECT_TYPE_SCHEMA:
944 1 : simple_string_list_append(&opts->schemaExcludeNames, objname);
945 1 : break;
946 : }
947 : }
948 : else
949 : {
950 : Assert(comtype == FILTER_COMMAND_TYPE_NONE);
951 : Assert(objtype == FILTER_OBJECT_TYPE_NONE);
952 : }
953 :
954 7 : if (objname)
955 7 : free(objname);
956 : }
957 :
958 6 : filter_free(&fstate);
959 6 : }
960 :
961 : /*
962 : * file_exists_in_directory
963 : *
964 : * Returns true if the file exists in the given directory.
965 : */
966 : static bool
967 221 : file_exists_in_directory(const char *dir, const char *filename)
968 : {
969 : struct stat st;
970 : char buf[MAXPGPATH];
971 :
972 221 : if (snprintf(buf, MAXPGPATH, "%s/%s", dir, filename) >= MAXPGPATH)
973 0 : pg_fatal("directory name too long: \"%s\"", dir);
974 :
975 221 : return (stat(buf, &st) == 0 && S_ISREG(st.st_mode));
976 : }
977 :
978 : /*
979 : * get_dbnames_list_to_restore
980 : *
981 : * This will mark for skipping any entries from dbname_oid_list that pattern match an
982 : * entry in the db_exclude_patterns list.
983 : *
984 : * Returns the number of database to be restored.
985 : *
986 : */
987 : static int
988 1 : get_dbnames_list_to_restore(PGconn *conn,
989 : SimplePtrList *dbname_oid_list,
990 : SimpleStringList db_exclude_patterns)
991 : {
992 1 : int count_db = 0;
993 : PQExpBuffer query;
994 : PQExpBuffer db_lit;
995 : PGresult *res;
996 :
997 1 : query = createPQExpBuffer();
998 1 : db_lit = createPQExpBuffer();
999 :
1000 : /*
1001 : * Process one by one all dbnames and if specified to skip restoring, then
1002 : * remove dbname from list.
1003 : */
1004 1 : for (SimplePtrListCell *db_cell = dbname_oid_list->head;
1005 7 : db_cell; db_cell = db_cell->next)
1006 : {
1007 6 : DbOidName *dbidname = (DbOidName *) db_cell->ptr;
1008 6 : bool skip_db_restore = false;
1009 :
1010 6 : resetPQExpBuffer(query);
1011 6 : resetPQExpBuffer(db_lit);
1012 :
1013 6 : appendStringLiteralConn(db_lit, dbidname->str, conn);
1014 :
1015 11 : for (SimpleStringListCell *pat_cell = db_exclude_patterns.head; pat_cell; pat_cell = pat_cell->next)
1016 : {
1017 : /*
1018 : * If there is an exact match then we don't need to try a pattern
1019 : * match
1020 : */
1021 6 : if (pg_strcasecmp(dbidname->str, pat_cell->val) == 0)
1022 1 : skip_db_restore = true;
1023 : /* Otherwise, try a pattern match if there is a connection */
1024 : else
1025 : {
1026 : int dotcnt;
1027 :
1028 5 : appendPQExpBufferStr(query, "SELECT 1 ");
1029 5 : processSQLNamePattern(conn, query, pat_cell->val, false,
1030 5 : false, NULL, db_lit->data,
1031 : NULL, NULL, NULL, &dotcnt);
1032 :
1033 5 : if (dotcnt > 0)
1034 : {
1035 0 : pg_log_error("improper qualified name (too many dotted names): %s",
1036 : dbidname->str);
1037 0 : PQfinish(conn);
1038 0 : exit_nicely(1);
1039 : }
1040 :
1041 5 : res = executeQuery(conn, query->data);
1042 :
1043 5 : if (PQntuples(res))
1044 : {
1045 0 : skip_db_restore = true;
1046 0 : pg_log_info("database name \"%s\" matches --exclude-database pattern \"%s\"", dbidname->str, pat_cell->val);
1047 : }
1048 :
1049 5 : PQclear(res);
1050 5 : resetPQExpBuffer(query);
1051 : }
1052 :
1053 6 : if (skip_db_restore)
1054 1 : break;
1055 : }
1056 :
1057 : /*
1058 : * Mark db to be skipped or increment the counter of dbs to be
1059 : * restored
1060 : */
1061 6 : if (skip_db_restore)
1062 : {
1063 1 : pg_log_info("excluding database \"%s\"", dbidname->str);
1064 1 : dbidname->oid = InvalidOid;
1065 : }
1066 : else
1067 5 : count_db++;
1068 : }
1069 :
1070 1 : destroyPQExpBuffer(query);
1071 1 : destroyPQExpBuffer(db_lit);
1072 :
1073 1 : return count_db;
1074 : }
1075 :
1076 : /*
1077 : * get_dbname_oid_list_from_mfile
1078 : *
1079 : * Open map.dat file and read line by line and then prepare a list of database
1080 : * names and corresponding db_oid.
1081 : *
1082 : * Returns, total number of database names in map.dat file.
1083 : */
1084 : static int
1085 9 : get_dbname_oid_list_from_mfile(char *dumpdirpath, SimplePtrList *dbname_oid_list)
1086 : {
1087 : StringInfoData linebuf;
1088 : FILE *pfile;
1089 : char map_file_path[MAXPGPATH];
1090 9 : int count = 0;
1091 : int len;
1092 :
1093 :
1094 : /*
1095 : * If there is no map.dat file in dump, then return from here as there is
1096 : * no database to restore.
1097 : */
1098 9 : if (!file_exists_in_directory(dumpdirpath, "map.dat"))
1099 : {
1100 0 : pg_log_info("database restoring is skipped because file \"%s\" does not exist in directory \"%s\"", "map.dat", dumpdirpath);
1101 0 : return 0;
1102 : }
1103 :
1104 9 : len = strlen(dumpdirpath);
1105 :
1106 : /* Trim slash from directory name. */
1107 9 : while (len > 1 && dumpdirpath[len - 1] == '/')
1108 : {
1109 0 : dumpdirpath[len - 1] = '\0';
1110 0 : len--;
1111 : }
1112 :
1113 9 : snprintf(map_file_path, MAXPGPATH, "%s/map.dat", dumpdirpath);
1114 :
1115 : /* Open map.dat file. */
1116 9 : pfile = fopen(map_file_path, PG_BINARY_R);
1117 :
1118 9 : if (pfile == NULL)
1119 0 : pg_fatal("could not open file \"%s\": %m", map_file_path);
1120 :
1121 9 : initStringInfo(&linebuf);
1122 :
1123 : /* Append all the dbname/db_oid combinations to the list. */
1124 161 : while (pg_get_line_buf(pfile, &linebuf))
1125 : {
1126 152 : Oid db_oid = InvalidOid;
1127 : char *dbname;
1128 : DbOidName *dbidname;
1129 : int namelen;
1130 152 : char *p = linebuf.data;
1131 :
1132 : /* look for the dboid. */
1133 430 : while (isdigit((unsigned char) *p))
1134 278 : p++;
1135 :
1136 : /* ignore lines that don't begin with a digit */
1137 152 : if (p == linebuf.data)
1138 82 : continue;
1139 :
1140 70 : if (*p == ' ')
1141 : {
1142 70 : sscanf(linebuf.data, "%u", &db_oid);
1143 70 : p++;
1144 : }
1145 :
1146 : /* dbname is the rest of the line */
1147 70 : dbname = p;
1148 70 : namelen = strlen(dbname);
1149 :
1150 : /* Strip trailing newline */
1151 70 : if (namelen > 0 && dbname[namelen - 1] == '\n')
1152 70 : dbname[--namelen] = '\0';
1153 :
1154 : /* Report error and exit if the file has any corrupted data. */
1155 70 : if (!OidIsValid(db_oid) || namelen < 1)
1156 0 : pg_fatal("invalid entry in file \"%s\" on line %d", map_file_path,
1157 : count + 1);
1158 :
1159 70 : dbidname = pg_malloc(offsetof(DbOidName, str) + namelen + 1);
1160 70 : dbidname->oid = db_oid;
1161 70 : strlcpy(dbidname->str, dbname, namelen + 1);
1162 :
1163 70 : pg_log_info("found database \"%s\" (OID: %u) in file \"%s\"",
1164 : dbidname->str, db_oid, map_file_path);
1165 :
1166 70 : simple_ptr_list_append(dbname_oid_list, dbidname);
1167 70 : count++;
1168 : }
1169 :
1170 : /* Close map.dat file. */
1171 9 : fclose(pfile);
1172 :
1173 9 : pfree(linebuf.data);
1174 :
1175 9 : return count;
1176 : }
1177 :
1178 : /*
1179 : * restore_all_databases
1180 : *
1181 : * This will restore databases those dumps are present in
1182 : * directory based on map.dat file mapping.
1183 : *
1184 : * This will skip restoring for databases that are specified with
1185 : * exclude-database option.
1186 : *
1187 : * returns, number of errors while doing restore.
1188 : */
1189 : static int
1190 9 : restore_all_databases(const char *inputFileSpec,
1191 : SimpleStringList db_exclude_patterns, RestoreOptions *opts,
1192 : int numWorkers)
1193 : {
1194 9 : SimplePtrList dbname_oid_list = {NULL, NULL};
1195 9 : int num_db_restore = 0;
1196 : int num_total_db;
1197 9 : int n_errors_total = 0;
1198 9 : char *connected_db = NULL;
1199 9 : PGconn *conn = NULL;
1200 9 : RestoreOptions *original_opts = pg_malloc0_object(RestoreOptions);
1201 9 : RestoreOptions *tmpopts = pg_malloc0_object(RestoreOptions);
1202 :
1203 9 : memcpy(original_opts, opts, sizeof(RestoreOptions));
1204 :
1205 : /* Save db name to reuse it for all the database. */
1206 9 : if (opts->cparams.dbname)
1207 0 : connected_db = opts->cparams.dbname;
1208 :
1209 9 : num_total_db = get_dbname_oid_list_from_mfile((char *) inputFileSpec, &dbname_oid_list);
1210 :
1211 9 : pg_log_info(ngettext("found %d database name in \"%s\"",
1212 : "found %d database names in \"%s\"",
1213 : num_total_db),
1214 : num_total_db, "map.dat");
1215 :
1216 : /*
1217 : * If exclude-patterns is given, connect to the database to process them.
1218 : */
1219 9 : if (db_exclude_patterns.head != NULL)
1220 : {
1221 1 : if (opts->cparams.dbname)
1222 : {
1223 0 : conn = ConnectDatabase(opts->cparams.dbname, NULL, opts->cparams.pghost,
1224 0 : opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
1225 : false, progname, NULL, NULL, NULL, NULL);
1226 :
1227 0 : if (!conn)
1228 0 : pg_fatal("could not connect to database \"%s\"", opts->cparams.dbname);
1229 : }
1230 :
1231 1 : if (!conn)
1232 : {
1233 1 : pg_log_info("trying to connect to database \"%s\"", "postgres");
1234 :
1235 1 : conn = ConnectDatabase("postgres", NULL, opts->cparams.pghost,
1236 1 : opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
1237 : false, progname, NULL, NULL, NULL, NULL);
1238 :
1239 : /* Try with template1. */
1240 1 : if (!conn)
1241 : {
1242 0 : pg_log_info("trying to connect to database \"%s\"", "template1");
1243 :
1244 0 : conn = ConnectDatabase("template1", NULL, opts->cparams.pghost,
1245 0 : opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
1246 : false, progname, NULL, NULL, NULL, NULL);
1247 0 : if (!conn)
1248 : {
1249 0 : pg_log_error("could not connect to databases \"postgres\" or \"template1\"\n"
1250 : "Please specify an alternative database.");
1251 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
1252 0 : exit_nicely(1);
1253 : }
1254 : }
1255 : }
1256 :
1257 : /* Filter the db list according to the exclude patterns. */
1258 1 : num_db_restore = get_dbnames_list_to_restore(conn, &dbname_oid_list,
1259 : db_exclude_patterns);
1260 1 : PQfinish(conn);
1261 : }
1262 : else
1263 8 : num_db_restore = num_total_db;
1264 :
1265 : /* Exit if no db needs to be restored. */
1266 9 : if (num_db_restore == 0)
1267 : {
1268 0 : pg_log_info(ngettext("no database needs restoring out of %d database",
1269 : "no database needs restoring out of %d databases", num_total_db),
1270 : num_total_db);
1271 0 : pg_free(original_opts);
1272 0 : pg_free(tmpopts);
1273 0 : return 0;
1274 : }
1275 :
1276 9 : pg_log_info("need to restore %d databases out of %d databases", num_db_restore, num_total_db);
1277 :
1278 : /*
1279 : * We have a list of databases to restore after processing the
1280 : * exclude-database switch(es). Now we can restore them one by one.
1281 : */
1282 9 : for (SimplePtrListCell *db_cell = dbname_oid_list.head;
1283 79 : db_cell; db_cell = db_cell->next)
1284 : {
1285 70 : DbOidName *dbidname = (DbOidName *) db_cell->ptr;
1286 : char subdirpath[MAXPGPATH];
1287 : char subdirdbpath[MAXPGPATH];
1288 : char dbfilename[MAXPGPATH];
1289 : int n_errors;
1290 :
1291 : /* ignore dbs marked for skipping */
1292 70 : if (dbidname->oid == InvalidOid)
1293 1 : continue;
1294 :
1295 : /*
1296 : * Since pg_backup_archiver.c may modify RestoreOptions during the
1297 : * previous restore, we must provide a fresh copy of the original
1298 : * "opts" for each call to restore_one_database.
1299 : */
1300 69 : memcpy(tmpopts, original_opts, sizeof(RestoreOptions));
1301 :
1302 : /*
1303 : * We need to reset override_dbname so that objects can be restored
1304 : * into an already created database. (used with -d/--dbname option)
1305 : */
1306 69 : if (tmpopts->cparams.override_dbname)
1307 : {
1308 0 : pfree(tmpopts->cparams.override_dbname);
1309 0 : tmpopts->cparams.override_dbname = NULL;
1310 : }
1311 :
1312 69 : snprintf(subdirdbpath, MAXPGPATH, "%s/databases", inputFileSpec);
1313 :
1314 : /*
1315 : * Look for the database dump file/dir. If there is an {oid}.tar or
1316 : * {oid}.dmp file, use it. Otherwise try to use a directory called
1317 : * {oid}
1318 : */
1319 69 : snprintf(dbfilename, MAXPGPATH, "%u.tar", dbidname->oid);
1320 69 : if (file_exists_in_directory(subdirdbpath, dbfilename))
1321 8 : snprintf(subdirpath, MAXPGPATH, "%s/databases/%u.tar", inputFileSpec, dbidname->oid);
1322 : else
1323 : {
1324 61 : snprintf(dbfilename, MAXPGPATH, "%u.dmp", dbidname->oid);
1325 :
1326 61 : if (file_exists_in_directory(subdirdbpath, dbfilename))
1327 16 : snprintf(subdirpath, MAXPGPATH, "%s/databases/%u.dmp", inputFileSpec, dbidname->oid);
1328 : else
1329 45 : snprintf(subdirpath, MAXPGPATH, "%s/databases/%u", inputFileSpec, dbidname->oid);
1330 : }
1331 :
1332 69 : pg_log_info("restoring database \"%s\"", dbidname->str);
1333 :
1334 : /* If database is already created, then don't set createDB flag. */
1335 69 : if (tmpopts->cparams.dbname)
1336 : {
1337 : PGconn *test_conn;
1338 :
1339 0 : test_conn = ConnectDatabase(dbidname->str, NULL, tmpopts->cparams.pghost,
1340 0 : tmpopts->cparams.pgport, tmpopts->cparams.username, TRI_DEFAULT,
1341 : false, progname, NULL, NULL, NULL, NULL);
1342 0 : if (test_conn)
1343 : {
1344 0 : PQfinish(test_conn);
1345 :
1346 : /* Use already created database for connection. */
1347 0 : tmpopts->createDB = 0;
1348 0 : tmpopts->cparams.dbname = dbidname->str;
1349 : }
1350 : else
1351 : {
1352 : /* We'll have to create it */
1353 0 : tmpopts->createDB = 1;
1354 0 : tmpopts->cparams.dbname = connected_db;
1355 : }
1356 : }
1357 :
1358 : /* Restore the single database. */
1359 69 : n_errors = restore_one_database(subdirpath, tmpopts, numWorkers, true);
1360 :
1361 69 : n_errors_total += n_errors;
1362 :
1363 : /* Print a summary of ignored errors during single database restore. */
1364 69 : if (n_errors)
1365 0 : pg_log_warning("errors ignored on database \"%s\" restore: %d", dbidname->str, n_errors);
1366 : }
1367 :
1368 : /* Log number of processed databases. */
1369 9 : pg_log_info("number of restored databases is %d", num_db_restore);
1370 :
1371 : /* Free dbname and dboid list. */
1372 9 : simple_ptr_list_destroy(&dbname_oid_list);
1373 :
1374 9 : pg_free(original_opts);
1375 9 : pg_free(tmpopts);
1376 :
1377 9 : return n_errors_total;
1378 : }
|