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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
/*-------------------------------------------------------------------------
*
* 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
* indexes
* 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 its use.
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_restore.c,v 1.43 2002/10/18 22:05:36 petere Exp $
*
*-------------------------------------------------------------------------
*/
#include "pg_backup.h"
#include "pg_backup_archiver.h"
#include "dumputils.h"
#include <ctype.h>
#ifndef HAVE_STRDUP
#include "strdup.h"
#endif
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif
#include <unistd.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#ifdef ENABLE_NLS
#include <locale.h>
#endif
#define _(x) gettext((x))
/* Forward decls */
static void usage(const char *progname);
static char *_cleanupName(char *name);
static char *_cleanupFuncName(char *name);
typedef struct option optType;
int
main(int argc, char **argv)
{
RestoreOptions *opts;
int c;
Archive *AH;
char *fileSpec = NULL;
extern int optind;
extern char *optarg;
static int use_setsessauth = 0;
static int disable_triggers = 0;
#ifdef HAVE_GETOPT_LONG
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", 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'},
{"oid-order", 0, NULL, 'o'},
{"orig-order", 0, NULL, 'N'},
{"password", 0, NULL, 'W'},
{"rearrange", 0, NULL, 'r'},
{"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'},
/*
* the following options don't have an equivalent short option
* letter, but are available as '-X long-name'
*/
{"use-set-session-authorization", no_argument, &use_setsessauth, 1},
{"disable-triggers", no_argument, &disable_triggers, 1},
{NULL, 0, NULL, 0}
};
#endif /* HAVE_GETOPT_LONG */
#ifdef ENABLE_NLS
setlocale(LC_ALL, "");
bindtextdomain("pg_dump", LOCALEDIR);
textdomain("pg_dump");
#endif
opts = NewRestoreOptions();
if (!strrchr(argv[0], '/'))
progname = argv[0];
else
progname = strrchr(argv[0], '/') + 1;
if (argc > 1)
{
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
{
usage(progname);
exit(0);
}
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
{
puts("pg_restore (PostgreSQL) " PG_VERSION);
exit(0);
}
}
#ifdef HAVE_GETOPT_LONG
while ((c = getopt_long(argc, argv, "acCd:f:F:h:iI:lL:NoOp:P:rRsS:t:T:uU:vWxX:", cmdopts, NULL)) != -1)
#else
while ((c = getopt(argc, argv, "acCd:f:F:h:iI:lL:NoOp:P:rRsS:t:T:uU:vWxX:")) != -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 'l': /* Dump the TOC summary */
opts->tocSummary = 1;
break;
case 'L': /* input TOC summary file name */
opts->tocFile = strdup(optarg);
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 = _cleanupFuncName(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 'u':
opts->requirePassword = true;
opts->username = simple_prompt("User name: ", 100, true);
break;
case 'U':
opts->username = optarg;
break;
case 'v': /* verbose */
opts->verbose = 1;
break;
case 'W':
opts->requirePassword = true;
break;
case 'x': /* skip ACL dump */
opts->aclsSkip = 1;
break;
case 'X':
if (strcmp(optarg, "use-set-session-authorization") == 0)
use_setsessauth = 1;
else if (strcmp(optarg, "disable-triggers") == 0)
disable_triggers = 1;
else
{
fprintf(stderr,
_("%s: invalid -X option -- %s\n"),
progname, optarg);
fprintf(stderr, _("Try '%s --help' for more information.\n"), progname);
exit(1);
}
break;
#ifdef HAVE_GETOPT_LONG
/* This covers the long options equivalent to -X xxx. */
case 0:
break;
#endif
default:
fprintf(stderr, _("Try '%s --help' for more information.\n"), progname);
exit(1);
}
}
if (optind < argc)
fileSpec = argv[optind];
else
fileSpec = NULL;
opts->use_setsessauth = use_setsessauth;
opts->disable_triggers = disable_triggers;
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:
write_msg("unrecognized archive format '%s'; please specify 't' or 'c'\n",
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, "SEQUENCE SET");
}
/* Database MUST be at start */
MoveToStart(AH, "DATABASE");
if (opts->tocSummary)
PrintTOCSummary(AH, opts);
else
RestoreArchive(AH, opts);
CloseArchive(AH);
return 0;
}
static void
usage(const char *progname)
{
printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname);
printf(_("Usage:\n"));
printf(_(" %s [OPTION]... [FILE]\n"), progname);
printf(_("\nGeneral options:\n"));
#ifdef HAVE_GETOPT_LONG
printf(_(" -d, --dbname=NAME output database name\n"));
printf(_(" -f, --file=FILENAME output file name\n"));
printf(_(" -F, --format=c|t specify backup file format\n"));
printf(_(" -i, --ignore-version proceed even when server version mismatches\n"));
printf(_(" -l, --list print summarized TOC of the archive\n"));
printf(_(" -v, --verbose verbose mode\n"));
#else /* not HAVE_GETOPT_LONG */
printf(_(" -d NAME output database name\n"));
printf(_(" -f FILENAME output file name\n"));
printf(_(" -F c|t specify backup file format\n"));
printf(_(" -i proceed even when server version mismatches\n"));
printf(_(" -l print summarized TOC of the archive\n"));
printf(_(" -v verbose mode\n"));
#endif /* not HAVE_GETOPT_LONG */
printf(_(" --help show this help, then exit\n"));
printf(_(" --version output version information, then exit\n"));
printf(_("\nOptions controlling the output content:\n"));
#ifdef HAVE_GETOPT_LONG
printf(_(" -a, --data-only restore only the data, no schema\n"));
printf(_(" -c, --clean clean (drop) schema prior to create\n"));
printf(_(" -C, --create issue commands to create the database\n"));
printf(_(" -I, --index=NAME restore named index\n"));
printf(_(" -L, --use-list=FILENAME use specified table of contents for ordering\n"
" output from this file\n"));
printf(_(" -N, --orig-order restore in original dump order\n"));
printf(_(" -o, --oid-order restore in OID order\n"));
printf(_(" -O, --no-owner do not reconnect to database to match\n"
" object owner\n"));
printf(_(" -P, --function=NAME(args)\n"
" restore named function\n"));
printf(_(" -r, --rearrange rearrange output to put indexes etc. at end\n"));
printf(_(" -R, --no-reconnect disallow ALL reconnections to the database\n"));
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 use-set-session-authorization, --use-set-session-authorization\n"
" use SET SESSION AUTHORIZATION commands instead\n"
" of reconnecting, if possible\n"));
printf(_(" -X disable-triggers, --disable-triggers\n"
" disable triggers during data-only restore\n"));
#else /* not HAVE_GETOPT_LONG */
printf(_(" -a restore only the data, no schema\n"));
printf(_(" -c clean (drop) schema prior to create\n"));
printf(_(" -C issue commands to create the database\n"));
printf(_(" -I NAME restore named index\n"));
printf(_(" -L FILENAME use specified table of contents for ordering\n"
" output from this file\n"));
printf(_(" -N restore in original dump order\n"));
printf(_(" -o restore in OID order\n"));
printf(_(" -O do not reconnect to database to match\n"
" object owner\n"));
printf(_(" -P NAME(args) restore named function\n"));
printf(_(" -r rearrange output to put indexes etc. at end\n"));
printf(_(" -R disallow ALL reconnections to the database\n"));
printf(_(" -s restore only the schema, no data\n"));
printf(_(" -S NAME specify the superuser user name to use for\n"
" disabling triggers\n"));
printf(_(" -t NAME restore named table\n"));
printf(_(" -T NAME restore named trigger\n"));
printf(_(" -x skip restoration of access privileges (grant/revoke)\n"));
printf(_(" -X use-set-session-authorization\n"
" use SET SESSION AUTHORIZATION commands instead\n"
" of reconnecting, if possible\n"));
printf(_(" -X disable-triggers disable triggers during data-only restore\n"));
#endif /* not HAVE_GETOPT_LONG */
printf(_("\nConnection options:\n"));
#ifdef HAVE_GETOPT_LONG
printf(_(" -h, --host=HOSTNAME database server host name\n"));
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"));
#else /* not HAVE_GETOPT_LONG */
printf(_(" -h HOSTNAME database server host name\n"));
printf(_(" -p PORT database server port number\n"));
printf(_(" -U NAME connect as specified database user\n"));
printf(_(" -W force password prompt (should happen automatically)\n"));
#endif /* not HAVE_GETOPT_LONG */
printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
}
static char *
_cleanupName(char *name)
{
int i;
if (!name || !name[0])
return NULL;
name = strdup(name);
if (name[0] == '"')
{
strcpy(name, &name[1]);
if (name[0] && *(name + strlen(name) - 1) == '"')
*(name + strlen(name) - 1) = '\0';
}
/* otherwise, convert table name to lowercase... */
else
{
for (i = 0; name[i]; i++)
if (isupper((unsigned char) name[i]))
name[i] = tolower((unsigned char) name[i]);
}
return name;
}
static char *
_cleanupFuncName(char *name)
{
int i;
char *ch;
if (!name || !name[0])
return NULL;
name = strdup(name);
if (name[0] == '"')
{
strcpy(name, &name[1]);
if (strchr(name, '"') != NULL)
strcpy(strchr(name, '"'), strchr(name, '"') + 1);
}
/* otherwise, convert function name to lowercase... */
else
{
for (i = 0; name[i]; i++)
if (isupper((unsigned char) name[i]))
name[i] = tolower((unsigned char) name[i]);
}
/* strip out any space before paren */
ch = strchr(name, '(');
while (ch && ch > name && *(ch - 1) == ' ')
{
strcpy(ch - 1, ch);
ch--;
}
/*
* Strip out spaces after commas in parameter list. We can't remove
* all spaces because some types, like 'double precision' have spaces.
*/
if ((ch = strchr(name, '(')) != NULL)
{
while ((ch = strstr(ch, ", ")) != NULL)
strcpy(ch + 1, ch + 2);
}
return name;
}
|