Force a checkpoint in CREATE DATABASE before starting to copy the files,
[PostgreSQL.git] / src / bin / pg_dump / pg_restore.c
blob899a8a4e4fa4506f7ec74faa44043146e81d8fe3
1 /*-------------------------------------------------------------------------
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.
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
19 * the output script is SQL that is understood by PostgreSQL
21 * Basic process in a restore operation is:
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
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.
32 * The author is not responsible for loss or damages that may
33 * result from its use.
36 * IDENTIFICATION
37 * $PostgreSQL$
39 *-------------------------------------------------------------------------
42 #include "pg_backup_archiver.h"
44 #include <ctype.h>
46 #ifdef HAVE_TERMIOS_H
47 #include <termios.h>
48 #endif
50 #include <unistd.h>
52 #include "getopt_long.h"
54 #ifndef HAVE_INT_OPTRESET
55 int optreset;
56 #endif
58 #ifdef ENABLE_NLS
59 #include <locale.h>
60 #endif
63 static void usage(const char *progname);
65 typedef struct option optType;
67 int
68 main(int argc, char **argv)
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 disable_triggers = 0;
78 static int no_data_for_failed_tables = 0;
79 static int outputNoTablespaces = 0;
80 static int use_setsessauth = 0;
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 {"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 {"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'},
112 * the following options don't have an equivalent short option letter
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 {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
119 {NULL, 0, NULL, 0}
122 set_pglocale_pgservice(argv[0], "pg_dump");
124 opts = NewRestoreOptions();
126 progname = get_progname(argv[0]);
128 if (argc > 1)
130 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
132 usage(progname);
133 exit(0);
135 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
137 puts("pg_restore (PostgreSQL) " PG_VERSION);
138 exit(0);
142 while ((c = getopt_long(argc, argv, "acCd:ef:F:h:iI:lL:n:Op:P:RsS:t:T:U:vWxX:1",
143 cmdopts, NULL)) != -1)
145 switch (c)
147 case 'a': /* Dump data only */
148 opts->dataOnly = 1;
149 break;
150 case 'c': /* clean (i.e., drop) schema prior to create */
151 opts->dropSchema = 1;
152 break;
153 case 'C':
154 opts->create = 1;
155 break;
156 case 'd':
157 opts->dbname = strdup(optarg);
158 break;
159 case 'e':
160 opts->exit_on_error = true;
161 break;
162 case 'f': /* output file name */
163 opts->filename = strdup(optarg);
164 break;
165 case 'F':
166 if (strlen(optarg) != 0)
167 opts->formatName = strdup(optarg);
168 break;
169 case 'h':
170 if (strlen(optarg) != 0)
171 opts->pghost = strdup(optarg);
172 break;
173 case 'i':
174 /* ignored, deprecated option */
175 break;
177 case 'l': /* Dump the TOC summary */
178 opts->tocSummary = 1;
179 break;
181 case 'L': /* input TOC summary file name */
182 opts->tocFile = strdup(optarg);
183 break;
185 case 'n': /* Dump data for this schema only */
186 opts->schemaNames = strdup(optarg);
187 break;
189 case 'O':
190 opts->noOwner = 1;
191 break;
193 case 'p':
194 if (strlen(optarg) != 0)
195 opts->pgport = strdup(optarg);
196 break;
197 case 'R':
198 /* no-op, still accepted for backwards compatibility */
199 break;
200 case 'P': /* Function */
201 opts->selTypes = 1;
202 opts->selFunction = 1;
203 opts->functionNames = strdup(optarg);
204 break;
205 case 'I': /* Index */
206 opts->selTypes = 1;
207 opts->selIndex = 1;
208 opts->indexNames = strdup(optarg);
209 break;
210 case 'T': /* Trigger */
211 opts->selTypes = 1;
212 opts->selTrigger = 1;
213 opts->triggerNames = strdup(optarg);
214 break;
215 case 's': /* dump schema only */
216 opts->schemaOnly = 1;
217 break;
218 case 'S': /* Superuser username */
219 if (strlen(optarg) != 0)
220 opts->superuser = strdup(optarg);
221 break;
222 case 't': /* Dump data for this table only */
223 opts->selTypes = 1;
224 opts->selTable = 1;
225 opts->tableNames = strdup(optarg);
226 break;
228 case 'U':
229 opts->username = optarg;
230 break;
232 case 'v': /* verbose */
233 opts->verbose = 1;
234 break;
236 case 'W':
237 opts->requirePassword = true;
238 break;
240 case 'x': /* skip ACL dump */
241 opts->aclsSkip = 1;
242 break;
244 case 'X':
245 /* -X is a deprecated alternative to long options */
246 if (strcmp(optarg, "disable-triggers") == 0)
247 disable_triggers = 1;
248 else if (strcmp(optarg, "no-data-for-failed-tables") == 0)
249 no_data_for_failed_tables = 1;
250 else if (strcmp(optarg, "no-tablespaces") == 0)
251 outputNoTablespaces = 1;
252 else if (strcmp(optarg, "use-set-session-authorization") == 0)
253 use_setsessauth = 1;
254 else
256 fprintf(stderr,
257 _("%s: invalid -X option -- %s\n"),
258 progname, optarg);
259 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
260 exit(1);
262 break;
264 case 0:
265 /* This covers the long options equivalent to -X xxx. */
266 break;
268 case '1': /* Restore data in a single transaction */
269 opts->single_txn = true;
270 opts->exit_on_error = true;
271 break;
273 default:
274 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
275 exit(1);
279 if (optind < argc)
280 inputFileSpec = argv[optind];
281 else
282 inputFileSpec = NULL;
284 /* Should get at most one of -d and -f, else user is confused */
285 if (opts->dbname)
287 if (opts->filename)
289 fprintf(stderr, _("%s: cannot specify both -d and -f output\n"),
290 progname);
291 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
292 progname);
293 exit(1);
295 opts->useDB = 1;
298 opts->disable_triggers = disable_triggers;
299 opts->noDataForFailedTables = no_data_for_failed_tables;
300 opts->noTablespace = outputNoTablespaces;
301 opts->use_setsessauth = use_setsessauth;
303 if (opts->formatName)
306 switch (opts->formatName[0])
309 case 'c':
310 case 'C':
311 opts->format = archCustom;
312 break;
314 case 'f':
315 case 'F':
316 opts->format = archFiles;
317 break;
319 case 't':
320 case 'T':
321 opts->format = archTar;
322 break;
324 default:
325 write_msg(NULL, "unrecognized archive format \"%s\"; please specify \"c\" or \"t\"\n",
326 opts->formatName);
327 exit(1);
331 AH = OpenArchive(inputFileSpec, opts->format);
333 /* Let the archiver know how noisy to be */
334 AH->verbose = opts->verbose;
337 * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
339 AH->exit_on_error = opts->exit_on_error;
341 if (opts->tocFile)
342 SortTocFromFile(AH, opts);
343 else if (opts->noDataForFailedTables)
346 * we implement this option by clearing idWanted entries, so must
347 * create a dummy idWanted array if there wasn't a tocFile
349 InitDummyWantedList(AH, opts);
352 if (opts->tocSummary)
353 PrintTOCSummary(AH, opts);
354 else
355 RestoreArchive(AH, opts);
357 /* done, print a summary of ignored errors */
358 if (AH->n_errors)
359 fprintf(stderr, _("WARNING: errors ignored on restore: %d\n"),
360 AH->n_errors);
362 /* AH may be freed in CloseArchive? */
363 exit_code = AH->n_errors ? 1 : 0;
365 CloseArchive(AH);
367 return exit_code;
370 static void
371 usage(const char *progname)
373 printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname);
374 printf(_("Usage:\n"));
375 printf(_(" %s [OPTION]... [FILE]\n"), progname);
377 printf(_("\nGeneral options:\n"));
378 printf(_(" -d, --dbname=NAME connect to database name\n"));
379 printf(_(" -f, --file=FILENAME output file name\n"));
380 printf(_(" -F, --format=c|t specify backup file format\n"));
381 printf(_(" -l, --list print summarized TOC of the archive\n"));
382 printf(_(" -v, --verbose verbose mode\n"));
383 printf(_(" --help show this help, then exit\n"));
384 printf(_(" --version output version information, then exit\n"));
386 printf(_("\nOptions controlling the restore:\n"));
387 printf(_(" -a, --data-only restore only the data, no schema\n"));
388 printf(_(" -c, --clean clean (drop) schema prior to create\n"));
389 printf(_(" -C, --create create the target database\n"));
390 printf(_(" -I, --index=NAME restore named index\n"));
391 printf(_(" -L, --use-list=FILENAME use specified table of contents for ordering\n"
392 " output from this file\n"));
393 printf(_(" -n, --schema=NAME restore only objects in this schema\n"));
394 printf(_(" -O, --no-owner skip restoration of object ownership\n"));
395 printf(_(" -P, --function=NAME(args)\n"
396 " restore named function\n"));
397 printf(_(" -s, --schema-only restore only the schema, no data\n"));
398 printf(_(" -S, --superuser=NAME specify the superuser user name to use for\n"
399 " disabling triggers\n"));
400 printf(_(" -t, --table=NAME restore named table\n"));
401 printf(_(" -T, --trigger=NAME restore named trigger\n"));
402 printf(_(" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
403 printf(_(" --disable-triggers disable triggers during data-only restore\n"));
404 printf(_(" --no-data-for-failed-tables\n"
405 " do not restore data of tables that could not be\n"
406 " created\n"));
407 printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
408 printf(_(" --use-set-session-authorization\n"
409 " use SESSION AUTHORIZATION commands instead of\n"
410 " OWNER TO commands\n"));
411 printf(_(" -1, --single-transaction\n"
412 " restore as a single transaction\n"));
414 printf(_("\nConnection options:\n"));
415 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
416 printf(_(" -p, --port=PORT database server port number\n"));
417 printf(_(" -U, --username=NAME connect as specified database user\n"));
418 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
419 printf(_(" -e, --exit-on-error exit on error, default is to continue\n"));
421 printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
422 printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));