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.
37 * $PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.69 2005/02/22 04:39:38 momjian Exp $
39 *-------------------------------------------------------------------------
42 #include "pg_backup.h"
43 #include "pg_backup_archiver.h"
44 #include "dumputils.h"
58 #include "getopt_long.h"
60 #ifndef HAVE_INT_OPTRESET
70 static void usage(const char *progname);
72 typedef struct option optType;
75 main(int argc, char **argv)
84 static int use_setsessauth = 0;
85 static int disable_triggers = 0;
87 struct option cmdopts[] = {
88 {"clean", 0, NULL, 'c'},
89 {"create", 0, NULL, 'C'},
90 {"data-only", 0, NULL, 'a'},
91 {"dbname", 1, NULL, 'd'},
92 {"exit-on-error", 0, NULL, 'e'},
93 {"file", 1, NULL, 'f'},
94 {"format", 1, NULL, 'F'},
95 {"function", 1, NULL, 'P'},
96 {"host", 1, NULL, 'h'},
97 {"ignore-version", 0, NULL, 'i'},
98 {"index", 1, NULL, 'I'},
99 {"list", 0, NULL, 'l'},
100 {"no-privileges", 0, NULL, 'x'},
101 {"no-acl", 0, NULL, 'x'},
102 {"no-owner", 0, NULL, 'O'},
103 {"no-reconnect", 0, NULL, 'R'},
104 {"port", 1, NULL, 'p'},
105 {"password", 0, NULL, 'W'},
106 {"schema-only", 0, NULL, 's'},
107 {"superuser", 1, NULL, 'S'},
108 {"table", 1, NULL, 't'},
109 {"trigger", 1, NULL, 'T'},
110 {"use-list", 1, NULL, 'L'},
111 {"username", 1, NULL, 'U'},
112 {"verbose", 0, NULL, 'v'},
115 * the following options don't have an equivalent short option
116 * letter, but are available as '-X long-name'
118 {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
119 {"disable-triggers", no_argument, &disable_triggers, 1},
124 set_pglocale_pgservice(argv[0], "pg_dump");
126 opts = NewRestoreOptions();
128 progname = get_progname(argv[0]);
132 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
137 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
139 puts("pg_restore (PostgreSQL) " PG_VERSION);
144 while ((c = getopt_long(argc, argv, "acCd:ef:F:h:iI:lL:Op:P:RsS:t:T:uU:vWxX:",
145 cmdopts, NULL)) != -1)
149 case 'a': /* Dump data only */
152 case 'c': /* clean (i.e., drop) schema prior to
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 opts->ignoreVersion = 1;
180 case 'l': /* Dump the TOC summary */
181 opts->tocSummary = 1;
184 case 'L': /* input TOC summary file name */
185 opts->tocFile = strdup(optarg);
192 if (strlen(optarg) != 0)
193 opts->pgport = strdup(optarg);
196 /* no-op, still accepted for backwards compatibility */
198 case 'P': /* Function */
200 opts->selFunction = 1;
201 opts->functionNames = strdup(optarg);
203 case 'I': /* Index */
206 opts->indexNames = strdup(optarg);
208 case 'T': /* Trigger */
210 opts->selTrigger = 1;
211 opts->triggerNames = strdup(optarg);
213 case 's': /* dump schema only */
214 opts->schemaOnly = 1;
216 case 'S': /* Superuser username */
217 if (strlen(optarg) != 0)
218 opts->superuser = strdup(optarg);
220 case 't': /* Dump data for this table only */
223 opts->tableNames = strdup(optarg);
227 opts->requirePassword = true;
228 opts->username = simple_prompt("User name: ", 100, true);
232 opts->username = optarg;
235 case 'v': /* verbose */
240 opts->requirePassword = true;
243 case 'x': /* skip ACL dump */
248 if (strcmp(optarg, "use-set-session-authorization") == 0)
250 else if (strcmp(optarg, "disable-triggers") == 0)
251 disable_triggers = 1;
255 _("%s: invalid -X option -- %s\n"),
257 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
262 /* This covers the long options equivalent to -X xxx. */
267 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
273 inputFileSpec = argv[optind];
275 inputFileSpec = NULL;
277 /* Should get at most one of -d and -f, else user is confused */
282 fprintf(stderr, _("%s: cannot specify both -d and -f output\n"),
284 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
291 opts->disable_triggers = disable_triggers;
292 opts->use_setsessauth = use_setsessauth;
294 if (opts->formatName)
297 switch (opts->formatName[0])
302 opts->format = archCustom;
307 opts->format = archFiles;
312 opts->format = archTar;
316 write_msg("unrecognized archive format '%s'; please specify 't' or 'c'\n",
322 AH = OpenArchive(inputFileSpec, opts->format);
324 /* Let the archiver know how noisy to be */
325 AH->verbose = opts->verbose;
328 * Whether to keep submitting sql commands as "pg_restore ... | psql
331 AH->exit_on_error = opts->exit_on_error;
334 SortTocFromFile(AH, opts);
336 if (opts->tocSummary)
337 PrintTOCSummary(AH, opts);
339 RestoreArchive(AH, opts);
341 /* done, print a summary of ignored errors */
343 fprintf(stderr, _("WARNING: errors ignored on restore: %d\n"),
346 /* AH may be freed in CloseArchive? */
347 exit_code = AH->n_errors ? 1 : 0;
355 usage(const char *progname)
357 printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname);
358 printf(_("Usage:\n"));
359 printf(_(" %s [OPTION]... [FILE]\n"), progname);
361 printf(_("\nGeneral options:\n"));
362 printf(_(" -d, --dbname=NAME connect to database name\n"));
363 printf(_(" -f, --file=FILENAME output file name\n"));
364 printf(_(" -F, --format=c|t specify backup file format\n"));
365 printf(_(" -i, --ignore-version proceed even when server version mismatches\n"));
366 printf(_(" -l, --list print summarized TOC of the archive\n"));
367 printf(_(" -v, --verbose verbose mode\n"));
368 printf(_(" --help show this help, then exit\n"));
369 printf(_(" --version output version information, then exit\n"));
371 printf(_("\nOptions controlling the restore:\n"));
372 printf(_(" -a, --data-only restore only the data, no schema\n"));
373 printf(_(" -c, --clean clean (drop) schema prior to create\n"));
374 printf(_(" -C, --create create the target database\n"));
375 printf(_(" -I, --index=NAME restore named index\n"));
376 printf(_(" -L, --use-list=FILENAME use specified table of contents for ordering\n"
377 " output from this file\n"));
378 printf(_(" -O, --no-owner skip restoration of object ownership\n"));
379 printf(_(" -P, --function=NAME(args)\n"
380 " restore named function\n"));
381 printf(_(" -s, --schema-only restore only the schema, no data\n"));
382 printf(_(" -S, --superuser=NAME specify the superuser user name to use for\n"
383 " disabling triggers\n"));
384 printf(_(" -t, --table=NAME restore named table\n"));
385 printf(_(" -T, --trigger=NAME restore named trigger\n"));
386 printf(_(" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
387 printf(_(" -X disable-triggers, --disable-triggers\n"
388 " disable triggers during data-only restore\n"));
389 printf(_(" -X use-set-session-authorization, --use-set-session-authorization\n"
390 " use SESSION AUTHORIZATION commands instead of\n"
391 " OWNER TO commands\n"));
393 printf(_("\nConnection options:\n"));
394 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
395 printf(_(" -p, --port=PORT database server port number\n"));
396 printf(_(" -U, --username=NAME connect as specified database user\n"));
397 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
398 printf(_(" -e, --exit-on-error exit on error, default is to continue\n"));
400 printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
401 printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));