1 /*-------------------------------------------------------------------------
4 * pg_restore is an utility extracting postgres database definitions
5 * from a backup archive created by pg_dump using the archiver
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
12 * user-defined functions
19 * the output script is SQL that is understood by PostgreSQL
21 * Basic process in a restore operation is:
23 * Open the Archive and read the TOC.
24 * Set flags in TOC entries, and *maybe* reorder them.
25 * Generate script to stdout
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.
32 * The author is not responsible for loss or damages that may
33 * result from its use.
39 *-------------------------------------------------------------------------
42 #include "pg_backup_archiver.h"
52 #include "getopt_long.h"
57 #ifndef HAVE_INT_OPTRESET
66 static void usage(const char *progname
);
68 typedef struct option optType
;
71 main(int argc
, char **argv
)
78 static int disable_triggers
= 0;
79 static int no_data_for_failed_tables
= 0;
80 static int outputNoTablespaces
= 0;
81 static int use_setsessauth
= 0;
83 struct option cmdopts
[] = {
84 {"clean", 0, NULL
, 'c'},
85 {"create", 0, NULL
, 'C'},
86 {"data-only", 0, NULL
, 'a'},
87 {"dbname", 1, NULL
, 'd'},
88 {"exit-on-error", 0, NULL
, 'e'},
89 {"file", 1, NULL
, 'f'},
90 {"format", 1, NULL
, 'F'},
91 {"function", 1, NULL
, 'P'},
92 {"host", 1, NULL
, 'h'},
93 {"ignore-version", 0, NULL
, 'i'},
94 {"index", 1, NULL
, 'I'},
95 {"list", 0, NULL
, 'l'},
96 {"multi-thread", 1, NULL
, 'm'},
97 {"no-privileges", 0, NULL
, 'x'},
98 {"no-acl", 0, NULL
, 'x'},
99 {"no-owner", 0, NULL
, 'O'},
100 {"no-reconnect", 0, NULL
, 'R'},
101 {"port", 1, NULL
, 'p'},
102 {"password", 0, NULL
, 'W'},
103 {"schema", 1, NULL
, 'n'},
104 {"schema-only", 0, NULL
, 's'},
105 {"superuser", 1, NULL
, 'S'},
106 {"table", 1, NULL
, 't'},
107 {"trigger", 1, NULL
, 'T'},
108 {"use-list", 1, NULL
, 'L'},
109 {"username", 1, NULL
, 'U'},
110 {"verbose", 0, NULL
, 'v'},
111 {"single-transaction", 0, NULL
, '1'},
114 * the following options don't have an equivalent short option letter
116 {"disable-triggers", no_argument
, &disable_triggers
, 1},
117 {"no-data-for-failed-tables", no_argument
, &no_data_for_failed_tables
, 1},
118 {"no-tablespaces", no_argument
, &outputNoTablespaces
, 1},
119 {"role", required_argument
, NULL
, 2},
120 {"use-set-session-authorization", no_argument
, &use_setsessauth
, 1},
125 set_pglocale_pgservice(argv
[0], PG_TEXTDOMAIN("pg_dump"));
127 opts
= NewRestoreOptions();
129 progname
= get_progname(argv
[0]);
133 if (strcmp(argv
[1], "--help") == 0 || strcmp(argv
[1], "-?") == 0)
138 if (strcmp(argv
[1], "--version") == 0 || strcmp(argv
[1], "-V") == 0)
140 puts("pg_restore (PostgreSQL) " PG_VERSION
);
145 while ((c
= getopt_long(argc
, argv
, "acCd:ef:F:h:iI:lL:m:n:Op:P:RsS:t:T:U:vWxX:1",
146 cmdopts
, NULL
)) != -1)
150 case 'a': /* Dump data only */
153 case 'c': /* clean (i.e., drop) schema prior to create */
154 opts
->dropSchema
= 1;
160 opts
->dbname
= strdup(optarg
);
163 opts
->exit_on_error
= true;
165 case 'f': /* output file name */
166 opts
->filename
= strdup(optarg
);
169 if (strlen(optarg
) != 0)
170 opts
->formatName
= strdup(optarg
);
173 if (strlen(optarg
) != 0)
174 opts
->pghost
= strdup(optarg
);
177 /* ignored, deprecated option */
180 case 'l': /* Dump the TOC summary */
181 opts
->tocSummary
= 1;
184 case 'L': /* input TOC summary file name */
185 opts
->tocFile
= strdup(optarg
);
188 case 'm': /* number of restore threads */
189 opts
->number_of_threads
= atoi(optarg
);
192 case 'n': /* Dump data for this schema only */
193 opts
->schemaNames
= strdup(optarg
);
201 if (strlen(optarg
) != 0)
202 opts
->pgport
= strdup(optarg
);
205 /* no-op, still accepted for backwards compatibility */
207 case 'P': /* Function */
209 opts
->selFunction
= 1;
210 opts
->functionNames
= strdup(optarg
);
212 case 'I': /* Index */
215 opts
->indexNames
= strdup(optarg
);
217 case 'T': /* Trigger */
219 opts
->selTrigger
= 1;
220 opts
->triggerNames
= strdup(optarg
);
222 case 's': /* dump schema only */
223 opts
->schemaOnly
= 1;
225 case 'S': /* Superuser username */
226 if (strlen(optarg
) != 0)
227 opts
->superuser
= strdup(optarg
);
229 case 't': /* Dump data for this table only */
232 opts
->tableNames
= strdup(optarg
);
236 opts
->username
= optarg
;
239 case 'v': /* verbose */
244 opts
->requirePassword
= true;
247 case 'x': /* skip ACL dump */
252 /* -X is a deprecated alternative to long options */
253 if (strcmp(optarg
, "disable-triggers") == 0)
254 disable_triggers
= 1;
255 else if (strcmp(optarg
, "no-data-for-failed-tables") == 0)
256 no_data_for_failed_tables
= 1;
257 else if (strcmp(optarg
, "no-tablespaces") == 0)
258 outputNoTablespaces
= 1;
259 else if (strcmp(optarg
, "use-set-session-authorization") == 0)
264 _("%s: invalid -X option -- %s\n"),
266 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
271 case '1': /* Restore data in a single transaction */
272 opts
->single_txn
= true;
273 opts
->exit_on_error
= true;
278 * This covers the long options without a short equivalent,
279 * including those equivalent to -X xxx.
283 case 2: /* SET ROLE */
284 opts
->use_role
= optarg
;
288 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"), progname
);
294 inputFileSpec
= argv
[optind
];
296 inputFileSpec
= NULL
;
298 /* Should get at most one of -d and -f, else user is confused */
303 fprintf(stderr
, _("%s: cannot specify both -d and -f output\n"),
305 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"),
312 /* Can't do single-txn mode with multiple connections */
313 if (opts
->single_txn
&& opts
->number_of_threads
> 1)
315 fprintf(stderr
, _("%s: cannot specify both --single-transaction and multiple threads\n"),
320 opts
->disable_triggers
= disable_triggers
;
321 opts
->noDataForFailedTables
= no_data_for_failed_tables
;
322 opts
->noTablespace
= outputNoTablespaces
;
323 opts
->use_setsessauth
= use_setsessauth
;
325 if (opts
->formatName
)
327 switch (opts
->formatName
[0])
331 opts
->format
= archCustom
;
336 opts
->format
= archFiles
;
341 opts
->format
= archTar
;
345 write_msg(NULL
, "unrecognized archive format \"%s\"; please specify \"c\" or \"t\"\n",
351 AH
= OpenArchive(inputFileSpec
, opts
->format
);
353 /* Let the archiver know how noisy to be */
354 AH
->verbose
= opts
->verbose
;
357 * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
359 AH
->exit_on_error
= opts
->exit_on_error
;
362 SortTocFromFile(AH
, opts
);
363 else if (opts
->noDataForFailedTables
)
366 * we implement this option by clearing idWanted entries, so must
367 * create a dummy idWanted array if there wasn't a tocFile
369 InitDummyWantedList(AH
, opts
);
372 if (opts
->tocSummary
)
373 PrintTOCSummary(AH
, opts
);
375 RestoreArchive(AH
, opts
);
377 /* done, print a summary of ignored errors */
379 fprintf(stderr
, _("WARNING: errors ignored on restore: %d\n"),
382 /* AH may be freed in CloseArchive? */
383 exit_code
= AH
->n_errors
? 1 : 0;
391 usage(const char *progname
)
393 printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname
);
394 printf(_("Usage:\n"));
395 printf(_(" %s [OPTION]... [FILE]\n"), progname
);
397 printf(_("\nGeneral options:\n"));
398 printf(_(" -d, --dbname=NAME connect to database name\n"));
399 printf(_(" -f, --file=FILENAME output file name\n"));
400 printf(_(" -F, --format=c|t backup file format (should be automatic)\n"));
401 printf(_(" -l, --list print summarized TOC of the archive\n"));
402 printf(_(" -v, --verbose verbose mode\n"));
403 printf(_(" --help show this help, then exit\n"));
404 printf(_(" --version output version information, then exit\n"));
406 printf(_("\nOptions controlling the restore:\n"));
407 printf(_(" -a, --data-only restore only the data, no schema\n"));
408 printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
409 printf(_(" -C, --create create the target database\n"));
410 printf(_(" -e, --exit-on-error exit on error, default is to continue\n"));
411 printf(_(" -I, --index=NAME restore named index\n"));
412 printf(_(" -L, --use-list=FILENAME use table of contents from this file for\n"
413 " selecting/ordering output\n"));
414 printf(_(" -m, --multi-thread=NUM use this many parallel connections to restore\n"));
415 printf(_(" -n, --schema=NAME restore only objects in this schema\n"));
416 printf(_(" -O, --no-owner skip restoration of object ownership\n"));
417 printf(_(" -P, --function=NAME(args)\n"
418 " restore named function\n"));
419 printf(_(" -s, --schema-only restore only the schema, no data\n"));
420 printf(_(" -S, --superuser=NAME superuser user name to use for disabling triggers\n"));
421 printf(_(" -t, --table=NAME restore named table\n"));
422 printf(_(" -T, --trigger=NAME restore named trigger\n"));
423 printf(_(" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
424 printf(_(" --disable-triggers disable triggers during data-only restore\n"));
425 printf(_(" --no-data-for-failed-tables\n"
426 " do not restore data of tables that could not be\n"
428 printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
429 printf(_(" --role=ROLENAME do SET ROLE before restore\n"));
430 printf(_(" --use-set-session-authorization\n"
431 " use SET SESSION AUTHORIZATION commands instead of\n"
432 " ALTER OWNER commands to set ownership\n"));
433 printf(_(" -1, --single-transaction\n"
434 " restore as a single transaction\n"));
436 printf(_("\nConnection options:\n"));
437 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
438 printf(_(" -p, --port=PORT database server port number\n"));
439 printf(_(" -U, --username=NAME connect as specified database user\n"));
440 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
442 printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
443 printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));