6 * Copyright (c) 2010-2012, PostgreSQL Global Development Group
7 * contrib/pg_upgrade/option.c
12 #include "pg_upgrade.h"
14 #include <getopt_long.h>
16 #include <sys/types.h>
23 static void usage(void);
24 static void check_required_directory(char **dirpath,
25 char *envVarName, char *cmdLineOption, char *description);
34 * Parses the command line (argc, argv[]) and loads structures
37 parseCommandLine(int argc, char *argv[])
39 static struct option long_options[] = {
40 {"old-datadir", required_argument, NULL, 'd'},
41 {"new-datadir", required_argument, NULL, 'D'},
42 {"old-bindir", required_argument, NULL, 'b'},
43 {"new-bindir", required_argument, NULL, 'B'},
44 {"old-options", required_argument, NULL, 'o'},
45 {"new-options", required_argument, NULL, 'O'},
46 {"old-port", required_argument, NULL, 'p'},
47 {"new-port", required_argument, NULL, 'P'},
49 {"user", required_argument, NULL, 'u'},
50 {"check", no_argument, NULL, 'c'},
51 {"link", no_argument, NULL, 'k'},
52 {"retain", no_argument, NULL, 'r'},
53 {"verbose", no_argument, NULL, 'v'},
56 int option; /* Command line option */
57 int optindex = 0; /* used by getopt_long */
58 int os_user_effective_id;
61 time_t run_time = time(NULL);
63 user_opts.transfer_mode = TRANSFER_MODE_COPY;
65 os_info.progname = get_progname(argv[0]);
67 /* Process libpq env. variables; load values here for usage() output */
68 old_cluster.port = getenv("PGPORTOLD") ? atoi(getenv("PGPORTOLD")) : DEF_PGUPORT;
69 new_cluster.port = getenv("PGPORTNEW") ? atoi(getenv("PGPORTNEW")) : DEF_PGUPORT;
71 os_user_effective_id = get_user_info(&os_info.user);
72 /* we override just the database user name; we got the OS id above */
75 pg_free(os_info.user);
76 /* must save value, getenv()'s pointer is not stable */
77 os_info.user = pg_strdup(getenv("PGUSER"));
82 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 ||
83 strcmp(argv[1], "-?") == 0)
88 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
90 puts("pg_upgrade (PostgreSQL) " PG_VERSION);
95 /* Allow help and version to be run as root, so do the test here. */
96 if (os_user_effective_id == 0)
97 pg_log(PG_FATAL, "%s: cannot be run as root\n", os_info.progname);
99 if ((log_opts.internal = fopen_priv(INTERNAL_LOG_FILE, "a")) == NULL)
100 pg_log(PG_FATAL, "cannot write to log file %s\n", INTERNAL_LOG_FILE);
102 while ((option = getopt_long(argc, argv, "d:D:b:B:cko:O:p:P:ru:v",
103 long_options, &optindex)) != -1)
108 old_cluster.bindir = pg_strdup(optarg);
112 new_cluster.bindir = pg_strdup(optarg);
116 user_opts.check = true;
120 old_cluster.pgdata = pg_strdup(optarg);
121 old_cluster.pgconfig = pg_strdup(optarg);
125 new_cluster.pgdata = pg_strdup(optarg);
126 new_cluster.pgconfig = pg_strdup(optarg);
130 user_opts.transfer_mode = TRANSFER_MODE_LINK;
134 old_cluster.pgopts = pg_strdup(optarg);
138 new_cluster.pgopts = pg_strdup(optarg);
142 * Someday, the port number option could be removed and passed
143 * using -o/-O, but that requires postmaster -C to be
144 * supported on all old/new versions.
147 if ((old_cluster.port = atoi(optarg)) <= 0)
149 pg_log(PG_FATAL, "invalid old port number\n");
155 if ((new_cluster.port = atoi(optarg)) <= 0)
157 pg_log(PG_FATAL, "invalid new port number\n");
163 log_opts.retain = true;
167 pg_free(os_info.user);
168 os_info.user = pg_strdup(optarg);
171 * Push the user name into the environment so pre-9.1
172 * pg_ctl/libpq uses it.
174 pg_putenv("PGUSER", os_info.user);
178 pg_log(PG_REPORT, "Running in verbose mode\n");
179 log_opts.verbose = true;
184 "Try \"%s --help\" for more information.\n",
190 /* label start of upgrade in logfiles */
191 for (filename = output_files; *filename != NULL; filename++)
193 if ((fp = fopen_priv(*filename, "a")) == NULL)
194 pg_log(PG_FATAL, "cannot write to log file %s\n", *filename);
196 /* Start with newline because we might be appending to a file. */
198 "-----------------------------------------------------------------\n"
199 " pg_upgrade run on %s"
200 "-----------------------------------------------------------------\n\n",
205 /* Get values from env if not already set */
206 check_required_directory(&old_cluster.bindir, "PGBINOLD", "-b",
207 "old cluster binaries reside");
208 check_required_directory(&new_cluster.bindir, "PGBINNEW", "-B",
209 "new cluster binaries reside");
210 check_required_directory(&old_cluster.pgdata, "PGDATAOLD", "-d",
211 "old cluster data resides");
212 check_required_directory(&new_cluster.pgdata, "PGDATANEW", "-D",
213 "new cluster data resides");
220 printf(_("pg_upgrade upgrades a PostgreSQL cluster to a different major version.\n\
222 pg_upgrade [OPTION]...\n\
225 -b, --old-bindir=OLDBINDIR old cluster executable directory\n\
226 -B, --new-bindir=NEWBINDIR new cluster executable directory\n\
227 -c, --check check clusters only, don't change any data\n\
228 -d, --old-datadir=OLDDATADIR old cluster data directory\n\
229 -D, --new-datadir=NEWDATADIR new cluster data directory\n\
230 -k, --link link instead of copying files to new cluster\n\
231 -o, --old-options=OPTIONS old cluster options to pass to the server\n\
232 -O, --new-options=OPTIONS new cluster options to pass to the server\n\
233 -p, --old-port=OLDPORT old cluster port number (default %d)\n\
234 -P, --new-port=NEWPORT new cluster port number (default %d)\n\
235 -r, --retain retain SQL and log files after success\n\
236 -u, --user=NAME cluster superuser (default \"%s\")\n\
237 -v, --verbose enable verbose internal logging\n\
238 -V, --version display version information, then exit\n\
239 -?, -h, --help show this help, then exit\n\
241 Before running pg_upgrade you must:\n\
242 create a new database cluster (using the new version of initdb)\n\
243 shutdown the postmaster servicing the old cluster\n\
244 shutdown the postmaster servicing the new cluster\n\
246 When you run pg_upgrade, you must provide the following information:\n\
247 the data directory for the old cluster (-d OLDDATADIR)\n\
248 the data directory for the new cluster (-D NEWDATADIR)\n\
249 the \"bin\" directory for the old version (-b OLDBINDIR)\n\
250 the \"bin\" directory for the new version (-B NEWBINDIR)\n\
253 pg_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin\n\
254 or\n"), old_cluster.port, new_cluster.port, os_info.user);
257 $ export PGDATAOLD=oldCluster/data\n\
258 $ export PGDATANEW=newCluster/data\n\
259 $ export PGBINOLD=oldCluster/bin\n\
260 $ export PGBINNEW=newCluster/bin\n\
264 C:\\> set PGDATAOLD=oldCluster/data\n\
265 C:\\> set PGDATANEW=newCluster/data\n\
266 C:\\> set PGBINOLD=oldCluster/bin\n\
267 C:\\> set PGBINNEW=newCluster/bin\n\
268 C:\\> pg_upgrade\n"));
270 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
275 * check_required_directory()
277 * Checks a directory option.
278 * dirpath - the directory name supplied on the command line
279 * envVarName - the name of an environment variable to get if dirpath is NULL
280 * cmdLineOption - the command line option corresponds to this directory (-o, -O, -n, -N)
281 * description - a description of this directory option
283 * We use the last two arguments to construct a meaningful error message if the
284 * user hasn't provided the required directory name.
287 check_required_directory(char **dirpath, char *envVarName,
288 char *cmdLineOption, char *description)
290 if (*dirpath == NULL || strlen(*dirpath) == 0)
294 if ((envVar = getenv(envVarName)) && strlen(envVar))
295 *dirpath = pg_strdup(envVar);
297 pg_log(PG_FATAL, "You must identify the directory where the %s.\n"
298 "Please use the %s command-line option or the %s environment variable.\n",
299 description, cmdLineOption, envVarName);
303 * Trim off any trailing path separators
306 if ((*dirpath)[strlen(*dirpath) - 1] == '/')
308 if ((*dirpath)[strlen(*dirpath) - 1] == '/' ||
309 (*dirpath)[strlen(*dirpath) - 1] == '\\')
311 (*dirpath)[strlen(*dirpath) - 1] = 0;
317 * If a configuration-only directory was specified, find the real data dir
318 * by quering the running server. This has limited checking because we
319 * can't check for a running server because we can't find postmaster.pid.
322 adjust_data_dir(ClusterInfo *cluster)
324 char filename[MAXPGPATH];
326 cmd_output[MAX_STRING];
330 /* If there is no postgresql.conf, it can't be a config-only dir */
331 snprintf(filename, sizeof(filename), "%s/postgresql.conf", cluster->pgconfig);
332 if ((fp = fopen(filename, "r")) == NULL)
336 /* If PG_VERSION exists, it can't be a config-only dir */
337 snprintf(filename, sizeof(filename), "%s/PG_VERSION", cluster->pgconfig);
338 if ((fp = fopen(filename, "r")) != NULL)
344 /* Must be a configuration directory, so find the real data directory. */
346 prep_status("Finding the real data directory for the %s cluster",
347 CLUSTER_NAME(cluster));
350 * We don't have a data directory yet, so we can't check the PG version,
351 * so this might fail --- only works for PG 9.2+. If this fails,
352 * pg_upgrade will fail anyway because the data files will not be found.
354 snprintf(cmd, sizeof(cmd), "\"%s/postmaster\" -D \"%s\" -C data_directory",
355 cluster->bindir, cluster->pgconfig);
357 if ((output = popen(cmd, "r")) == NULL ||
358 fgets(cmd_output, sizeof(cmd_output), output) == NULL)
359 pg_log(PG_FATAL, "Could not get data directory using %s: %s\n",
360 cmd, getErrorText(errno));
364 /* Remove trailing newline */
365 if (strchr(cmd_output, '\n') != NULL)
366 *strchr(cmd_output, '\n') = '\0';
368 cluster->pgdata = pg_strdup(cmd_output);