pg_restore.c 10.4 KB
Newer Older
1 2 3 4
/*-------------------------------------------------------------------------
 *
 * pg_restore.c
 *	pg_restore is an utility extracting postgres database definitions
5
 *	from a backup archive created by pg_dump using the archiver
6 7 8 9 10 11 12 13
 *	interface.
 *
 *	pg_restore will read the backup archive and
 *	dump out a script that reproduces
 *	the schema of the database in terms of
 *		  user-defined types
 *		  user-defined functions
 *		  tables
14
 *		  indexes
15 16 17 18 19 20 21
 *		  aggregates
 *		  operators
 *		  ACL - grant/revoke
 *
 * the output script is SQL that is understood by PostgreSQL
 *
 * Basic process in a restore operation is:
22 23 24 25 26
 *
 *	Open the Archive and read the TOC.
 *	Set flags in TOC entries, and *maybe* reorder them.
 *	Generate script to stdout
 *	Exit
27 28
 *
 * Copyright (c) 2000, Philip Warner
29 30
 *		Rights are granted to use this software in any way so long
 *		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.66 2004/11/11 17:06:46 momjian Exp $
38
 *
39 40 41
 *-------------------------------------------------------------------------
 */

42
#include "pg_backup.h"
43
#include "pg_backup_archiver.h"
44
#include "dumputils.h"
45

46 47 48 49 50 51 52 53 54 55
#include <ctype.h>

#ifndef HAVE_STRDUP
#include "strdup.h"
#endif

#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif

56 57
#include <unistd.h>

58
#include "getopt_long.h"
59 60

#ifndef HAVE_OPTRESET
Bruce Momjian's avatar
Bruce Momjian committed
61
int			optreset;
62 63
#endif

64 65 66 67
#ifdef ENABLE_NLS
#include <locale.h>
#endif

68 69
#define _(x) gettext((x))

70 71 72 73 74
/* Forward decls */
static void usage(const char *progname);

typedef struct option optType;

75 76
int
main(int argc, char **argv)
77
{
78 79
	RestoreOptions *opts;
	int			c;
80
	int			exit_code;
81
	Archive    *AH;
82
	char	   *inputFileSpec;
83 84
	extern int	optind;
	extern char *optarg;
85
	static int	use_setsessauth = 0;
86
	static int	disable_triggers = 0;
87 88 89 90 91 92

	struct option cmdopts[] = {
		{"clean", 0, NULL, 'c'},
		{"create", 0, NULL, 'C'},
		{"data-only", 0, NULL, 'a'},
		{"dbname", 1, NULL, 'd'},
93
		{"exit-on-error", 0, NULL, 'e'},
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
		{"file", 1, NULL, 'f'},
		{"format", 1, NULL, 'F'},
		{"function", 1, NULL, 'P'},
		{"host", 1, NULL, 'h'},
		{"ignore-version", 0, NULL, 'i'},
		{"index", 1, NULL, 'I'},
		{"list", 0, NULL, 'l'},
		{"no-privileges", 0, NULL, 'x'},
		{"no-acl", 0, NULL, 'x'},
		{"no-owner", 0, NULL, 'O'},
		{"no-reconnect", 0, NULL, 'R'},
		{"port", 1, NULL, 'p'},
		{"password", 0, NULL, 'W'},
		{"schema-only", 0, NULL, 's'},
		{"superuser", 1, NULL, 'S'},
		{"table", 1, NULL, 't'},
		{"trigger", 1, NULL, 'T'},
		{"use-list", 1, NULL, 'L'},
		{"username", 1, NULL, 'U'},
		{"verbose", 0, NULL, 'v'},

115 116 117 118
		/*
		 * the following options don't have an equivalent short option
		 * letter, but are available as '-X long-name'
		 */
119
		{"use-set-session-authorization", no_argument, &use_setsessauth, 1},
120 121
		{"disable-triggers", no_argument, &disable_triggers, 1},

122 123
		{NULL, 0, NULL, 0}
	};
124

125
	set_pglocale_pgservice(argv[0], "pg_dump");
126

127 128
	opts = NewRestoreOptions();

129
	progname = get_progname(argv[0]);
130 131 132

	if (argc > 1)
	{
133
		if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
134 135 136 137
		{
			usage(progname);
			exit(0);
		}
138
		if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
139 140 141 142 143
		{
			puts("pg_restore (PostgreSQL) " PG_VERSION);
			exit(0);
		}
	}
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 149 150 151 152 153 154 155 156
	{
		switch (c)
		{
			case 'a':			/* Dump data only */
				opts->dataOnly = 1;
				break;
			case 'c':			/* clean (i.e., drop) schema prior to
								 * create */
				opts->dropSchema = 1;
				break;
157 158 159
			case 'C':
				opts->create = 1;
				break;
160
			case 'd':
161
				opts->dbname = strdup(optarg);
162
				break;
163 164 165
			case 'e':
				opts->exit_on_error = true;
				break;
166 167 168 169
			case 'f':			/* output file name */
				opts->filename = strdup(optarg);
				break;
			case 'F':
170 171
				if (strlen(optarg) != 0)
					opts->formatName = strdup(optarg);
172 173 174 175 176 177 178 179
				break;
			case 'h':
				if (strlen(optarg) != 0)
					opts->pghost = strdup(optarg);
				break;
			case 'i':
				opts->ignoreVersion = 1;
				break;
180 181 182 183 184 185 186 187 188

			case 'l':			/* Dump the TOC summary */
				opts->tocSummary = 1;
				break;

			case 'L':			/* input TOC summary file name */
				opts->tocFile = strdup(optarg);
				break;

189
			case 'O':
190
				opts->noOwner = 1;
191 192 193 194 195
				break;
			case 'p':
				if (strlen(optarg) != 0)
					opts->pgport = strdup(optarg);
				break;
196
			case 'R':
197
				/* no-op, still accepted for backwards compatibility */
198
				break;
199
			case 'P':			/* Function */
200 201
				opts->selTypes = 1;
				opts->selFunction = 1;
202
				opts->functionNames = strdup(optarg);
203
				break;
204
			case 'I':			/* Index */
205 206
				opts->selTypes = 1;
				opts->selIndex = 1;
207
				opts->indexNames = strdup(optarg);
208
				break;
209
			case 'T':			/* Trigger */
210 211
				opts->selTypes = 1;
				opts->selTrigger = 1;
212
				opts->triggerNames = strdup(optarg);
213 214 215 216
				break;
			case 's':			/* dump schema only */
				opts->schemaOnly = 1;
				break;
217 218 219 220
			case 'S':			/* Superuser username */
				if (strlen(optarg) != 0)
					opts->superuser = strdup(optarg);
				break;
221 222 223
			case 't':			/* Dump data for this table only */
				opts->selTypes = 1;
				opts->selTable = 1;
224
				opts->tableNames = strdup(optarg);
225 226 227
				break;

			case 'u':
228
				opts->requirePassword = true;
229
				opts->username = simple_prompt("User name: ", 100, true);
230 231 232 233
				break;

			case 'U':
				opts->username = optarg;
234 235 236 237 238
				break;

			case 'v':			/* verbose */
				opts->verbose = 1;
				break;
239 240 241 242 243

			case 'W':
				opts->requirePassword = true;
				break;

244 245 246
			case 'x':			/* skip ACL dump */
				opts->aclsSkip = 1;
				break;
247 248

			case 'X':
249
				if (strcmp(optarg, "use-set-session-authorization") == 0)
250
					use_setsessauth = 1;
251 252
				else if (strcmp(optarg, "disable-triggers") == 0)
					disable_triggers = 1;
253 254 255
				else
				{
					fprintf(stderr,
256
							_("%s: invalid -X option -- %s\n"),
257
							progname, optarg);
258
					fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
259 260 261 262 263 264 265 266
					exit(1);
				}
				break;

				/* This covers the long options equivalent to -X xxx. */
			case 0:
				break;

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 277 278 279 280 281 282 283 284 285 286 287 288 289 290
		inputFileSpec = NULL;

	/* Should get at most one of -d and -f, else user is confused */
	if (opts->dbname)
	{
		if (opts->filename)
		{
			fprintf(stderr, _("%s: cannot specify both -d and -f output\n"),
					progname);
			fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
					progname);
			exit(1);
		}
		opts->useDB = 1;
	}
291

292
	opts->disable_triggers = disable_triggers;
293
	opts->use_setsessauth = use_setsessauth;
294

295 296
	if (opts->formatName)
	{
297

298 299
		switch (opts->formatName[0])
		{
300

301 302 303 304
			case 'c':
			case 'C':
				opts->format = archCustom;
				break;
305

306 307 308 309
			case 'f':
			case 'F':
				opts->format = archFiles;
				break;
310

311 312 313 314
			case 't':
			case 'T':
				opts->format = archTar;
				break;
315

316
			default:
317 318
				write_msg("unrecognized archive format '%s'; please specify 't' or 'c'\n",
						  opts->formatName);
319
				exit(1);
320
		}
321
	}
322

323
	AH = OpenArchive(inputFileSpec, opts->format);
324 325 326 327

	/* Let the archiver know how noisy to be */
	AH->verbose = opts->verbose;

328
	/*
Bruce Momjian's avatar
Bruce Momjian committed
329 330
	 * Whether to keep submitting sql commands as "pg_restore ... | psql
	 * ... "
331
	 */
332
	AH->exit_on_error = opts->exit_on_error;
333

334
	if (opts->tocFile)
335 336
		SortTocFromFile(AH, opts);

337
	if (opts->tocSummary)
338
		PrintTOCSummary(AH, opts);
339
	else
340 341
		RestoreArchive(AH, opts);

342 343
	/* done, print a summary of ignored errors */
	if (AH->n_errors)
Peter Eisentraut's avatar
Peter Eisentraut committed
344
		fprintf(stderr, _("WARNING: errors ignored on restore: %d\n"),
345 346 347
				AH->n_errors);

	/* AH may be freed in CloseArchive? */
Bruce Momjian's avatar
Bruce Momjian committed
348
	exit_code = AH->n_errors ? 1 : 0;
349

350
	CloseArchive(AH);
351

352
	return exit_code;
353 354
}

355 356
static void
usage(const char *progname)
357
{
358 359
	printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname);
	printf(_("Usage:\n"));
360
	printf(_("  %s [OPTION]... [FILE]\n"), progname);
361

362
	printf(_("\nGeneral options:\n"));
363
	printf(_("  -d, --dbname=NAME        connect to database name\n"));
Bruce Momjian's avatar
Bruce Momjian committed
364
	printf(_("  -f, --file=FILENAME      output file name\n"));
365
	printf(_("  -F, --format=c|t         specify backup file format\n"));
Bruce Momjian's avatar
Bruce Momjian committed
366 367
	printf(_("  -i, --ignore-version     proceed even when server version mismatches\n"));
	printf(_("  -l, --list               print summarized TOC of the archive\n"));
368 369 370 371
	printf(_("  -v, --verbose            verbose mode\n"));
	printf(_("  --help                   show this help, then exit\n"));
	printf(_("  --version                output version information, then exit\n"));

372
	printf(_("\nOptions controlling the restore:\n"));
373 374
	printf(_("  -a, --data-only          restore only the data, no schema\n"));
	printf(_("  -c, --clean              clean (drop) schema prior to create\n"));
375
	printf(_("  -C, --create             create the target database\n"));
376
	printf(_("  -I, --index=NAME         restore named index\n"));
Bruce Momjian's avatar
Bruce Momjian committed
377 378
	printf(_("  -L, --use-list=FILENAME  use specified table of contents for ordering\n"
			 "                           output from this file\n"));
379
	printf(_("  -O, --no-owner           do not issue commands to set object ownership\n"));
380 381
	printf(_("  -P, --function=NAME(args)\n"
			 "                           restore named function\n"));
Bruce Momjian's avatar
Bruce Momjian committed
382 383 384 385 386 387 388 389
	printf(_("  -s, --schema-only        restore only the schema, no data\n"));
	printf(_("  -S, --superuser=NAME     specify the superuser user name to use for\n"
			 "                           disabling triggers\n"));
	printf(_("  -t, --table=NAME         restore named table\n"));
	printf(_("  -T, --trigger=NAME       restore named trigger\n"));
	printf(_("  -x, --no-privileges      skip restoration of access privileges (grant/revoke)\n"));
	printf(_("  -X disable-triggers, --disable-triggers\n"
			 "                           disable triggers during data-only restore\n"));
390 391 392
	printf(_("  -X use-set-session-authorization, --use-set-session-authorization\n"
			 "                           use SESSION AUTHORIZATION commands instead of\n"
			 "                           OWNER TO commands\n"));
393 394

	printf(_("\nConnection options:\n"));
395
	printf(_("  -h, --host=HOSTNAME      database server host or socket directory\n"));
396 397 398
	printf(_("  -p, --port=PORT          database server port number\n"));
	printf(_("  -U, --username=NAME      connect as specified database user\n"));
	printf(_("  -W, --password           force password prompt (should happen automatically)\n"));
399
	printf(_("  -e, --exit-on-error      exit on error, default is to continue\n"));
400 401 402

	printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
	printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
403
}