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