LCOV - code coverage report
Current view: top level - src/bin/pg_dump - pg_restore.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 196 255 76.9 %
Date: 2021-12-09 04:09:06 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       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.
       7             :  *
       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
      18             :  *
      19             :  * the output script is SQL that is understood by PostgreSQL
      20             :  *
      21             :  * Basic process in a restore operation is:
      22             :  *
      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
      27             :  *
      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.
      31             :  *
      32             :  *  The author is not responsible for loss or damages that may
      33             :  *  result from its use.
      34             :  *
      35             :  *
      36             :  * IDENTIFICATION
      37             :  *      src/bin/pg_dump/pg_restore.c
      38             :  *
      39             :  *-------------------------------------------------------------------------
      40             :  */
      41             : #include "postgres_fe.h"
      42             : 
      43             : #include <ctype.h>
      44             : #ifdef HAVE_TERMIOS_H
      45             : #include <termios.h>
      46             : #endif
      47             : 
      48             : #include "dumputils.h"
      49             : #include "fe_utils/option_utils.h"
      50             : #include "getopt_long.h"
      51             : #include "parallel.h"
      52             : #include "pg_backup_utils.h"
      53             : 
      54             : static void usage(const char *progname);
      55             : 
      56             : int
      57          64 : main(int argc, char **argv)
      58             : {
      59             :     RestoreOptions *opts;
      60             :     int         c;
      61             :     int         exit_code;
      62          64 :     int         numWorkers = 1;
      63             :     Archive    *AH;
      64             :     char       *inputFileSpec;
      65             :     static int  disable_triggers = 0;
      66             :     static int  enable_row_security = 0;
      67             :     static int  if_exists = 0;
      68             :     static int  no_data_for_failed_tables = 0;
      69             :     static int  outputNoTablespaces = 0;
      70             :     static int  use_setsessauth = 0;
      71             :     static int  no_comments = 0;
      72             :     static int  no_publications = 0;
      73             :     static int  no_security_labels = 0;
      74             :     static int  no_subscriptions = 0;
      75             :     static int  strict_names = 0;
      76             : 
      77          64 :     struct option cmdopts[] = {
      78             :         {"clean", 0, NULL, 'c'},
      79             :         {"create", 0, NULL, 'C'},
      80             :         {"data-only", 0, NULL, 'a'},
      81             :         {"dbname", 1, NULL, 'd'},
      82             :         {"exit-on-error", 0, NULL, 'e'},
      83             :         {"exclude-schema", 1, NULL, 'N'},
      84             :         {"file", 1, NULL, 'f'},
      85             :         {"format", 1, NULL, 'F'},
      86             :         {"function", 1, NULL, 'P'},
      87             :         {"host", 1, NULL, 'h'},
      88             :         {"index", 1, NULL, 'I'},
      89             :         {"jobs", 1, NULL, 'j'},
      90             :         {"list", 0, NULL, 'l'},
      91             :         {"no-privileges", 0, NULL, 'x'},
      92             :         {"no-acl", 0, NULL, 'x'},
      93             :         {"no-owner", 0, NULL, 'O'},
      94             :         {"no-reconnect", 0, NULL, 'R'},
      95             :         {"port", 1, NULL, 'p'},
      96             :         {"no-password", 0, NULL, 'w'},
      97             :         {"password", 0, NULL, 'W'},
      98             :         {"schema", 1, NULL, 'n'},
      99             :         {"schema-only", 0, NULL, 's'},
     100             :         {"superuser", 1, NULL, 'S'},
     101             :         {"table", 1, NULL, 't'},
     102             :         {"trigger", 1, NULL, 'T'},
     103             :         {"use-list", 1, NULL, 'L'},
     104             :         {"username", 1, NULL, 'U'},
     105             :         {"verbose", 0, NULL, 'v'},
     106             :         {"single-transaction", 0, NULL, '1'},
     107             : 
     108             :         /*
     109             :          * the following options don't have an equivalent short option letter
     110             :          */
     111             :         {"disable-triggers", no_argument, &disable_triggers, 1},
     112             :         {"enable-row-security", no_argument, &enable_row_security, 1},
     113             :         {"if-exists", no_argument, &if_exists, 1},
     114             :         {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
     115             :         {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
     116             :         {"role", required_argument, NULL, 2},
     117             :         {"section", required_argument, NULL, 3},
     118             :         {"strict-names", no_argument, &strict_names, 1},
     119             :         {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
     120             :         {"no-comments", no_argument, &no_comments, 1},
     121             :         {"no-publications", no_argument, &no_publications, 1},
     122             :         {"no-security-labels", no_argument, &no_security_labels, 1},
     123             :         {"no-subscriptions", no_argument, &no_subscriptions, 1},
     124             : 
     125             :         {NULL, 0, NULL, 0}
     126             :     };
     127             : 
     128          64 :     pg_logging_init(argv[0]);
     129          64 :     pg_logging_set_level(PG_LOG_WARNING);
     130          64 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
     131             : 
     132          64 :     init_parallel_dump_utils();
     133             : 
     134          64 :     opts = NewRestoreOptions();
     135             : 
     136          64 :     progname = get_progname(argv[0]);
     137             : 
     138          64 :     if (argc > 1)
     139             :     {
     140          62 :         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
     141             :         {
     142           2 :             usage(progname);
     143           2 :             exit_nicely(0);
     144             :         }
     145          60 :         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
     146             :         {
     147           4 :             puts("pg_restore (PostgreSQL) " PG_VERSION);
     148           4 :             exit_nicely(0);
     149             :         }
     150             :     }
     151             : 
     152         230 :     while ((c = getopt_long(argc, argv, "acCd:ef:F:h:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1",
     153             :                             cmdopts, NULL)) != -1)
     154             :     {
     155         176 :         switch (c)
     156             :         {
     157           4 :             case 'a':           /* Dump data only */
     158           4 :                 opts->dataOnly = 1;
     159           4 :                 break;
     160           6 :             case 'c':           /* clean (i.e., drop) schema prior to create */
     161           6 :                 opts->dropSchema = 1;
     162           6 :                 break;
     163          16 :             case 'C':
     164          16 :                 opts->createDB = 1;
     165          16 :                 break;
     166          18 :             case 'd':
     167          18 :                 opts->cparams.dbname = pg_strdup(optarg);
     168          18 :                 break;
     169          12 :             case 'e':
     170          12 :                 opts->exit_on_error = true;
     171          12 :                 break;
     172          34 :             case 'f':           /* output file name */
     173          34 :                 opts->filename = pg_strdup(optarg);
     174          34 :                 break;
     175          10 :             case 'F':
     176          10 :                 if (strlen(optarg) != 0)
     177          10 :                     opts->formatName = pg_strdup(optarg);
     178          10 :                 break;
     179          12 :             case 'h':
     180          12 :                 if (strlen(optarg) != 0)
     181          12 :                     opts->cparams.pghost = pg_strdup(optarg);
     182          12 :                 break;
     183             : 
     184           8 :             case 'j':           /* number of restore jobs */
     185           8 :                 if (!option_parse_int(optarg, "-j/--jobs", 1,
     186             :                                       PG_MAX_JOBS,
     187             :                                       &numWorkers))
     188           2 :                     exit(1);
     189           6 :                 break;
     190             : 
     191           0 :             case 'l':           /* Dump the TOC summary */
     192           0 :                 opts->tocSummary = 1;
     193           0 :                 break;
     194             : 
     195           0 :             case 'L':           /* input TOC summary file name */
     196           0 :                 opts->tocFile = pg_strdup(optarg);
     197           0 :                 break;
     198             : 
     199           0 :             case 'n':           /* Dump data for this schema only */
     200           0 :                 simple_string_list_append(&opts->schemaNames, optarg);
     201           0 :                 break;
     202             : 
     203           0 :             case 'N':           /* Do not dump data for this schema */
     204           0 :                 simple_string_list_append(&opts->schemaExcludeNames, optarg);
     205           0 :                 break;
     206             : 
     207           0 :             case 'O':
     208           0 :                 opts->noOwner = 1;
     209           0 :                 break;
     210             : 
     211          12 :             case 'p':
     212          12 :                 if (strlen(optarg) != 0)
     213          12 :                     opts->cparams.pgport = pg_strdup(optarg);
     214          12 :                 break;
     215           0 :             case 'R':
     216             :                 /* no-op, still accepted for backwards compatibility */
     217           0 :                 break;
     218           0 :             case 'P':           /* Function */
     219           0 :                 opts->selTypes = 1;
     220           0 :                 opts->selFunction = 1;
     221           0 :                 simple_string_list_append(&opts->functionNames, optarg);
     222           0 :                 break;
     223           0 :             case 'I':           /* Index */
     224           0 :                 opts->selTypes = 1;
     225           0 :                 opts->selIndex = 1;
     226           0 :                 simple_string_list_append(&opts->indexNames, optarg);
     227           0 :                 break;
     228           0 :             case 'T':           /* Trigger */
     229           0 :                 opts->selTypes = 1;
     230           0 :                 opts->selTrigger = 1;
     231           0 :                 simple_string_list_append(&opts->triggerNames, optarg);
     232           0 :                 break;
     233           2 :             case 's':           /* dump schema only */
     234           2 :                 opts->schemaOnly = 1;
     235           2 :                 break;
     236           0 :             case 'S':           /* Superuser username */
     237           0 :                 if (strlen(optarg) != 0)
     238           0 :                     opts->superuser = pg_strdup(optarg);
     239           0 :                 break;
     240           0 :             case 't':           /* Dump specified table(s) only */
     241           0 :                 opts->selTypes = 1;
     242           0 :                 opts->selTable = 1;
     243           0 :                 simple_string_list_append(&opts->tableNames, optarg);
     244           0 :                 break;
     245             : 
     246          16 :             case 'U':
     247          16 :                 opts->cparams.username = pg_strdup(optarg);
     248          16 :                 break;
     249             : 
     250          18 :             case 'v':           /* verbose */
     251          18 :                 opts->verbose = 1;
     252          18 :                 pg_logging_increase_verbosity();
     253          18 :                 break;
     254             : 
     255           0 :             case 'w':
     256           0 :                 opts->cparams.promptPassword = TRI_NO;
     257           0 :                 break;
     258             : 
     259           0 :             case 'W':
     260           0 :                 opts->cparams.promptPassword = TRI_YES;
     261           0 :                 break;
     262             : 
     263           0 :             case 'x':           /* skip ACL dump */
     264           0 :                 opts->aclsSkip = 1;
     265           0 :                 break;
     266             : 
     267           4 :             case '1':           /* Restore data in a single transaction */
     268           4 :                 opts->single_txn = true;
     269           4 :                 opts->exit_on_error = true;
     270           4 :                 break;
     271             : 
     272           2 :             case 0:
     273             : 
     274             :                 /*
     275             :                  * This covers the long options without a short equivalent.
     276             :                  */
     277           2 :                 break;
     278             : 
     279           0 :             case 2:             /* SET ROLE */
     280           0 :                 opts->use_role = pg_strdup(optarg);
     281           0 :                 break;
     282             : 
     283           0 :             case 3:             /* section */
     284           0 :                 set_dump_section(optarg, &(opts->dumpSections));
     285           0 :                 break;
     286             : 
     287           2 :             default:
     288           2 :                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     289           2 :                 exit_nicely(1);
     290             :         }
     291             :     }
     292             : 
     293             :     /* Get file name from command line */
     294          54 :     if (optind < argc)
     295          38 :         inputFileSpec = argv[optind++];
     296             :     else
     297          16 :         inputFileSpec = NULL;
     298             : 
     299             :     /* Complain if any arguments remain */
     300          54 :     if (optind < argc)
     301             :     {
     302           2 :         pg_log_error("too many command-line arguments (first is \"%s\")",
     303             :                      argv[optind]);
     304           2 :         fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
     305             :                 progname);
     306           2 :         exit_nicely(1);
     307             :     }
     308             : 
     309             :     /* Complain if neither -f nor -d was specified (except if dumping TOC) */
     310          52 :     if (!opts->cparams.dbname && !opts->filename && !opts->tocSummary)
     311             :     {
     312           2 :         pg_log_error("one of -d/--dbname and -f/--file must be specified");
     313           2 :         exit_nicely(1);
     314             :     }
     315             : 
     316             :     /* Should get at most one of -d and -f, else user is confused */
     317          50 :     if (opts->cparams.dbname)
     318             :     {
     319          18 :         if (opts->filename)
     320             :         {
     321           2 :             pg_log_error("options -d/--dbname and -f/--file cannot be used together");
     322           2 :             fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
     323             :                     progname);
     324           2 :             exit_nicely(1);
     325             :         }
     326          16 :         opts->useDB = 1;
     327             :     }
     328             : 
     329          48 :     if (opts->dataOnly && opts->schemaOnly)
     330             :     {
     331           2 :         pg_log_error("options -s/--schema-only and -a/--data-only cannot be used together");
     332           2 :         exit_nicely(1);
     333             :     }
     334             : 
     335          46 :     if (opts->dataOnly && opts->dropSchema)
     336             :     {
     337           2 :         pg_log_error("options -c/--clean and -a/--data-only cannot be used together");
     338           2 :         exit_nicely(1);
     339             :     }
     340             : 
     341             :     /*
     342             :      * -C is not compatible with -1, because we can't create a database inside
     343             :      * a transaction block.
     344             :      */
     345          44 :     if (opts->createDB && opts->single_txn)
     346             :     {
     347           2 :         pg_log_error("options -C/--create and -1/--single-transaction cannot be used together");
     348           2 :         exit_nicely(1);
     349             :     }
     350             : 
     351             :     /* Can't do single-txn mode with multiple connections */
     352          42 :     if (opts->single_txn && numWorkers > 1)
     353             :     {
     354           2 :         pg_log_error("cannot specify both --single-transaction and multiple jobs");
     355           2 :         exit_nicely(1);
     356             :     }
     357             : 
     358          40 :     opts->disable_triggers = disable_triggers;
     359          40 :     opts->enable_row_security = enable_row_security;
     360          40 :     opts->noDataForFailedTables = no_data_for_failed_tables;
     361          40 :     opts->noTablespace = outputNoTablespaces;
     362          40 :     opts->use_setsessauth = use_setsessauth;
     363          40 :     opts->no_comments = no_comments;
     364          40 :     opts->no_publications = no_publications;
     365          40 :     opts->no_security_labels = no_security_labels;
     366          40 :     opts->no_subscriptions = no_subscriptions;
     367             : 
     368          40 :     if (if_exists && !opts->dropSchema)
     369             :     {
     370           2 :         pg_log_error("option --if-exists requires option -c/--clean");
     371           2 :         exit_nicely(1);
     372             :     }
     373          38 :     opts->if_exists = if_exists;
     374          38 :     opts->strict_names = strict_names;
     375             : 
     376          38 :     if (opts->formatName)
     377             :     {
     378          10 :         switch (opts->formatName[0])
     379             :         {
     380           4 :             case 'c':
     381             :             case 'C':
     382           4 :                 opts->format = archCustom;
     383           4 :                 break;
     384             : 
     385           2 :             case 'd':
     386             :             case 'D':
     387           2 :                 opts->format = archDirectory;
     388           2 :                 break;
     389             : 
     390           2 :             case 't':
     391             :             case 'T':
     392           2 :                 opts->format = archTar;
     393           2 :                 break;
     394             : 
     395           2 :             default:
     396           2 :                 pg_log_error("unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
     397             :                              opts->formatName);
     398           2 :                 exit_nicely(1);
     399             :         }
     400          28 :     }
     401             : 
     402          36 :     AH = OpenArchive(inputFileSpec, opts->format);
     403             : 
     404          36 :     SetArchiveOptions(AH, NULL, opts);
     405             : 
     406             :     /*
     407             :      * We don't have a connection yet but that doesn't matter. The connection
     408             :      * is initialized to NULL and if we terminate through exit_nicely() while
     409             :      * it's still NULL, the cleanup function will just be a no-op.
     410             :      */
     411          36 :     on_exit_close_archive(AH);
     412             : 
     413             :     /* Let the archiver know how noisy to be */
     414          36 :     AH->verbose = opts->verbose;
     415             : 
     416             :     /*
     417             :      * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
     418             :      */
     419          36 :     AH->exit_on_error = opts->exit_on_error;
     420             : 
     421          36 :     if (opts->tocFile)
     422           0 :         SortTocFromFile(AH);
     423             : 
     424          36 :     AH->numWorkers = numWorkers;
     425             : 
     426          36 :     if (opts->tocSummary)
     427           0 :         PrintTOCSummary(AH);
     428             :     else
     429             :     {
     430          36 :         ProcessArchiveRestoreOptions(AH);
     431          36 :         RestoreArchive(AH);
     432             :     }
     433             : 
     434             :     /* done, print a summary of ignored errors */
     435          36 :     if (AH->n_errors)
     436           0 :         pg_log_warning("errors ignored on restore: %d", AH->n_errors);
     437             : 
     438             :     /* AH may be freed in CloseArchive? */
     439          36 :     exit_code = AH->n_errors ? 1 : 0;
     440             : 
     441          36 :     CloseArchive(AH);
     442             : 
     443          36 :     return exit_code;
     444             : }
     445             : 
     446             : static void
     447           2 : usage(const char *progname)
     448             : {
     449           2 :     printf(_("%s restores a PostgreSQL database from an archive created by pg_dump.\n\n"), progname);
     450           2 :     printf(_("Usage:\n"));
     451           2 :     printf(_("  %s [OPTION]... [FILE]\n"), progname);
     452             : 
     453           2 :     printf(_("\nGeneral options:\n"));
     454           2 :     printf(_("  -d, --dbname=NAME        connect to database name\n"));
     455           2 :     printf(_("  -f, --file=FILENAME      output file name (- for stdout)\n"));
     456           2 :     printf(_("  -F, --format=c|d|t       backup file format (should be automatic)\n"));
     457           2 :     printf(_("  -l, --list               print summarized TOC of the archive\n"));
     458           2 :     printf(_("  -v, --verbose            verbose mode\n"));
     459           2 :     printf(_("  -V, --version            output version information, then exit\n"));
     460           2 :     printf(_("  -?, --help               show this help, then exit\n"));
     461             : 
     462           2 :     printf(_("\nOptions controlling the restore:\n"));
     463           2 :     printf(_("  -a, --data-only              restore only the data, no schema\n"));
     464           2 :     printf(_("  -c, --clean                  clean (drop) database objects before recreating\n"));
     465           2 :     printf(_("  -C, --create                 create the target database\n"));
     466           2 :     printf(_("  -e, --exit-on-error          exit on error, default is to continue\n"));
     467           2 :     printf(_("  -I, --index=NAME             restore named index\n"));
     468           2 :     printf(_("  -j, --jobs=NUM               use this many parallel jobs to restore\n"));
     469           2 :     printf(_("  -L, --use-list=FILENAME      use table of contents from this file for\n"
     470             :              "                               selecting/ordering output\n"));
     471           2 :     printf(_("  -n, --schema=NAME            restore only objects in this schema\n"));
     472           2 :     printf(_("  -N, --exclude-schema=NAME    do not restore objects in this schema\n"));
     473           2 :     printf(_("  -O, --no-owner               skip restoration of object ownership\n"));
     474           2 :     printf(_("  -P, --function=NAME(args)    restore named function\n"));
     475           2 :     printf(_("  -s, --schema-only            restore only the schema, no data\n"));
     476           2 :     printf(_("  -S, --superuser=NAME         superuser user name to use for disabling triggers\n"));
     477           2 :     printf(_("  -t, --table=NAME             restore named relation (table, view, etc.)\n"));
     478           2 :     printf(_("  -T, --trigger=NAME           restore named trigger\n"));
     479           2 :     printf(_("  -x, --no-privileges          skip restoration of access privileges (grant/revoke)\n"));
     480           2 :     printf(_("  -1, --single-transaction     restore as a single transaction\n"));
     481           2 :     printf(_("  --disable-triggers           disable triggers during data-only restore\n"));
     482           2 :     printf(_("  --enable-row-security        enable row security\n"));
     483           2 :     printf(_("  --if-exists                  use IF EXISTS when dropping objects\n"));
     484           2 :     printf(_("  --no-comments                do not restore comments\n"));
     485           2 :     printf(_("  --no-data-for-failed-tables  do not restore data of tables that could not be\n"
     486             :              "                               created\n"));
     487           2 :     printf(_("  --no-publications            do not restore publications\n"));
     488           2 :     printf(_("  --no-security-labels         do not restore security labels\n"));
     489           2 :     printf(_("  --no-subscriptions           do not restore subscriptions\n"));
     490           2 :     printf(_("  --no-tablespaces             do not restore tablespace assignments\n"));
     491           2 :     printf(_("  --section=SECTION            restore named section (pre-data, data, or post-data)\n"));
     492           2 :     printf(_("  --strict-names               require table and/or schema include patterns to\n"
     493             :              "                               match at least one entity each\n"));
     494           2 :     printf(_("  --use-set-session-authorization\n"
     495             :              "                               use SET SESSION AUTHORIZATION commands instead of\n"
     496             :              "                               ALTER OWNER commands to set ownership\n"));
     497             : 
     498           2 :     printf(_("\nConnection options:\n"));
     499           2 :     printf(_("  -h, --host=HOSTNAME      database server host or socket directory\n"));
     500           2 :     printf(_("  -p, --port=PORT          database server port number\n"));
     501           2 :     printf(_("  -U, --username=NAME      connect as specified database user\n"));
     502           2 :     printf(_("  -w, --no-password        never prompt for password\n"));
     503           2 :     printf(_("  -W, --password           force password prompt (should happen automatically)\n"));
     504           2 :     printf(_("  --role=ROLENAME          do SET ROLE before restore\n"));
     505             : 
     506           2 :     printf(_("\n"
     507             :              "The options -I, -n, -N, -P, -t, -T, and --section can be combined and specified\n"
     508             :              "multiple times to select multiple objects.\n"));
     509           2 :     printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
     510           2 :     printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
     511           2 :     printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
     512           2 : }

Generated by: LCOV version 1.14