Age Owner TLA 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 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 : #ifdef HAVE_TERMIOS_H
45 : #include <termios.h>
46 : #endif
47 :
48 : #include "dumputils.h"
49 : #include "fe_utils/option_utils.h"
50 : #include "getopt_long.h"
51 : #include "parallel.h"
52 : #include "pg_backup_utils.h"
53 :
54 : static void usage(const char *progname);
55 :
56 : int
8053 bruce 57 CBC 46 : main(int argc, char **argv)
58 : {
59 : RestoreOptions *opts;
60 : int c;
61 : int exit_code;
3668 andrew 62 46 : int numWorkers = 1;
63 : Archive *AH;
64 : char *inputFileSpec;
65 : static int disable_triggers = 0;
66 : static int enable_row_security = 0;
67 : static int if_exists = 0;
68 : static int no_data_for_failed_tables = 0;
69 : static int outputNoTableAm = 0;
70 : static int outputNoTablespaces = 0;
71 : static int use_setsessauth = 0;
72 : static int no_comments = 0;
73 : static int no_publications = 0;
74 : static int no_security_labels = 0;
75 : static int no_subscriptions = 0;
76 : static int strict_names = 0;
77 :
7900 peter_e 78 46 : struct option cmdopts[] = {
79 : {"clean", 0, NULL, 'c'},
80 : {"create", 0, NULL, 'C'},
81 : {"data-only", 0, NULL, 'a'},
82 : {"dbname", 1, NULL, 'd'},
83 : {"exit-on-error", 0, NULL, 'e'},
84 : {"exclude-schema", 1, NULL, 'N'},
85 : {"file", 1, NULL, 'f'},
86 : {"format", 1, NULL, 'F'},
87 : {"function", 1, NULL, 'P'},
88 : {"host", 1, NULL, 'h'},
89 : {"index", 1, NULL, 'I'},
90 : {"jobs", 1, NULL, 'j'},
91 : {"list", 0, NULL, 'l'},
92 : {"no-privileges", 0, NULL, 'x'},
93 : {"no-acl", 0, NULL, 'x'},
94 : {"no-owner", 0, NULL, 'O'},
95 : {"no-reconnect", 0, NULL, 'R'},
96 : {"port", 1, NULL, 'p'},
97 : {"no-password", 0, NULL, 'w'},
98 : {"password", 0, NULL, 'W'},
99 : {"schema", 1, NULL, 'n'},
100 : {"schema-only", 0, NULL, 's'},
101 : {"superuser", 1, NULL, 'S'},
102 : {"table", 1, NULL, 't'},
103 : {"trigger", 1, NULL, 'T'},
104 : {"use-list", 1, NULL, 'L'},
105 : {"username", 1, NULL, 'U'},
106 : {"verbose", 0, NULL, 'v'},
107 : {"single-transaction", 0, NULL, '1'},
108 :
109 : /*
110 : * the following options don't have an equivalent short option letter
111 : */
112 : {"disable-triggers", no_argument, &disable_triggers, 1},
113 : {"enable-row-security", no_argument, &enable_row_security, 1},
114 : {"if-exists", no_argument, &if_exists, 1},
115 : {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
116 : {"no-table-access-method", no_argument, &outputNoTableAm, 1},
117 : {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
118 : {"role", required_argument, NULL, 2},
119 : {"section", required_argument, NULL, 3},
120 : {"strict-names", no_argument, &strict_names, 1},
121 : {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
122 : {"no-comments", no_argument, &no_comments, 1},
123 : {"no-publications", no_argument, &no_publications, 1},
124 : {"no-security-labels", no_argument, &no_security_labels, 1},
125 : {"no-subscriptions", no_argument, &no_subscriptions, 1},
126 :
127 : {NULL, 0, NULL, 0}
128 : };
129 :
1469 peter 130 46 : pg_logging_init(argv[0]);
1460 131 46 : pg_logging_set_level(PG_LOG_WARNING);
5232 peter_e 132 46 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
133 :
5142 andrew 134 46 : init_parallel_dump_utils();
135 :
8297 pjw 136 46 : opts = NewRestoreOptions();
137 :
7310 bruce 138 46 : progname = get_progname(argv[0]);
139 :
8128 peter_e 140 46 : if (argc > 1)
141 : {
8053 bruce 142 45 : if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
143 : {
8128 peter_e 144 1 : usage(progname);
4070 rhaas 145 1 : exit_nicely(0);
146 : }
8053 bruce 147 44 : if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
148 : {
8128 peter_e 149 3 : puts("pg_restore (PostgreSQL) " PG_VERSION);
4070 rhaas 150 3 : exit_nicely(0);
151 : }
152 : }
153 :
2392 peter_e 154 148 : while ((c = getopt_long(argc, argv, "acCd:ef:F:h:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1",
7398 155 148 : cmdopts, NULL)) != -1)
156 : {
8297 pjw 157 108 : switch (c)
158 : {
159 2 : case 'a': /* Dump data only */
160 2 : opts->dataOnly = 1;
161 2 : break;
6385 bruce 162 3 : case 'c': /* clean (i.e., drop) schema prior to create */
8297 pjw 163 3 : opts->dropSchema = 1;
164 3 : break;
8286 165 8 : case 'C':
4712 tgl 166 8 : opts->createDB = 1;
8286 pjw 167 8 : break;
8297 168 11 : case 'd':
927 tgl 169 11 : opts->cparams.dbname = pg_strdup(optarg);
8297 pjw 170 11 : break;
6806 bruce 171 6 : case 'e':
172 6 : opts->exit_on_error = true;
173 6 : break;
8297 pjw 174 23 : case 'f': /* output file name */
4153 bruce 175 23 : opts->filename = pg_strdup(optarg);
8297 pjw 176 23 : break;
177 5 : case 'F':
8053 bruce 178 5 : if (strlen(optarg) != 0)
4153 179 5 : opts->formatName = pg_strdup(optarg);
8297 pjw 180 5 : break;
181 6 : case 'h':
182 6 : if (strlen(optarg) != 0)
927 tgl 183 6 : opts->cparams.pghost = pg_strdup(optarg);
8297 pjw 184 6 : break;
185 :
5133 peter_e 186 9 : case 'j': /* number of restore jobs */
624 michael 187 9 : if (!option_parse_int(optarg, "-j/--jobs", 1,
188 : PG_MAX_JOBS,
189 : &numWorkers))
190 1 : exit(1);
5133 peter_e 191 8 : break;
192 :
8069 pjw 193 5 : case 'l': /* Dump the TOC summary */
194 5 : opts->tocSummary = 1;
195 5 : break;
196 :
8069 pjw 197 UBC 0 : case 'L': /* input TOC summary file name */
4153 bruce 198 0 : opts->tocFile = pg_strdup(optarg);
8069 pjw 199 0 : break;
200 :
6265 bruce 201 0 : case 'n': /* Dump data for this schema only */
3511 heikki.linnakangas 202 0 : simple_string_list_append(&opts->schemaNames, optarg);
6265 bruce 203 0 : break;
204 :
2392 peter_e 205 0 : case 'N': /* Do not dump data for this schema */
206 0 : simple_string_list_append(&opts->schemaExcludeNames, optarg);
207 0 : break;
208 :
8297 pjw 209 0 : case 'O':
8286 210 0 : opts->noOwner = 1;
8297 211 0 : break;
212 :
8297 pjw 213 CBC 6 : case 'p':
214 6 : if (strlen(optarg) != 0)
927 tgl 215 6 : opts->cparams.pgport = pg_strdup(optarg);
8297 pjw 216 6 : break;
8286 pjw 217 UBC 0 : case 'R':
218 : /* no-op, still accepted for backwards compatibility */
219 0 : break;
8053 bruce 220 0 : case 'P': /* Function */
8297 pjw 221 0 : opts->selTypes = 1;
222 0 : opts->selFunction = 1;
3511 heikki.linnakangas 223 0 : simple_string_list_append(&opts->functionNames, optarg);
8297 pjw 224 0 : break;
8053 bruce 225 0 : case 'I': /* Index */
8297 pjw 226 0 : opts->selTypes = 1;
227 0 : opts->selIndex = 1;
3511 heikki.linnakangas 228 0 : simple_string_list_append(&opts->indexNames, optarg);
8297 pjw 229 0 : break;
8053 bruce 230 0 : case 'T': /* Trigger */
8297 pjw 231 0 : opts->selTypes = 1;
232 0 : opts->selTrigger = 1;
3511 heikki.linnakangas 233 0 : simple_string_list_append(&opts->triggerNames, optarg);
8297 pjw 234 0 : break;
8297 pjw 235 CBC 1 : case 's': /* dump schema only */
236 1 : opts->schemaOnly = 1;
237 1 : break;
8286 pjw 238 UBC 0 : case 'S': /* Superuser username */
239 0 : if (strlen(optarg) != 0)
4153 bruce 240 0 : opts->superuser = pg_strdup(optarg);
8286 pjw 241 0 : break;
2838 tgl 242 0 : case 't': /* Dump specified table(s) only */
8297 pjw 243 0 : opts->selTypes = 1;
244 0 : opts->selTable = 1;
3734 magnus 245 0 : simple_string_list_append(&opts->tableNames, optarg);
8297 pjw 246 0 : break;
247 :
7997 peter_e 248 CBC 8 : case 'U':
927 tgl 249 8 : opts->cparams.username = pg_strdup(optarg);
8297 pjw 250 8 : break;
251 :
252 11 : case 'v': /* verbose */
253 11 : opts->verbose = 1;
934 tgl 254 11 : pg_logging_increase_verbosity();
8297 pjw 255 11 : break;
256 :
5155 peter_e 257 UBC 0 : case 'w':
927 tgl 258 0 : opts->cparams.promptPassword = TRI_NO;
5155 peter_e 259 0 : break;
260 :
7997 261 0 : case 'W':
927 tgl 262 0 : opts->cparams.promptPassword = TRI_YES;
7997 peter_e 263 0 : break;
264 :
8297 pjw 265 0 : case 'x': /* skip ACL dump */
266 0 : opts->aclsSkip = 1;
267 0 : break;
268 :
5207 tgl 269 CBC 2 : case '1': /* Restore data in a single transaction */
270 2 : opts->single_txn = true;
271 2 : opts->exit_on_error = true;
272 2 : break;
273 :
7900 peter_e 274 1 : case 0:
275 :
276 : /*
277 : * This covers the long options without a short equivalent.
278 : */
279 1 : break;
280 :
5207 tgl 281 UBC 0 : case 2: /* SET ROLE */
3831 bruce 282 0 : opts->use_role = pg_strdup(optarg);
6265 283 0 : break;
284 :
4132 andrew 285 0 : case 3: /* section */
3967 tgl 286 0 : set_dump_section(optarg, &(opts->dumpSections));
4132 andrew 287 0 : break;
288 :
8297 pjw 289 CBC 1 : default:
290 : /* getopt_long already emitted a complaint */
366 tgl 291 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
4070 rhaas 292 1 : exit_nicely(1);
293 : }
294 : }
295 :
296 : /* Get file name from command line */
8053 bruce 297 40 : if (optind < argc)
4622 tgl 298 32 : inputFileSpec = argv[optind++];
299 : else
7111 300 8 : inputFileSpec = NULL;
301 :
302 : /* Complain if any arguments remain */
4622 303 40 : if (optind < argc)
304 : {
1469 peter 305 1 : pg_log_error("too many command-line arguments (first is \"%s\")",
306 : argv[optind]);
366 tgl 307 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
4070 rhaas 308 1 : exit_nicely(1);
309 : }
310 :
311 : /* Complain if neither -f nor -d was specified (except if dumping TOC) */
927 tgl 312 39 : if (!opts->cparams.dbname && !opts->filename && !opts->tocSummary)
366 313 1 : pg_fatal("one of -d/--dbname and -f/--file must be specified");
314 :
315 : /* Should get at most one of -d and -f, else user is confused */
927 316 38 : if (opts->cparams.dbname)
317 : {
7111 318 11 : if (opts->filename)
319 : {
1469 peter 320 1 : pg_log_error("options -d/--dbname and -f/--file cannot be used together");
366 tgl 321 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
4070 rhaas 322 1 : exit_nicely(1);
323 : }
7111 tgl 324 10 : opts->useDB = 1;
325 : }
326 :
3568 peter_e 327 37 : if (opts->dataOnly && opts->schemaOnly)
366 tgl 328 1 : pg_fatal("options -s/--schema-only and -a/--data-only cannot be used together");
329 :
3568 peter_e 330 36 : if (opts->dataOnly && opts->dropSchema)
366 tgl 331 1 : pg_fatal("options -c/--clean and -a/--data-only cannot be used together");
332 :
333 : /*
334 : * -C is not compatible with -1, because we can't create a database inside
335 : * a transaction block.
336 : */
1622 michael 337 35 : if (opts->createDB && opts->single_txn)
366 tgl 338 1 : pg_fatal("options -C/--create and -1/--single-transaction cannot be used together");
339 :
340 : /* Can't do single-txn mode with multiple connections */
3668 andrew 341 34 : if (opts->single_txn && numWorkers > 1)
366 tgl 342 1 : pg_fatal("cannot specify both --single-transaction and multiple jobs");
343 :
7639 344 33 : opts->disable_triggers = disable_triggers;
3124 sfrost 345 33 : opts->enable_row_security = enable_row_security;
6028 peter_e 346 33 : opts->noDataForFailedTables = no_data_for_failed_tables;
447 michael 347 33 : opts->noTableAm = outputNoTableAm;
5498 tgl 348 33 : opts->noTablespace = outputNoTablespaces;
349 33 : opts->use_setsessauth = use_setsessauth;
1900 350 33 : opts->no_comments = no_comments;
2158 peter_e 351 33 : opts->no_publications = no_publications;
4343 352 33 : opts->no_security_labels = no_security_labels;
2161 353 33 : opts->no_subscriptions = no_subscriptions;
354 :
3324 alvherre 355 33 : if (if_exists && !opts->dropSchema)
366 tgl 356 1 : pg_fatal("option --if-exists requires option -c/--clean");
3324 alvherre 357 32 : opts->if_exists = if_exists;
2764 teodor 358 32 : opts->strict_names = strict_names;
359 :
8053 bruce 360 32 : if (opts->formatName)
361 : {
362 5 : switch (opts->formatName[0])
363 : {
8069 pjw 364 2 : case 'c':
365 : case 'C':
366 2 : opts->format = archCustom;
367 2 : break;
368 :
4459 heikki.linnakangas 369 1 : case 'd':
370 : case 'D':
371 1 : opts->format = archDirectory;
372 1 : break;
373 :
8069 pjw 374 1 : case 't':
375 : case 'T':
376 1 : opts->format = archTar;
377 1 : break;
378 :
379 1 : default:
366 tgl 380 1 : pg_fatal("unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
381 : opts->formatName);
382 : }
383 : }
384 :
7111 385 31 : AH = OpenArchive(inputFileSpec, opts->format);
386 :
2643 387 31 : SetArchiveOptions(AH, NULL, opts);
388 :
389 : /*
390 : * We don't have a connection yet but that doesn't matter. The connection
391 : * is initialized to NULL and if we terminate through exit_nicely() while
392 : * it's still NULL, the cleanup function will just be a no-op.
393 : */
4037 alvherre 394 31 : on_exit_close_archive(AH);
395 :
396 : /* Let the archiver know how noisy to be */
8297 pjw 397 31 : AH->verbose = opts->verbose;
398 :
399 : /*
400 : * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
401 : */
6806 bruce 402 31 : AH->exit_on_error = opts->exit_on_error;
403 :
8053 404 31 : if (opts->tocFile)
2643 tgl 405 UBC 0 : SortTocFromFile(AH);
406 :
3668 andrew 407 CBC 31 : AH->numWorkers = numWorkers;
408 :
8053 bruce 409 31 : if (opts->tocSummary)
2643 tgl 410 5 : PrintTOCSummary(AH);
411 : else
412 : {
413 26 : ProcessArchiveRestoreOptions(AH);
3967 414 26 : RestoreArchive(AH);
415 : }
416 :
417 : /* done, print a summary of ignored errors */
6926 bruce 418 31 : if (AH->n_errors)
1469 peter 419 UBC 0 : pg_log_warning("errors ignored on restore: %d", AH->n_errors);
420 :
421 : /* AH may be freed in CloseArchive? */
6797 bruce 422 CBC 31 : exit_code = AH->n_errors ? 1 : 0;
423 :
2643 tgl 424 31 : CloseArchive(AH);
425 :
6926 bruce 426 31 : return exit_code;
427 : }
428 :
429 : static void
8053 430 1 : usage(const char *progname)
431 : {
7529 peter_e 432 1 : printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname);
433 1 : printf(_("Usage:\n"));
7478 434 1 : printf(_(" %s [OPTION]... [FILE]\n"), progname);
435 :
436 1 : printf(_("\nGeneral options:\n"));
6723 bruce 437 1 : printf(_(" -d, --dbname=NAME connect to database name\n"));
1466 alvherre 438 1 : printf(_(" -f, --file=FILENAME output file name (- for stdout)\n"));
4459 heikki.linnakangas 439 1 : printf(_(" -F, --format=c|d|t backup file format (should be automatic)\n"));
7522 bruce 440 1 : printf(_(" -l, --list print summarized TOC of the archive\n"));
7478 peter_e 441 1 : printf(_(" -v, --verbose verbose mode\n"));
3947 442 1 : printf(_(" -V, --version output version information, then exit\n"));
443 1 : printf(_(" -?, --help show this help, then exit\n"));
444 :
6752 bruce 445 1 : printf(_("\nOptions controlling the restore:\n"));
3978 peter_e 446 1 : printf(_(" -a, --data-only restore only the data, no schema\n"));
447 1 : printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
448 1 : printf(_(" -C, --create create the target database\n"));
449 1 : printf(_(" -e, --exit-on-error exit on error, default is to continue\n"));
3151 450 1 : printf(_(" -I, --index=NAME restore named index\n"));
3978 451 1 : printf(_(" -j, --jobs=NUM use this many parallel jobs to restore\n"));
452 1 : printf(_(" -L, --use-list=FILENAME use table of contents from this file for\n"
453 : " selecting/ordering output\n"));
3151 454 1 : printf(_(" -n, --schema=NAME restore only objects in this schema\n"));
2392 455 1 : printf(_(" -N, --exclude-schema=NAME do not restore objects in this schema\n"));
3978 456 1 : printf(_(" -O, --no-owner skip restoration of object ownership\n"));
3151 457 1 : printf(_(" -P, --function=NAME(args) restore named function\n"));
3978 458 1 : printf(_(" -s, --schema-only restore only the schema, no data\n"));
459 1 : printf(_(" -S, --superuser=NAME superuser user name to use for disabling triggers\n"));
2540 460 1 : printf(_(" -t, --table=NAME restore named relation (table, view, etc.)\n"));
3151 461 1 : printf(_(" -T, --trigger=NAME restore named trigger\n"));
3978 462 1 : printf(_(" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
463 1 : printf(_(" -1, --single-transaction restore as a single transaction\n"));
464 1 : printf(_(" --disable-triggers disable triggers during data-only restore\n"));
2762 465 1 : printf(_(" --enable-row-security enable row security\n"));
3324 alvherre 466 1 : printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
1748 michael 467 1 : printf(_(" --no-comments do not restore comments\n"));
3978 peter_e 468 1 : printf(_(" --no-data-for-failed-tables do not restore data of tables that could not be\n"
469 : " created\n"));
2158 470 1 : printf(_(" --no-publications do not restore publications\n"));
3978 471 1 : printf(_(" --no-security-labels do not restore security labels\n"));
2161 472 1 : printf(_(" --no-subscriptions do not restore subscriptions\n"));
447 michael 473 1 : printf(_(" --no-table-access-method do not restore table access methods\n"));
3978 peter_e 474 1 : printf(_(" --no-tablespaces do not restore tablespace assignments\n"));
3151 475 1 : printf(_(" --section=SECTION restore named section (pre-data, data, or post-data)\n"));
2764 teodor 476 1 : printf(_(" --strict-names require table and/or schema include patterns to\n"
477 : " match at least one entity each\n"));
5498 tgl 478 1 : printf(_(" --use-set-session-authorization\n"
479 : " use SET SESSION AUTHORIZATION commands instead of\n"
480 : " ALTER OWNER commands to set ownership\n"));
481 :
7478 peter_e 482 1 : printf(_("\nConnection options:\n"));
7242 bruce 483 1 : printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
7478 peter_e 484 1 : printf(_(" -p, --port=PORT database server port number\n"));
485 1 : printf(_(" -U, --username=NAME connect as specified database user\n"));
5155 486 1 : printf(_(" -w, --no-password never prompt for password\n"));
7478 487 1 : printf(_(" -W, --password force password prompt (should happen automatically)\n"));
4337 488 1 : printf(_(" --role=ROLENAME do SET ROLE before restore\n"));
489 :
3151 490 1 : printf(_("\n"
491 : "The options -I, -n, -N, -P, -t, -T, and --section can be combined and specified\n"
492 : "multiple times to select multiple objects.\n"));
7529 493 1 : printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
1136 peter 494 1 : printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
495 1 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
8297 pjw 496 1 : }
|