]> granicus.if.org Git - postgresql/blob - src/bin/pg_dump/pg_restore.c
1625dc49d163df22c0c94c00d88f26b887a9b98c
[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.81 2006/09/27 15:41:23 tgl Exp $
38  *
39  *-------------------------------------------------------------------------
40  */
41
42 #include "pg_backup_archiver.h"
43
44 #include <ctype.h>
45
46 #ifdef HAVE_TERMIOS_H
47 #include <termios.h>
48 #endif
49
50 #include <unistd.h>
51
52 #include "getopt_long.h"
53
54 #ifndef HAVE_INT_OPTRESET
55 int                     optreset;
56 #endif
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         extern int      optind;
76         extern char *optarg;
77         static int      use_setsessauth = 0;
78         static int      disable_triggers = 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                 {"list", 0, NULL, 'l'},
93                 {"no-privileges", 0, NULL, 'x'},
94                 {"no-acl", 0, NULL, 'x'},
95                 {"no-owner", 0, NULL, 'O'},
96                 {"no-reconnect", 0, NULL, 'R'},
97                 {"port", 1, NULL, 'p'},
98                 {"password", 0, NULL, 'W'},
99                 {"schema", 1, NULL, 'n'},
100                 {"schema-only", 0, NULL, 's'},
101                 {"superuser", 1, NULL, 'S'},
102                 {"table", 1, NULL, 't'},
103                 {"trigger", 1, NULL, 'T'},
104                 {"use-list", 1, NULL, 'L'},
105                 {"username", 1, NULL, 'U'},
106                 {"verbose", 0, NULL, 'v'},
107                 {"single-transaction", 0, NULL, '1'},
108
109                 /*
110                  * the following options don't have an equivalent short option letter,
111                  * but are available as '-X long-name'
112                  */
113                 {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
114                 {"disable-triggers", no_argument, &disable_triggers, 1},
115
116                 {NULL, 0, NULL, 0}
117         };
118
119         set_pglocale_pgservice(argv[0], "pg_dump");
120
121         opts = NewRestoreOptions();
122
123         progname = get_progname(argv[0]);
124
125         if (argc > 1)
126         {
127                 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
128                 {
129                         usage(progname);
130                         exit(0);
131                 }
132                 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
133                 {
134                         puts("pg_restore (PostgreSQL) " PG_VERSION);
135                         exit(0);
136                 }
137         }
138
139         while ((c = getopt_long(argc, argv, "acCd:ef:F:h:iI:lL:n:Op:P:RsS:t:T:uU:vWxX:1",
140                                                         cmdopts, NULL)) != -1)
141         {
142                 switch (c)
143                 {
144                         case 'a':                       /* Dump data only */
145                                 opts->dataOnly = 1;
146                                 break;
147                         case 'c':                       /* clean (i.e., drop) schema prior to create */
148                                 opts->dropSchema = 1;
149                                 break;
150                         case 'C':
151                                 opts->create = 1;
152                                 break;
153                         case 'd':
154                                 opts->dbname = strdup(optarg);
155                                 break;
156                         case 'e':
157                                 opts->exit_on_error = true;
158                                 break;
159                         case 'f':                       /* output file name */
160                                 opts->filename = strdup(optarg);
161                                 break;
162                         case 'F':
163                                 if (strlen(optarg) != 0)
164                                         opts->formatName = strdup(optarg);
165                                 break;
166                         case 'h':
167                                 if (strlen(optarg) != 0)
168                                         opts->pghost = strdup(optarg);
169                                 break;
170                         case 'i':
171                                 opts->ignoreVersion = 1;
172                                 break;
173
174                         case 'l':                       /* Dump the TOC summary */
175                                 opts->tocSummary = 1;
176                                 break;
177
178                         case 'L':                       /* input TOC summary file name */
179                                 opts->tocFile = strdup(optarg);
180                                 break;
181
182                         case 'n':                       /* Dump data for this schema only */
183                                 opts->schemaNames = strdup(optarg);
184                                 break;
185
186                         case 'O':
187                                 opts->noOwner = 1;
188                                 break;
189
190                         case 'p':
191                                 if (strlen(optarg) != 0)
192                                         opts->pgport = strdup(optarg);
193                                 break;
194                         case 'R':
195                                 /* no-op, still accepted for backwards compatibility */
196                                 break;
197                         case 'P':                       /* Function */
198                                 opts->selTypes = 1;
199                                 opts->selFunction = 1;
200                                 opts->functionNames = strdup(optarg);
201                                 break;
202                         case 'I':                       /* Index */
203                                 opts->selTypes = 1;
204                                 opts->selIndex = 1;
205                                 opts->indexNames = strdup(optarg);
206                                 break;
207                         case 'T':                       /* Trigger */
208                                 opts->selTypes = 1;
209                                 opts->selTrigger = 1;
210                                 opts->triggerNames = strdup(optarg);
211                                 break;
212                         case 's':                       /* dump schema only */
213                                 opts->schemaOnly = 1;
214                                 break;
215                         case 'S':                       /* Superuser username */
216                                 if (strlen(optarg) != 0)
217                                         opts->superuser = strdup(optarg);
218                                 break;
219                         case 't':                       /* Dump data for this table only */
220                                 opts->selTypes = 1;
221                                 opts->selTable = 1;
222                                 opts->tableNames = strdup(optarg);
223                                 break;
224
225                         case 'u':
226                                 opts->requirePassword = true;
227                                 opts->username = simple_prompt("User name: ", 100, true);
228                                 break;
229
230                         case 'U':
231                                 opts->username = optarg;
232                                 break;
233
234                         case 'v':                       /* verbose */
235                                 opts->verbose = 1;
236                                 break;
237
238                         case 'W':
239                                 opts->requirePassword = true;
240                                 break;
241
242                         case 'x':                       /* skip ACL dump */
243                                 opts->aclsSkip = 1;
244                                 break;
245
246                         case 'X':
247                                 if (strcmp(optarg, "use-set-session-authorization") == 0)
248                                         use_setsessauth = 1;
249                                 else if (strcmp(optarg, "disable-triggers") == 0)
250                                         disable_triggers = 1;
251                                 else if (strcmp(optarg, "no-data-for-failed-tables") == 0)
252                                         opts->noDataForFailedTables = 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                         case '1':                       /* Restore data in a single transaction */
268                                 opts->single_txn = true;
269                                 opts->exit_on_error = true;
270                                 break;
271
272                         default:
273                                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
274                                 exit(1);
275                 }
276         }
277
278         if (optind < argc)
279                 inputFileSpec = argv[optind];
280         else
281                 inputFileSpec = NULL;
282
283         /* Should get at most one of -d and -f, else user is confused */
284         if (opts->dbname)
285         {
286                 if (opts->filename)
287                 {
288                         fprintf(stderr, _("%s: cannot specify both -d and -f output\n"),
289                                         progname);
290                         fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
291                                         progname);
292                         exit(1);
293                 }
294                 opts->useDB = 1;
295         }
296
297         opts->disable_triggers = disable_triggers;
298         opts->use_setsessauth = use_setsessauth;
299
300         if (opts->formatName)
301         {
302
303                 switch (opts->formatName[0])
304                 {
305
306                         case 'c':
307                         case 'C':
308                                 opts->format = archCustom;
309                                 break;
310
311                         case 'f':
312                         case 'F':
313                                 opts->format = archFiles;
314                                 break;
315
316                         case 't':
317                         case 'T':
318                                 opts->format = archTar;
319                                 break;
320
321                         default:
322                                 write_msg(NULL, "unrecognized archive format \"%s\"; please specify \"c\" or \"t\"\n",
323                                                   opts->formatName);
324                                 exit(1);
325                 }
326         }
327
328         AH = OpenArchive(inputFileSpec, opts->format);
329
330         /* Let the archiver know how noisy to be */
331         AH->verbose = opts->verbose;
332
333         /*
334          * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
335          */
336         AH->exit_on_error = opts->exit_on_error;
337
338         if (opts->tocFile)
339                 SortTocFromFile(AH, opts);
340
341         if (opts->tocSummary)
342                 PrintTOCSummary(AH, opts);
343         else
344                 RestoreArchive(AH, opts);
345
346         /* done, print a summary of ignored errors */
347         if (AH->n_errors)
348                 fprintf(stderr, _("WARNING: errors ignored on restore: %d\n"),
349                                 AH->n_errors);
350
351         /* AH may be freed in CloseArchive? */
352         exit_code = AH->n_errors ? 1 : 0;
353
354         CloseArchive(AH);
355
356         return exit_code;
357 }
358
359 static void
360 usage(const char *progname)
361 {
362         printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname);
363         printf(_("Usage:\n"));
364         printf(_("  %s [OPTION]... [FILE]\n"), progname);
365
366         printf(_("\nGeneral options:\n"));
367         printf(_("  -d, --dbname=NAME        connect to database name\n"));
368         printf(_("  -f, --file=FILENAME      output file name\n"));
369         printf(_("  -F, --format=c|t         specify backup file format\n"));
370         printf(_("  -i, --ignore-version     proceed even when server version mismatches\n"));
371         printf(_("  -l, --list               print summarized TOC of the archive\n"));
372         printf(_("  -v, --verbose            verbose mode\n"));
373         printf(_("  --help                   show this help, then exit\n"));
374         printf(_("  --version                output version information, then exit\n"));
375
376         printf(_("\nOptions controlling the restore:\n"));
377         printf(_("  -a, --data-only          restore only the data, no schema\n"));
378         printf(_("  -c, --clean              clean (drop) schema prior to create\n"));
379         printf(_("  -C, --create             create the target database\n"));
380         printf(_("  -I, --index=NAME         restore named index\n"));
381         printf(_("  -L, --use-list=FILENAME  use specified table of contents for ordering\n"
382                          "                           output from this file\n"));
383         printf(_("  -n, --schema=NAME        restore only objects in this schema\n"));
384         printf(_("  -O, --no-owner           skip restoration of object ownership\n"));
385         printf(_("  -P, --function=NAME(args)\n"
386                          "                           restore named function\n"));
387         printf(_("  -s, --schema-only        restore only the schema, no data\n"));
388         printf(_("  -S, --superuser=NAME     specify the superuser user name to use for\n"
389                          "                           disabling triggers\n"));
390         printf(_("  -t, --table=NAME         restore named table\n"));
391         printf(_("  -T, --trigger=NAME       restore named trigger\n"));
392         printf(_("  -x, --no-privileges      skip restoration of access privileges (grant/revoke)\n"));
393         printf(_("  -X disable-triggers, --disable-triggers\n"
394                          "                           disable triggers during data-only restore\n"));
395         printf(_("  -X use-set-session-authorization, --use-set-session-authorization\n"
396                          "                           use SESSION AUTHORIZATION commands instead of\n"
397                          "                           OWNER TO commands\n"));
398         printf(_("  -X no-data-for-failed-tables\n"
399                          "                           do not restore data of tables which could not be\n"
400                          "                           created\n"));
401         printf(_("  -1, --single-transaction restore as a single transaction\n"));
402
403         printf(_("\nConnection options:\n"));
404         printf(_("  -h, --host=HOSTNAME      database server host or socket directory\n"));
405         printf(_("  -p, --port=PORT          database server port number\n"));
406         printf(_("  -U, --username=NAME      connect as specified database user\n"));
407         printf(_("  -W, --password           force password prompt (should happen automatically)\n"));
408         printf(_("  -e, --exit-on-error      exit on error, default is to continue\n"));
409
410         printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
411         printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
412 }