OSDN Git Service

918a93a656872ca905905b04459695238b6753c4
[pg-rex/syncrep.git] / 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.60 2004/08/20 04:20:23 momjian Exp $
38  *
39  *-------------------------------------------------------------------------
40  */
41
42 #include "pg_backup.h"
43 #include "pg_backup_archiver.h"
44 #include "dumputils.h"
45
46 #include <ctype.h>
47
48 #ifndef HAVE_STRDUP
49 #include "strdup.h"
50 #endif
51
52 #ifdef HAVE_TERMIOS_H
53 #include <termios.h>
54 #endif
55
56 #include <unistd.h>
57
58 #include "getopt_long.h"
59
60 #ifndef HAVE_OPTRESET
61 int                     optreset;
62 #endif
63
64 #ifdef ENABLE_NLS
65 #include <locale.h>
66 #endif
67
68 #define _(x) gettext((x))
69
70 /* Forward decls */
71 static void usage(const char *progname);
72
73 typedef struct option optType;
74
75 int
76 main(int argc, char **argv)
77 {
78         RestoreOptions *opts;
79         int                     c;
80         int                     exit_code;
81         Archive    *AH;
82         char       *inputFileSpec;
83         extern int      optind;
84         extern char *optarg;
85         static int      use_setsessauth = 0;
86         static int      disable_triggers = 0;
87
88         struct option cmdopts[] = {
89                 {"clean", 0, NULL, 'c'},
90                 {"create", 0, NULL, 'C'},
91                 {"data-only", 0, NULL, 'a'},
92                 {"dbname", 1, NULL, 'd'},
93                 {"exit-on-error", 0, NULL, 'e'},
94                 {"file", 1, NULL, 'f'},
95                 {"format", 1, NULL, 'F'},
96                 {"function", 1, NULL, 'P'},
97                 {"host", 1, NULL, 'h'},
98                 {"ignore-version", 0, NULL, 'i'},
99                 {"index", 1, NULL, 'I'},
100                 {"list", 0, NULL, 'l'},
101                 {"no-privileges", 0, NULL, 'x'},
102                 {"no-acl", 0, NULL, 'x'},
103                 {"no-owner", 0, NULL, 'O'},
104                 {"no-reconnect", 0, NULL, 'R'},
105                 {"port", 1, NULL, 'p'},
106                 {"password", 0, NULL, 'W'},
107                 {"schema-only", 0, NULL, 's'},
108                 {"superuser", 1, NULL, 'S'},
109                 {"table", 1, NULL, 't'},
110                 {"trigger", 1, NULL, 'T'},
111                 {"use-list", 1, NULL, 'L'},
112                 {"username", 1, NULL, 'U'},
113                 {"verbose", 0, NULL, 'v'},
114
115                 /*
116                  * the following options don't have an equivalent short option
117                  * letter, but are available as '-X long-name'
118                  */
119                 {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
120                 {"disable-triggers", no_argument, &disable_triggers, 1},
121
122                 {NULL, 0, NULL, 0}
123         };
124
125         set_pglocale_pgservice(argv[0], "pg_dump");
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:lL:Op:P:RsS:t:T:uU:vWxX:",
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
154                                                                  * create */
155                                 opts->dropSchema = 1;
156                                 break;
157                         case 'C':
158                                 opts->create = 1;
159                                 break;
160                         case 'd':
161                                 opts->dbname = strdup(optarg);
162                                 break;
163                         case 'e':
164                                 opts->exit_on_error = true;
165                                 break;
166                         case 'f':                       /* output file name */
167                                 opts->filename = strdup(optarg);
168                                 break;
169                         case 'F':
170                                 if (strlen(optarg) != 0)
171                                         opts->formatName = strdup(optarg);
172                                 break;
173                         case 'h':
174                                 if (strlen(optarg) != 0)
175                                         opts->pghost = strdup(optarg);
176                                 break;
177                         case 'i':
178                                 opts->ignoreVersion = 1;
179                                 break;
180
181                         case 'l':                       /* Dump the TOC summary */
182                                 opts->tocSummary = 1;
183                                 break;
184
185                         case 'L':                       /* input TOC summary file name */
186                                 opts->tocFile = strdup(optarg);
187                                 break;
188
189                         case 'O':
190                                 opts->noOwner = 1;
191                                 break;
192                         case 'p':
193                                 if (strlen(optarg) != 0)
194                                         opts->pgport = strdup(optarg);
195                                 break;
196                         case 'R':
197                                 /* no-op, still accepted for backwards compatibility */
198                                 break;
199                         case 'P':                       /* Function */
200                                 opts->selTypes = 1;
201                                 opts->selFunction = 1;
202                                 opts->functionNames = strdup(optarg);
203                                 break;
204                         case 'I':                       /* Index */
205                                 opts->selTypes = 1;
206                                 opts->selIndex = 1;
207                                 opts->indexNames = strdup(optarg);
208                                 break;
209                         case 'T':                       /* Trigger */
210                                 opts->selTypes = 1;
211                                 opts->selTrigger = 1;
212                                 opts->triggerNames = strdup(optarg);
213                                 break;
214                         case 's':                       /* dump schema only */
215                                 opts->schemaOnly = 1;
216                                 break;
217                         case 'S':                       /* Superuser username */
218                                 if (strlen(optarg) != 0)
219                                         opts->superuser = strdup(optarg);
220                                 break;
221                         case 't':                       /* Dump data for this table only */
222                                 opts->selTypes = 1;
223                                 opts->selTable = 1;
224                                 opts->tableNames = strdup(optarg);
225                                 break;
226
227                         case 'u':
228                                 opts->requirePassword = true;
229                                 opts->username = simple_prompt("User name: ", 100, true);
230                                 break;
231
232                         case 'U':
233                                 opts->username = optarg;
234                                 break;
235
236                         case 'v':                       /* verbose */
237                                 opts->verbose = 1;
238                                 break;
239
240                         case 'W':
241                                 opts->requirePassword = true;
242                                 break;
243
244                         case 'x':                       /* skip ACL dump */
245                                 opts->aclsSkip = 1;
246                                 break;
247
248                         case 'X':
249                                 if (strcmp(optarg, "use-set-session-authorization") == 0)
250                                         use_setsessauth = 1;
251                                 else if (strcmp(optarg, "disable-triggers") == 0)
252                                         disable_triggers = 1;
253                                 else
254                                 {
255                                         fprintf(stderr,
256                                                         _("%s: invalid -X option -- %s\n"),
257                                                         progname, optarg);
258                                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
259                                         exit(1);
260                                 }
261                                 break;
262
263                                 /* This covers the long options equivalent to -X xxx. */
264                         case 0:
265                                 break;
266
267                         default:
268                                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
269                                 exit(1);
270                 }
271         }
272
273         if (optind < argc)
274                 inputFileSpec = argv[optind];
275         else
276                 inputFileSpec = NULL;
277
278         /* Should get at most one of -d and -f, else user is confused */
279         if (opts->dbname)
280         {
281                 if (opts->filename)
282                 {
283                         fprintf(stderr, _("%s: cannot specify both -d and -f output\n"),
284                                         progname);
285                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
286                                         progname);
287                         exit(1);
288                 }
289                 opts->useDB = 1;
290         }
291
292         opts->disable_triggers = disable_triggers;
293         opts->use_setsessauth = use_setsessauth;
294
295         if (opts->formatName)
296         {
297
298                 switch (opts->formatName[0])
299                 {
300
301                         case 'c':
302                         case 'C':
303                                 opts->format = archCustom;
304                                 break;
305
306                         case 'f':
307                         case 'F':
308                                 opts->format = archFiles;
309                                 break;
310
311                         case 't':
312                         case 'T':
313                                 opts->format = archTar;
314                                 break;
315
316                         default:
317                                 write_msg("unrecognized archive format '%s'; please specify 't' or 'c'\n",
318                                                   opts->formatName);
319                                 exit(1);
320                 }
321         }
322
323         AH = OpenArchive(inputFileSpec, opts->format);
324
325         /* Let the archiver know how noisy to be */
326         AH->verbose = opts->verbose;
327
328         /*
329          *      Whether to keep submitting sql commands as "pg_restore ... | psql ... "
330          */
331         AH->exit_on_error = opts->exit_on_error;
332
333         if (opts->tocFile)
334                 SortTocFromFile(AH, opts);
335
336         if (opts->tocSummary)
337                 PrintTOCSummary(AH, opts);
338         else
339                 RestoreArchive(AH, opts);
340
341         /* done, print a summary of ignored errors */
342         if (AH->n_errors)
343                 fprintf(stderr, _("WARNING, errors ignored on restore: %d\n"), 
344                                 AH->n_errors);
345
346         /* AH may be freed in CloseArchive? */
347         exit_code = AH->n_errors? 1: 0;
348
349         CloseArchive(AH);
350
351         return exit_code;
352 }
353
354 static void
355 usage(const char *progname)
356 {
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);
360
361         printf(_("\nGeneral options:\n"));
362         printf(_("  -d, --dbname=NAME        output 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"));
370
371         printf(_("\nOptions controlling the output content:\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             issue commands to create the 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           do not output commands to set 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"));
392
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"));
399
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"));
402 }