]> granicus.if.org Git - postgresql/blob - src/bin/pg_dump/pg_restore.c
Add some enumeration commas, for consistency
[postgresql] / src / bin / pg_dump / pg_restore.c
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
42 #include "pg_backup_archiver.h"
43
44 #include "dumpmem.h"
45 #include "dumputils.h"
46
47 #include <ctype.h>
48
49 #ifdef HAVE_TERMIOS_H
50 #include <termios.h>
51 #endif
52
53 #include <unistd.h>
54
55 #include "getopt_long.h"
56
57 extern char *optarg;
58 extern int      optind;
59
60 #ifdef ENABLE_NLS
61 #include <locale.h>
62 #endif
63
64
65 static void usage(const char *progname);
66
67 typedef struct option optType;
68
69 int
70 main(int argc, char **argv)
71 {
72         RestoreOptions *opts;
73         int                     c;
74         int                     exit_code;
75         Archive    *AH;
76         char       *inputFileSpec;
77         static int      disable_triggers = 0;
78         static int      no_data_for_failed_tables = 0;
79         static int      outputNoTablespaces = 0;
80         static int      use_setsessauth = 0;
81         static int      no_security_labels = 0;
82
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                 {"jobs", 1, NULL, 'j'},
96                 {"list", 0, NULL, 'l'},
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                 {"no-password", 0, NULL, 'w'},
103                 {"password", 0, NULL, 'W'},
104                 {"schema", 1, NULL, 'n'},
105                 {"schema-only", 0, NULL, 's'},
106                 {"superuser", 1, NULL, 'S'},
107                 {"table", 1, NULL, 't'},
108                 {"trigger", 1, NULL, 'T'},
109                 {"use-list", 1, NULL, 'L'},
110                 {"username", 1, NULL, 'U'},
111                 {"verbose", 0, NULL, 'v'},
112                 {"single-transaction", 0, NULL, '1'},
113
114                 /*
115                  * the following options don't have an equivalent short option letter
116                  */
117                 {"disable-triggers", no_argument, &disable_triggers, 1},
118                 {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
119                 {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
120                 {"role", required_argument, NULL, 2},
121                 {"section", required_argument, NULL, 3},
122                 {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
123                 {"no-security-labels", no_argument, &no_security_labels, 1},
124
125                 {NULL, 0, NULL, 0}
126         };
127
128         set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
129
130         init_parallel_dump_utils();
131
132         opts = NewRestoreOptions();
133
134         progname = get_progname(argv[0]);
135
136         if (argc > 1)
137         {
138                 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
139                 {
140                         usage(progname);
141                         exit_nicely(0);
142                 }
143                 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
144                 {
145                         puts("pg_restore (PostgreSQL) " PG_VERSION);
146                         exit_nicely(0);
147                 }
148         }
149
150         while ((c = getopt_long(argc, argv, "acCd:ef:F:h:iI:j:lL:n:Op:P:RsS:t:T:U:vwWx1",
151                                                         cmdopts, NULL)) != -1)
152         {
153                 switch (c)
154                 {
155                         case 'a':                       /* Dump data only */
156                                 opts->dataOnly = 1;
157                                 break;
158                         case 'c':                       /* clean (i.e., drop) schema prior to create */
159                                 opts->dropSchema = 1;
160                                 break;
161                         case 'C':
162                                 opts->createDB = 1;
163                                 break;
164                         case 'd':
165                                 opts->dbname = pg_strdup(optarg);
166                                 break;
167                         case 'e':
168                                 opts->exit_on_error = true;
169                                 break;
170                         case 'f':                       /* output file name */
171                                 opts->filename = pg_strdup(optarg);
172                                 break;
173                         case 'F':
174                                 if (strlen(optarg) != 0)
175                                         opts->formatName = pg_strdup(optarg);
176                                 break;
177                         case 'h':
178                                 if (strlen(optarg) != 0)
179                                         opts->pghost = pg_strdup(optarg);
180                                 break;
181                         case 'i':
182                                 /* ignored, deprecated option */
183                                 break;
184
185                         case 'j':                       /* number of restore jobs */
186                                 opts->number_of_jobs = atoi(optarg);
187                                 break;
188
189                         case 'l':                       /* Dump the TOC summary */
190                                 opts->tocSummary = 1;
191                                 break;
192
193                         case 'L':                       /* input TOC summary file name */
194                                 opts->tocFile = pg_strdup(optarg);
195                                 break;
196
197                         case 'n':                       /* Dump data for this schema only */
198                                 opts->schemaNames = pg_strdup(optarg);
199                                 break;
200
201                         case 'O':
202                                 opts->noOwner = 1;
203                                 break;
204
205                         case 'p':
206                                 if (strlen(optarg) != 0)
207                                         opts->pgport = pg_strdup(optarg);
208                                 break;
209                         case 'R':
210                                 /* no-op, still accepted for backwards compatibility */
211                                 break;
212                         case 'P':                       /* Function */
213                                 opts->selTypes = 1;
214                                 opts->selFunction = 1;
215                                 opts->functionNames = pg_strdup(optarg);
216                                 break;
217                         case 'I':                       /* Index */
218                                 opts->selTypes = 1;
219                                 opts->selIndex = 1;
220                                 opts->indexNames = pg_strdup(optarg);
221                                 break;
222                         case 'T':                       /* Trigger */
223                                 opts->selTypes = 1;
224                                 opts->selTrigger = 1;
225                                 opts->triggerNames = pg_strdup(optarg);
226                                 break;
227                         case 's':                       /* dump schema only */
228                                 opts->schemaOnly = 1;
229                                 break;
230                         case 'S':                       /* Superuser username */
231                                 if (strlen(optarg) != 0)
232                                         opts->superuser = pg_strdup(optarg);
233                                 break;
234                         case 't':                       /* Dump data for this table only */
235                                 opts->selTypes = 1;
236                                 opts->selTable = 1;
237                                 opts->tableNames = pg_strdup(optarg);
238                                 break;
239
240                         case 'U':
241                                 opts->username = optarg;
242                                 break;
243
244                         case 'v':                       /* verbose */
245                                 opts->verbose = 1;
246                                 break;
247
248                         case 'w':
249                                 opts->promptPassword = TRI_NO;
250                                 break;
251
252                         case 'W':
253                                 opts->promptPassword = TRI_YES;
254                                 break;
255
256                         case 'x':                       /* skip ACL dump */
257                                 opts->aclsSkip = 1;
258                                 break;
259
260                         case '1':                       /* Restore data in a single transaction */
261                                 opts->single_txn = true;
262                                 opts->exit_on_error = true;
263                                 break;
264
265                         case 0:
266
267                                 /*
268                                  * This covers the long options without a short equivalent.
269                                  */
270                                 break;
271
272                         case 2:                         /* SET ROLE */
273                                 opts->use_role = optarg;
274                                 break;
275
276                         case 3:                         /* section */
277                                 set_section(optarg, &(opts->dumpSections));
278                                 break;
279
280                         default:
281                                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
282                                 exit_nicely(1);
283                 }
284         }
285
286         /* Get file name from command line */
287         if (optind < argc)
288                 inputFileSpec = argv[optind++];
289         else
290                 inputFileSpec = NULL;
291
292         /* Complain if any arguments remain */
293         if (optind < argc)
294         {
295                 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
296                                 progname, argv[optind]);
297                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
298                                 progname);
299                 exit_nicely(1);
300         }
301
302         if (opts->dataOnly && opts->schemaOnly)
303         {
304                 fprintf(stderr, _("%s: options -s/--schema-only and -a/--data-only cannot be used together\n"),
305                         progname);
306                 exit_nicely(1);
307         }
308
309         if ((opts->dataOnly || opts->schemaOnly) && (opts->dumpSections != DUMP_UNSECTIONED))
310         {
311                 fprintf(stderr, _("%s: options -s/--schema-only and -a/--data-only cannot be used with --section\n"),
312                         progname);
313                 exit_nicely(1);
314         }
315
316         if (opts->dataOnly)
317                 opts->dumpSections = DUMP_DATA;
318         else if (opts->schemaOnly)
319                 opts->dumpSections = DUMP_PRE_DATA | DUMP_POST_DATA;
320         else if ( opts->dumpSections != DUMP_UNSECTIONED)
321         {
322                 opts->dataOnly = opts->dumpSections == DUMP_DATA;
323                 opts->schemaOnly = !(opts->dumpSections & DUMP_DATA);
324         }
325
326         /* Should get at most one of -d and -f, else user is confused */
327         if (opts->dbname)
328         {
329                 if (opts->filename)
330                 {
331                         fprintf(stderr, _("%s: options -d/--dbname and -f/--file cannot be used together\n"),
332                                         progname);
333                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
334                                         progname);
335                         exit_nicely(1);
336                 }
337                 opts->useDB = 1;
338         }
339
340         /* Can't do single-txn mode with multiple connections */
341         if (opts->single_txn && opts->number_of_jobs > 1)
342         {
343                 fprintf(stderr, _("%s: cannot specify both --single-transaction and multiple jobs\n"),
344                                 progname);
345                 exit_nicely(1);
346         }
347
348         opts->disable_triggers = disable_triggers;
349         opts->noDataForFailedTables = no_data_for_failed_tables;
350         opts->noTablespace = outputNoTablespaces;
351         opts->use_setsessauth = use_setsessauth;
352         opts->no_security_labels = no_security_labels;
353
354         if (opts->formatName)
355         {
356                 switch (opts->formatName[0])
357                 {
358                         case 'c':
359                         case 'C':
360                                 opts->format = archCustom;
361                                 break;
362
363                         case 'd':
364                         case 'D':
365                                 opts->format = archDirectory;
366                                 break;
367
368                         case 'f':
369                         case 'F':
370                                 opts->format = archFiles;
371                                 break;
372
373                         case 't':
374                         case 'T':
375                                 opts->format = archTar;
376                                 break;
377
378                         default:
379                                 write_msg(NULL, "unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"\n",
380                                                   opts->formatName);
381                                 exit_nicely(1);
382                 }
383         }
384
385         AH = OpenArchive(inputFileSpec, opts->format);
386
387         /* Let the archiver know how noisy to be */
388         AH->verbose = opts->verbose;
389
390         /*
391          * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
392          */
393         AH->exit_on_error = opts->exit_on_error;
394
395         if (opts->tocFile)
396                 SortTocFromFile(AH, opts);
397         else if (opts->noDataForFailedTables)
398         {
399                 /*
400                  * we implement this option by clearing idWanted entries, so must
401                  * create a dummy idWanted array if there wasn't a tocFile
402                  */
403                 InitDummyWantedList(AH, opts);
404         }
405
406         if (opts->tocSummary)
407                 PrintTOCSummary(AH, opts);
408         else
409                 RestoreArchive(AH, opts);
410
411         /* done, print a summary of ignored errors */
412         if (AH->n_errors)
413                 fprintf(stderr, _("WARNING: errors ignored on restore: %d\n"),
414                                 AH->n_errors);
415
416         /* AH may be freed in CloseArchive? */
417         exit_code = AH->n_errors ? 1 : 0;
418
419         CloseArchive(AH);
420
421         return exit_code;
422 }
423
424 static void
425 usage(const char *progname)
426 {
427         printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname);
428         printf(_("Usage:\n"));
429         printf(_("  %s [OPTION]... [FILE]\n"), progname);
430
431         printf(_("\nGeneral options:\n"));
432         printf(_("  -d, --dbname=NAME        connect to database name\n"));
433         printf(_("  -f, --file=FILENAME      output file name\n"));
434         printf(_("  -F, --format=c|d|t       backup file format (should be automatic)\n"));
435         printf(_("  -l, --list               print summarized TOC of the archive\n"));
436         printf(_("  -v, --verbose            verbose mode\n"));
437         printf(_("  --help                   show this help, then exit\n"));
438         printf(_("  --version                output version information, then exit\n"));
439
440         printf(_("\nOptions controlling the restore:\n"));
441         printf(_("  -a, --data-only          restore only the data, no schema\n"));
442         printf(_("  -c, --clean              clean (drop) database objects before recreating\n"));
443         printf(_("  -C, --create             create the target database\n"));
444         printf(_("  -e, --exit-on-error      exit on error, default is to continue\n"));
445         printf(_("  -I, --index=NAME         restore named index\n"));
446         printf(_("  -j, --jobs=NUM           use this many parallel jobs to restore\n"));
447         printf(_("  -L, --use-list=FILENAME  use table of contents from this file for\n"
448                          "                           selecting/ordering output\n"));
449         printf(_("  -n, --schema=NAME        restore only objects in this schema\n"));
450         printf(_("  -O, --no-owner           skip restoration of object ownership\n"));
451         printf(_("  -P, --function=NAME(args)\n"
452                          "                           restore named function\n"));
453         printf(_("  -s, --schema-only        restore only the schema, no data\n"));
454         printf(_("  -S, --superuser=NAME     superuser user name to use for disabling triggers\n"));
455         printf(_("  -t, --table=NAME         restore named table\n"));
456         printf(_("  -T, --trigger=NAME       restore named trigger\n"));
457         printf(_("  -x, --no-privileges      skip restoration of access privileges (grant/revoke)\n"));
458         printf(_("  -1, --single-transaction\n"
459                          "                           restore as a single transaction\n"));
460         printf(_("  --disable-triggers       disable triggers during data-only restore\n"));
461         printf(_("  --no-data-for-failed-tables\n"
462                          "                           do not restore data of tables that could not be\n"
463                          "                           created\n"));
464         printf(_("  --no-security-labels     do not restore security labels\n"));
465         printf(_("  --no-tablespaces         do not restore tablespace assignments\n"));
466         printf(_("  --section=SECTION        restore named section (pre-data, data, or post-data)\n"));
467         printf(_("  --use-set-session-authorization\n"
468                          "                           use SET SESSION AUTHORIZATION commands instead of\n"
469           "                           ALTER OWNER commands to set ownership\n"));
470
471         printf(_("\nConnection options:\n"));
472         printf(_("  -h, --host=HOSTNAME      database server host or socket directory\n"));
473         printf(_("  -p, --port=PORT          database server port number\n"));
474         printf(_("  -U, --username=NAME      connect as specified database user\n"));
475         printf(_("  -w, --no-password        never prompt for password\n"));
476         printf(_("  -W, --password           force password prompt (should happen automatically)\n"));
477         printf(_("  --role=ROLENAME          do SET ROLE before restore\n"));
478
479         printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
480         printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
481 }