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