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