aboutsummaryrefslogtreecommitdiff
path: root/src/bin/pg_dump/pg_restore.c
blob: dd10ff54808d4bc241d6a4397a215c9c06495f26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*-------------------------------------------------------------------------
 *
 * pg_restore.c
 *	pg_restore is an utility extracting postgres database definitions
 *	from a backup archive created by pg_dump using the archiver 
 *	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
 *		  indices
 *		  aggregates
 *		  operators
 *		  ACL - grant/revoke
 *
 * the output script is SQL that is understood by PostgreSQL
 *
 * Basic process in a restore operation is:
 * 
 * 	Open the Archive and read the TOC.
 * 	Set flags in TOC entries, and *maybe* reorder them.
 * 	Generate script to stdout
 * 	Exit
 *
 * Copyright (c) 2000, Philip Warner
 *      Rights are granted to use this software in any way so long
 *      as this notice is not removed.
 *
 *	The author is not responsible for loss or damages that may
 *	result from it's use.
 *
 *
 * IDENTIFICATION
 *
 * Modifications - 28-Jun-2000 - pjw@rhyme.com.au
 *
 *		Initial version. Command processing taken from original pg_dump.
 *
 * Modifications - 28-Jul-2000 - pjw@rhyme.com.au (1.45)
 *
 * 		Added --create, --no-owner, --superuser, --no-reconnect (pg_dump & pg_restore)
 *		Added code to dump 'Create Schema' statement (pg_dump)
 *		Don't bother to disable/enable triggers if we don't have a superuser (pg_restore)
 *		Cleaned up code for reconnecting to database.
 *		Force a reconnect as superuser before enabling/disabling triggers.
 *
 *-------------------------------------------------------------------------
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>


/*
#include "postgres.h"
#include "access/htup.h"
#include "catalog/pg_type.h"
#include "catalog/pg_language.h"
#include "catalog/pg_index.h"
#include "catalog/pg_trigger.h"
#include "libpq-fe.h"
*/

#include "pg_backup.h"

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

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

#ifdef HAVE_GETOPT_H 
#include <getopt.h>
#else
#include <unistd.h>
#endif

/* Forward decls */
static void usage(const char *progname);
static char* _cleanupName(char* name);

typedef struct option optType;

#ifdef HAVE_GETOPT_H
struct option cmdopts[] = {	
				{ "clean", 0, NULL, 'c' },
				{ "create", 0, NULL, 'C' },
				{ "data-only", 0, NULL, 'a' },
				{ "dbname", 1, NULL, 'd' },
				{ "file", 1, NULL, 'f' },
				{ "format", 1, NULL, 'F' },
				{ "function", 2, NULL, 'P' },
				{ "host", 1, NULL, 'h' },
				{ "ignore-version", 0, NULL, 'i'},
				{ "index", 2, NULL, 'I'},
				{ "list", 0, NULL, 'l'},
				{ "no-acl", 0, NULL, 'x' },
				{ "no-owner", 0, NULL, 'O'},
				{ "no-reconnect", 0, NULL, 'R' },
				{ "port", 1, NULL, 'p' },
				{ "oid-order", 0, NULL, 'o'},
				{ "orig-order", 0, NULL, 'N'},
				{ "password", 0, NULL, 'u' },
				{ "rearrange", 0, NULL, 'r'},
				{ "schema-only", 0, NULL, 's' },
				{ "superuser", 1, NULL, 'S' },
				{ "table", 2, NULL, 't'},
				{ "trigger", 2, NULL, 'T' },
				{ "use-list", 1, NULL, 'U'},
				{ "verbose", 0, NULL, 'v' },
				{ NULL, 0, NULL, 0}
			    };
#endif

int main(int argc, char **argv)
{
	RestoreOptions	*opts;
	char		*progname;
	int		c;
	Archive*    	AH;
	char		*fileSpec = NULL;

	opts = NewRestoreOptions();

	progname = *argv;

#ifdef HAVE_GETOPT_LONG
	while ((c = getopt_long(argc, argv, "acCd:f:F:h:i:lNoOp:rRsS:t:T:uU:vx", cmdopts, NULL)) != EOF)
#else
	while ((c = getopt(argc, argv, "acCd:f:F:h:i:lNoOp:rRsS:t:T:uU:vx")) != -1)
#endif
	{
		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;
			case 'C':
				opts->create = 1;
				break;
			case 'd':
				if (strlen(optarg) != 0)
				{
					opts->dbname = strdup(optarg);
					opts->useDB = 1;
				}
				break;
			case 'f':			/* output file name */
				opts->filename = strdup(optarg);
				break;
			case 'F':
				if (strlen(optarg) != 0) 
				    opts->formatName = strdup(optarg);
				break;
			case 'h':
				if (strlen(optarg) != 0)
					opts->pghost = strdup(optarg);
				break;
			case 'i':
				opts->ignoreVersion = 1;
				break;
			case 'N':
				opts->origOrder = 1;
				break;
			case 'o':
				opts->oidOrder = 1;
				break;
			case 'O':
				opts->noOwner = 1;
				break;
			case 'p':
				if (strlen(optarg) != 0)
					opts->pgport = strdup(optarg);
				break;
			case 'r':
				opts->rearrange = 1;
				break;
			case 'R':
				opts->noReconnect = 1;
				break;
			case 'P': /* Function */
				opts->selTypes = 1;
				opts->selFunction = 1;
				opts->functionNames = _cleanupName(optarg);
				break;
			case 'I': /* Index */
				opts->selTypes = 1;
				opts->selIndex = 1;
				opts->indexNames = _cleanupName(optarg);
				break;
			case 'T': /* Trigger */
				opts->selTypes = 1;
				opts->selTrigger = 1;
				opts->triggerNames = _cleanupName(optarg);
				break;
			case 's':			/* dump schema only */
				opts->schemaOnly = 1;
				break;
			case 'S':			/* Superuser username */
				if (strlen(optarg) != 0)
					opts->superuser = strdup(optarg);
				break;
			case 't':			/* Dump data for this table only */
				opts->selTypes = 1;
				opts->selTable = 1;
				opts->tableNames = _cleanupName(optarg);
				break;
			case 'l':			/* Dump the TOC summary */
				opts->tocSummary = 1;
				break;

			case 'u':
				opts->requirePassword = 1;
				break;

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

			case 'v':			/* verbose */
				opts->verbose = 1;
				break;
			case 'x':			/* skip ACL dump */
				opts->aclsSkip = 1;
				break;
			default:
				usage(progname);
				break;
		}
	}

	if (optind < argc) {
	    fileSpec = argv[optind];
	} else {
	    fileSpec = NULL;
	}

    if (opts->formatName) { 

	switch (opts->formatName[0]) {

	    case 'c':
	    case 'C':
			opts->format = archCustom;
			break;

	    case 'f':
	    case 'F':
			opts->format = archFiles;
			break;

		case 't':
		case 'T':
			opts->format = archTar;
			break;

	    default:
			fprintf(stderr, "%s: Unknown archive format '%s', please specify 't' or 'c'\n",
						progname, opts->formatName);
			exit (1);
	}
    }

    AH = OpenArchive(fileSpec, opts->format);

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

    if (opts->tocFile)
		SortTocFromFile(AH, opts);

    if (opts->oidOrder)
		SortTocByOID(AH);
    else if (opts->origOrder)
		SortTocByID(AH);

    if (opts->rearrange) {
		MoveToStart(AH, "<Init>");
		MoveToEnd(AH, "TABLE DATA");
		MoveToEnd(AH, "BLOBS");
		MoveToEnd(AH, "INDEX");
		MoveToEnd(AH, "TRIGGER");
		MoveToEnd(AH, "RULE");
		MoveToEnd(AH, "ACL");
    }

	/* Database MUST be at start */
	MoveToStart(AH, "DATABASE");

    if (opts->tocSummary) {
		PrintTOCSummary(AH, opts);
    } else {
		RestoreArchive(AH, opts);
    }

    CloseArchive(AH);

    return 1;
}

static void usage(const char *progname)
{
#ifdef HAVE_GETOPT_LONG
	fprintf(stderr,
	"usage:  %s [options] [backup file]\n"
	    "  -a, --data-only             \t dump out only the data, no schema\n"
		"  -d, --dbname <name>         \t specify database name\n"
	    "  -c, --clean                 \t clean(drop) schema prior to create\n"
		"  -C, --create                \t output commands to create the database\n"
	    "  -f filename                 \t script output filename\n"
	    "  -F, --format {c|f}          \t specify backup file format\n"
		"  -h, --host <hostname>       \t server host name\n"
	    "  -i, --index[=name]          \t dump indexes or named index\n"
	    "  -l, --list                  \t dump summarized TOC for this file\n"
		"  -N, --orig-order            \t dump in original dump order\n"
	    "  -o, --oid-order             \t dump in oid order\n"
		"  -O, --no-owner              \t don't output reconnect to database to match object owner\n"
		"  -p, --port <port>           \t server port number\n"
		"  -P, --function[=name]       \t dump functions or named function\n"
	    "  -r, --rearrange             \t rearrange output to put indexes etc at end\n"
		"  -R, --no-reconnect          \t disallow ALL reconnections to the database\n"
	    "  -s, --schema-only           \t dump out only the schema, no data\n"
		"  -S, --superuser <name>      \t specify the superuser username to use in disabling triggers\n"
	    "  -t [table], --table[=table] \t dump for this table only\n"
	    "  -T, --trigger[=name]        \t dump triggers or named trigger\n"
		"  -u, --password              \t use password authentication\n"
	    "  -U, --use-list filename     \t use specified TOC for ordering output from this file\n"
	    "  -v, --verbose               \t verbose\n"
	    "  -x, --no-acl                \t skip dumping of ACLs (grant/revoke)\n"
	    , progname);

#else
	fprintf(stderr,
	"usage:  %s [options] [backup file]\n"
	    "  -a                          \t dump out only the data, no schema\n"
		"  -d <name>                   \t specify database name\n"
	    "  -c                          \t clean(drop) schema prior to create\n"
		"  -C                          \t output commands to create the database\n"
	    "  -f filename                 \t script output filename\n"
	    "  -F {c|f}                    \t specify backup file format\n"
		"  -h <hostname>               \t server host name\n"
	    "  -i name                     \t dump indexes or named index\n"
	    "  -l                          \t dump summarized TOC for this file\n"
		"  -N                          \t dump in original dump order\n"
	    "  -o                          \t dump in oid order\n"
		"  -O                          \t don't output reconnect to database to match object owner\n"
		"  -p <port>                   \t server port number\n"
		"  -P name                     \t dump functions or named function\n"
	    "  -r                          \t rearrange output to put indexes etc at end\n"
		"  -R                          \t disallow ALL reconnections to the database\n"
	    "  -s                          \t dump out only the schema, no data\n"
		"  -S <name>                   \t specify the superuser username to use in disabling triggers\n"
	    "  -t name                     \t dump for this table only\n"
	    "  -T name                     \t dump triggers or named trigger\n"
		"  -u                          \t use password authentication\n"
	    "  -U filename                 \t use specified TOC for ordering output from this file\n"
	    "  -v                          \t verbose\n"
	    "  -x                          \t skip dumping of ACLs (grant/revoke)\n"
	    , progname);
#endif
	fprintf(stderr,
			"\nIf [backup file] is not supplied, then standard input "
			"is used.\n");
	fprintf(stderr, "\n");

	exit(1);
}

static char* _cleanupName(char* name)
{
    int		i;

    if (!name)
	return NULL;

    if (strlen(name) == 0)
	return NULL;

    name = strdup(name);

    if (name[0] == '"')
    {
	strcpy(name, &name[1]);
	if (*(name + strlen(name) - 1) == '"')
	    *(name + strlen(name) - 1) = '\0';
    }
    /* otherwise, convert table name to lowercase... */
    else
    {
	for (i = 0; name[i]; i++)
	    if (isascii((unsigned char) name[i]) && isupper(name[i]))
		name[i] = tolower(name[i]);
    }
    return name;
}