Header And Logo

PostgreSQL
| The world's most advanced open source database.

pg_restore.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * pg_restore.c
00004  *  pg_restore is an utility extracting postgres database definitions
00005  *  from a backup archive created by pg_dump using the archiver
00006  *  interface.
00007  *
00008  *  pg_restore will read the backup archive and
00009  *  dump out a script that reproduces
00010  *  the schema of the database in terms of
00011  *        user-defined types
00012  *        user-defined functions
00013  *        tables
00014  *        indexes
00015  *        aggregates
00016  *        operators
00017  *        ACL - grant/revoke
00018  *
00019  * the output script is SQL that is understood by PostgreSQL
00020  *
00021  * Basic process in a restore operation is:
00022  *
00023  *  Open the Archive and read the TOC.
00024  *  Set flags in TOC entries, and *maybe* reorder them.
00025  *  Generate script to stdout
00026  *  Exit
00027  *
00028  * Copyright (c) 2000, Philip Warner
00029  *      Rights are granted to use this software in any way so long
00030  *      as this notice is not removed.
00031  *
00032  *  The author is not responsible for loss or damages that may
00033  *  result from its use.
00034  *
00035  *
00036  * IDENTIFICATION
00037  *      src/bin/pg_dump/pg_restore.c
00038  *
00039  *-------------------------------------------------------------------------
00040  */
00041 
00042 #include "pg_backup_archiver.h"
00043 #include "pg_backup_utils.h"
00044 #include "dumputils.h"
00045 #include "parallel.h"
00046 
00047 #include <ctype.h>
00048 
00049 #ifdef HAVE_TERMIOS_H
00050 #include <termios.h>
00051 #endif
00052 
00053 #include <unistd.h>
00054 
00055 #include "getopt_long.h"
00056 
00057 extern char *optarg;
00058 extern int  optind;
00059 
00060 #ifdef ENABLE_NLS
00061 #include <locale.h>
00062 #endif
00063 
00064 
00065 static void usage(const char *progname);
00066 
00067 typedef struct option optType;
00068 
00069 int
00070 main(int argc, char **argv)
00071 {
00072     RestoreOptions *opts;
00073     int         c;
00074     int         exit_code;
00075     int         numWorkers = 1;
00076     Archive    *AH;
00077     char       *inputFileSpec;
00078     static int  disable_triggers = 0;
00079     static int  no_data_for_failed_tables = 0;
00080     static int  outputNoTablespaces = 0;
00081     static int  use_setsessauth = 0;
00082     static int  no_security_labels = 0;
00083 
00084     struct option cmdopts[] = {
00085         {"clean", 0, NULL, 'c'},
00086         {"create", 0, NULL, 'C'},
00087         {"data-only", 0, NULL, 'a'},
00088         {"dbname", 1, NULL, 'd'},
00089         {"exit-on-error", 0, NULL, 'e'},
00090         {"file", 1, NULL, 'f'},
00091         {"format", 1, NULL, 'F'},
00092         {"function", 1, NULL, 'P'},
00093         {"host", 1, NULL, 'h'},
00094         {"ignore-version", 0, NULL, 'i'},
00095         {"index", 1, NULL, 'I'},
00096         {"jobs", 1, NULL, 'j'},
00097         {"list", 0, NULL, 'l'},
00098         {"no-privileges", 0, NULL, 'x'},
00099         {"no-acl", 0, NULL, 'x'},
00100         {"no-owner", 0, NULL, 'O'},
00101         {"no-reconnect", 0, NULL, 'R'},
00102         {"port", 1, NULL, 'p'},
00103         {"no-password", 0, NULL, 'w'},
00104         {"password", 0, NULL, 'W'},
00105         {"schema", 1, NULL, 'n'},
00106         {"schema-only", 0, NULL, 's'},
00107         {"superuser", 1, NULL, 'S'},
00108         {"table", 1, NULL, 't'},
00109         {"trigger", 1, NULL, 'T'},
00110         {"use-list", 1, NULL, 'L'},
00111         {"username", 1, NULL, 'U'},
00112         {"verbose", 0, NULL, 'v'},
00113         {"single-transaction", 0, NULL, '1'},
00114 
00115         /*
00116          * the following options don't have an equivalent short option letter
00117          */
00118         {"disable-triggers", no_argument, &disable_triggers, 1},
00119         {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
00120         {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
00121         {"role", required_argument, NULL, 2},
00122         {"section", required_argument, NULL, 3},
00123         {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
00124         {"no-security-labels", no_argument, &no_security_labels, 1},
00125 
00126         {NULL, 0, NULL, 0}
00127     };
00128 
00129     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
00130 
00131     init_parallel_dump_utils();
00132 
00133     opts = NewRestoreOptions();
00134 
00135     progname = get_progname(argv[0]);
00136 
00137     if (argc > 1)
00138     {
00139         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
00140         {
00141             usage(progname);
00142             exit_nicely(0);
00143         }
00144         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
00145         {
00146             puts("pg_restore (PostgreSQL) " PG_VERSION);
00147             exit_nicely(0);
00148         }
00149     }
00150 
00151     while ((c = getopt_long(argc, argv, "acCd:ef:F:h:iI:j:lL:n:Op:P:RsS:t:T:U:vwWx1",
00152                             cmdopts, NULL)) != -1)
00153     {
00154         switch (c)
00155         {
00156             case 'a':           /* Dump data only */
00157                 opts->dataOnly = 1;
00158                 break;
00159             case 'c':           /* clean (i.e., drop) schema prior to create */
00160                 opts->dropSchema = 1;
00161                 break;
00162             case 'C':
00163                 opts->createDB = 1;
00164                 break;
00165             case 'd':
00166                 opts->dbname = pg_strdup(optarg);
00167                 break;
00168             case 'e':
00169                 opts->exit_on_error = true;
00170                 break;
00171             case 'f':           /* output file name */
00172                 opts->filename = pg_strdup(optarg);
00173                 break;
00174             case 'F':
00175                 if (strlen(optarg) != 0)
00176                     opts->formatName = pg_strdup(optarg);
00177                 break;
00178             case 'h':
00179                 if (strlen(optarg) != 0)
00180                     opts->pghost = pg_strdup(optarg);
00181                 break;
00182             case 'i':
00183                 /* ignored, deprecated option */
00184                 break;
00185 
00186             case 'j':           /* number of restore jobs */
00187                 numWorkers = atoi(optarg);
00188                 break;
00189 
00190             case 'l':           /* Dump the TOC summary */
00191                 opts->tocSummary = 1;
00192                 break;
00193 
00194             case 'L':           /* input TOC summary file name */
00195                 opts->tocFile = pg_strdup(optarg);
00196                 break;
00197 
00198             case 'n':           /* Dump data for this schema only */
00199                 opts->schemaNames = pg_strdup(optarg);
00200                 break;
00201 
00202             case 'O':
00203                 opts->noOwner = 1;
00204                 break;
00205 
00206             case 'p':
00207                 if (strlen(optarg) != 0)
00208                     opts->pgport = pg_strdup(optarg);
00209                 break;
00210             case 'R':
00211                 /* no-op, still accepted for backwards compatibility */
00212                 break;
00213             case 'P':           /* Function */
00214                 opts->selTypes = 1;
00215                 opts->selFunction = 1;
00216                 opts->functionNames = pg_strdup(optarg);
00217                 break;
00218             case 'I':           /* Index */
00219                 opts->selTypes = 1;
00220                 opts->selIndex = 1;
00221                 opts->indexNames = pg_strdup(optarg);
00222                 break;
00223             case 'T':           /* Trigger */
00224                 opts->selTypes = 1;
00225                 opts->selTrigger = 1;
00226                 opts->triggerNames = pg_strdup(optarg);
00227                 break;
00228             case 's':           /* dump schema only */
00229                 opts->schemaOnly = 1;
00230                 break;
00231             case 'S':           /* Superuser username */
00232                 if (strlen(optarg) != 0)
00233                     opts->superuser = pg_strdup(optarg);
00234                 break;
00235             case 't':           /* Dump data for this table only */
00236                 opts->selTypes = 1;
00237                 opts->selTable = 1;
00238                 simple_string_list_append(&opts->tableNames, optarg);
00239                 break;
00240 
00241             case 'U':
00242                 opts->username = pg_strdup(optarg);
00243                 break;
00244 
00245             case 'v':           /* verbose */
00246                 opts->verbose = 1;
00247                 break;
00248 
00249             case 'w':
00250                 opts->promptPassword = TRI_NO;
00251                 break;
00252 
00253             case 'W':
00254                 opts->promptPassword = TRI_YES;
00255                 break;
00256 
00257             case 'x':           /* skip ACL dump */
00258                 opts->aclsSkip = 1;
00259                 break;
00260 
00261             case '1':           /* Restore data in a single transaction */
00262                 opts->single_txn = true;
00263                 opts->exit_on_error = true;
00264                 break;
00265 
00266             case 0:
00267 
00268                 /*
00269                  * This covers the long options without a short equivalent.
00270                  */
00271                 break;
00272 
00273             case 2:             /* SET ROLE */
00274                 opts->use_role = pg_strdup(optarg);
00275                 break;
00276 
00277             case 3:             /* section */
00278                 set_dump_section(optarg, &(opts->dumpSections));
00279                 break;
00280 
00281             default:
00282                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
00283                 exit_nicely(1);
00284         }
00285     }
00286 
00287     /* Get file name from command line */
00288     if (optind < argc)
00289         inputFileSpec = argv[optind++];
00290     else
00291         inputFileSpec = NULL;
00292 
00293     /* Complain if any arguments remain */
00294     if (optind < argc)
00295     {
00296         fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
00297                 progname, argv[optind]);
00298         fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
00299                 progname);
00300         exit_nicely(1);
00301     }
00302 
00303     /* Should get at most one of -d and -f, else user is confused */
00304     if (opts->dbname)
00305     {
00306         if (opts->filename)
00307         {
00308             fprintf(stderr, _("%s: options -d/--dbname and -f/--file cannot be used together\n"),
00309                     progname);
00310             fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
00311                     progname);
00312             exit_nicely(1);
00313         }
00314         opts->useDB = 1;
00315     }
00316 
00317     /* Can't do single-txn mode with multiple connections */
00318     if (opts->single_txn && numWorkers > 1)
00319     {
00320         fprintf(stderr, _("%s: cannot specify both --single-transaction and multiple jobs\n"),
00321                 progname);
00322         exit_nicely(1);
00323     }
00324 
00325     opts->disable_triggers = disable_triggers;
00326     opts->noDataForFailedTables = no_data_for_failed_tables;
00327     opts->noTablespace = outputNoTablespaces;
00328     opts->use_setsessauth = use_setsessauth;
00329     opts->no_security_labels = no_security_labels;
00330 
00331     if (opts->formatName)
00332     {
00333         switch (opts->formatName[0])
00334         {
00335             case 'c':
00336             case 'C':
00337                 opts->format = archCustom;
00338                 break;
00339 
00340             case 'd':
00341             case 'D':
00342                 opts->format = archDirectory;
00343                 break;
00344 
00345             case 't':
00346             case 'T':
00347                 opts->format = archTar;
00348                 break;
00349 
00350             default:
00351                 write_msg(NULL, "unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"\n",
00352                           opts->formatName);
00353                 exit_nicely(1);
00354         }
00355     }
00356 
00357     AH = OpenArchive(inputFileSpec, opts->format);
00358 
00359     /*
00360      * We don't have a connection yet but that doesn't matter. The connection
00361      * is initialized to NULL and if we terminate through exit_nicely() while
00362      * it's still NULL, the cleanup function will just be a no-op.
00363      */
00364     on_exit_close_archive(AH);
00365 
00366     /* Let the archiver know how noisy to be */
00367     AH->verbose = opts->verbose;
00368 
00369     /*
00370      * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
00371      */
00372     AH->exit_on_error = opts->exit_on_error;
00373 
00374     if (opts->tocFile)
00375         SortTocFromFile(AH, opts);
00376 
00377     /* See comments in pg_dump.c */
00378 #ifdef WIN32
00379     if (numWorkers > MAXIMUM_WAIT_OBJECTS)
00380     {
00381         fprintf(stderr, _("%s: maximum number of parallel jobs is %d\n"),
00382                 progname, MAXIMUM_WAIT_OBJECTS);
00383         exit(1);
00384     }
00385 #endif
00386 
00387     AH->numWorkers = numWorkers;
00388 
00389     if (opts->tocSummary)
00390         PrintTOCSummary(AH, opts);
00391     else
00392     {
00393         SetArchiveRestoreOptions(AH, opts);
00394         RestoreArchive(AH);
00395     }
00396 
00397     /* done, print a summary of ignored errors */
00398     if (AH->n_errors)
00399         fprintf(stderr, _("WARNING: errors ignored on restore: %d\n"),
00400                 AH->n_errors);
00401 
00402     /* AH may be freed in CloseArchive? */
00403     exit_code = AH->n_errors ? 1 : 0;
00404 
00405     CloseArchive(AH);
00406 
00407     return exit_code;
00408 }
00409 
00410 static void
00411 usage(const char *progname)
00412 {
00413     printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname);
00414     printf(_("Usage:\n"));
00415     printf(_("  %s [OPTION]... [FILE]\n"), progname);
00416 
00417     printf(_("\nGeneral options:\n"));
00418     printf(_("  -d, --dbname=NAME        connect to database name\n"));
00419     printf(_("  -f, --file=FILENAME      output file name\n"));
00420     printf(_("  -F, --format=c|d|t       backup file format (should be automatic)\n"));
00421     printf(_("  -l, --list               print summarized TOC of the archive\n"));
00422     printf(_("  -v, --verbose            verbose mode\n"));
00423     printf(_("  -V, --version            output version information, then exit\n"));
00424     printf(_("  -?, --help               show this help, then exit\n"));
00425 
00426     printf(_("\nOptions controlling the restore:\n"));
00427     printf(_("  -a, --data-only              restore only the data, no schema\n"));
00428     printf(_("  -c, --clean                  clean (drop) database objects before recreating\n"));
00429     printf(_("  -C, --create                 create the target database\n"));
00430     printf(_("  -e, --exit-on-error          exit on error, default is to continue\n"));
00431     printf(_("  -I, --index=NAME             restore named index\n"));
00432     printf(_("  -j, --jobs=NUM               use this many parallel jobs to restore\n"));
00433     printf(_("  -L, --use-list=FILENAME      use table of contents from this file for\n"
00434              "                               selecting/ordering output\n"));
00435     printf(_("  -n, --schema=NAME            restore only objects in this schema\n"));
00436     printf(_("  -O, --no-owner               skip restoration of object ownership\n"));
00437     printf(_("  -P, --function=NAME(args)    restore named function\n"));
00438     printf(_("  -s, --schema-only            restore only the schema, no data\n"));
00439     printf(_("  -S, --superuser=NAME         superuser user name to use for disabling triggers\n"));
00440     printf(_("  -t, --table=NAME             restore named table(s)\n"));
00441     printf(_("  -T, --trigger=NAME           restore named trigger\n"));
00442     printf(_("  -x, --no-privileges          skip restoration of access privileges (grant/revoke)\n"));
00443     printf(_("  -1, --single-transaction     restore as a single transaction\n"));
00444     printf(_("  --disable-triggers           disable triggers during data-only restore\n"));
00445     printf(_("  --no-data-for-failed-tables  do not restore data of tables that could not be\n"
00446              "                               created\n"));
00447     printf(_("  --no-security-labels         do not restore security labels\n"));
00448     printf(_("  --no-tablespaces             do not restore tablespace assignments\n"));
00449     printf(_("  --section=SECTION            restore named section (pre-data, data, or post-data)\n"));
00450     printf(_("  --use-set-session-authorization\n"
00451              "                               use SET SESSION AUTHORIZATION commands instead of\n"
00452              "                               ALTER OWNER commands to set ownership\n"));
00453 
00454     printf(_("\nConnection options:\n"));
00455     printf(_("  -h, --host=HOSTNAME      database server host or socket directory\n"));
00456     printf(_("  -p, --port=PORT          database server port number\n"));
00457     printf(_("  -U, --username=NAME      connect as specified database user\n"));
00458     printf(_("  -w, --no-password        never prompt for password\n"));
00459     printf(_("  -W, --password           force password prompt (should happen automatically)\n"));
00460     printf(_("  --role=ROLENAME          do SET ROLE before restore\n"));
00461 
00462     printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
00463     printf(_("Report bugs to <[email protected]>.\n"));
00464 }