* Portions Copyright (c) 1994, Regents of the University of California
*
*
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.88 2007/01/25 02:46:33 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.89 2007/01/25 03:30:43 momjian Exp $
*
*-------------------------------------------------------------------------
*/
static int use_setsessauth = 0;
static int server_version;
+static FILE *OPF;
+static char *filename = NULL;
int
main(int argc, char *argv[])
{
- char *pghost = NULL;
- char *pgport = NULL;
- char *pguser = NULL;
- char *pgdb = NULL;
+ char *pghost = NULL;
+ char *pgport = NULL;
+ char *pguser = NULL;
+ char *pgdb = NULL;
bool force_password = false;
bool data_only = false;
bool globals_only = false;
bool roles_only = false;
bool tablespaces_only = false;
bool schema_only = false;
- PGconn *conn;
+ PGconn *conn;
int encoding;
- const char *std_strings;
+ const char *std_strings;
int c,
ret;
{"inserts", no_argument, NULL, 'd'},
{"attribute-inserts", no_argument, NULL, 'D'},
{"column-inserts", no_argument, NULL, 'D'},
+ {"file", required_argument, NULL, 'f'},
{"globals-only", no_argument, NULL, 'g'},
{"host", required_argument, NULL, 'h'},
{"ignore-version", no_argument, NULL, 'i'},
pgdumpopts = createPQExpBuffer();
- while ((c = getopt_long(argc, argv, "acdDgh:il:oOp:rsS:tU:vWxX:", long_options, &optindex)) != -1)
+ while ((c = getopt_long(argc, argv, "acdDf:gh:il:oOp:rsS:tU:vWxX:", long_options, &optindex)) != -1)
{
switch (c)
{
case 'D':
appendPQExpBuffer(pgdumpopts, " -%c", c);
break;
+
+ case 'f':
+ filename = optarg;
+#ifndef WIN32
+ appendPQExpBuffer(pgdumpopts, " -f '%s'", filename);
+#else
+ appendPQExpBuffer(pgdumpopts, " -f \"%s\"", filename);
+#endif
+
+ break;
case 'g':
globals_only = true;
exit(1);
}
}
+
+ /*
+ * Open the output file if required, otherwise use stdout
+ */
+ if (filename)
+ {
+ OPF = fopen(filename, PG_BINARY_W);
+ if (!OPF)
+ {
+ fprintf(stderr, _("%s: could not open the output file \"%s\"\n"),
+ progname, filename);
+ exit(1);
+ }
+ }
+ else
+ OPF = stdout;
/*
* Get the active encoding and the standard_conforming_strings setting, so
if (!std_strings)
std_strings = "off";
- printf("--\n-- PostgreSQL database cluster dump\n--\n\n");
+ fprintf(OPF, "--\n-- PostgreSQL database cluster dump\n--\n\n");
if (verbose)
dumpTimestamp("Started on");
- printf("\\connect postgres\n\n");
+ fprintf(OPF, "\\connect postgres\n\n");
if (!data_only)
{
/* Replicate encoding and std_strings in output */
- printf("SET client_encoding = '%s';\n",
+ fprintf(OPF, "SET client_encoding = '%s';\n",
pg_encoding_to_char(encoding));
- printf("SET standard_conforming_strings = %s;\n", std_strings);
+ fprintf(OPF, "SET standard_conforming_strings = %s;\n", std_strings);
if (strcmp(std_strings, "off") == 0)
- printf("SET escape_string_warning = 'off';\n");
- printf("\n");
+ fprintf(OPF, "SET escape_string_warning = 'off';\n");
+ fprintf(OPF, "\n");
if (!tablespaces_only)
{
if (verbose)
dumpTimestamp("Completed on");
- printf("--\n-- PostgreSQL database cluster dump complete\n--\n\n");
+ fprintf(OPF, "--\n-- PostgreSQL database cluster dump complete\n--\n\n");
+
+ if (filename)
+ fclose(OPF);
exit(0);
}
printf(_(" %s [OPTION]...\n"), progname);
printf(_("\nGeneral options:\n"));
+ printf(_(" -f, --file=FILENAME output file name\n"));
printf(_(" -i, --ignore-version proceed even when server version mismatches\n"
" pg_dumpall version\n"));
printf(_(" --help show this help, then exit\n"));
i_rolcomment = PQfnumber(res, "rolcomment");
if (PQntuples(res) > 0)
- printf("--\n-- Roles\n--\n\n");
+ fprintf(OPF, "--\n-- Roles\n--\n\n");
for (i = 0; i < PQntuples(res); i++)
{
appendPQExpBuffer(buf, ";\n");
}
- printf("%s", buf->data);
+ fprintf(OPF, "%s", buf->data);
if (server_version >= 70300)
dumpUserConfig(conn, rolename);
PQclear(res);
- printf("\n\n");
+ fprintf(OPF, "\n\n");
destroyPQExpBuffer(buf);
}
"ORDER BY 1,2,3");
if (PQntuples(res) > 0)
- printf("--\n-- Role memberships\n--\n\n");
+ fprintf(OPF, "--\n-- Role memberships\n--\n\n");
for (i = 0; i < PQntuples(res); i++)
{
char *grantor = PQgetvalue(res, i, 2);
char *option = PQgetvalue(res, i, 3);
- printf("GRANT %s", fmtId(roleid));
- printf(" TO %s", fmtId(member));
+ fprintf(OPF, "GRANT %s", fmtId(roleid));
+ fprintf(OPF, " TO %s", fmtId(member));
if (*option == 't')
- printf(" WITH ADMIN OPTION");
- printf(" GRANTED BY %s;\n", fmtId(grantor));
+ fprintf(OPF, " WITH ADMIN OPTION");
+ fprintf(OPF, " GRANTED BY %s;\n", fmtId(grantor));
}
PQclear(res);
- printf("\n\n");
+ fprintf(OPF, "\n\n");
}
/*
"SELECT groname, grolist FROM pg_group ORDER BY 1");
if (PQntuples(res) > 0)
- printf("--\n-- Role memberships\n--\n\n");
+ fprintf(OPF, "--\n-- Role memberships\n--\n\n");
for (i = 0; i < PQntuples(res); i++)
{
if (strcmp(groname, usename) == 0)
continue;
- printf("GRANT %s", fmtId(groname));
- printf(" TO %s;\n", fmtId(usename));
+ fprintf(OPF, "GRANT %s", fmtId(groname));
+ fprintf(OPF, " TO %s;\n", fmtId(usename));
}
PQclear(res2);
PQclear(res);
destroyPQExpBuffer(buf);
- printf("\n\n");
+ fprintf(OPF, "\n\n");
}
/*
"ORDER BY 1");
if (PQntuples(res) > 0)
- printf("--\n-- Tablespaces\n--\n\n");
+ fprintf(OPF, "--\n-- Tablespaces\n--\n\n");
for (i = 0; i < PQntuples(res); i++)
{
appendPQExpBuffer(buf, ";\n");
}
- printf("%s", buf->data);
+ fprintf(OPF, "%s", buf->data);
free(fspcname);
destroyPQExpBuffer(buf);
}
PQclear(res);
- printf("\n\n");
+ fprintf(OPF, "\n\n");
}
/*
PGresult *res;
int i;
- printf("--\n-- Database creation\n--\n\n");
+ fprintf(OPF, "--\n-- Database creation\n--\n\n");
if (server_version >= 80100)
res = executeQuery(conn,
exit(1);
}
- printf("%s", buf->data);
+ fprintf(OPF, "%s", buf->data);
if (server_version >= 70300)
dumpDatabaseConfig(conn, dbname);
PQclear(res);
destroyPQExpBuffer(buf);
- printf("\n\n");
+ fprintf(OPF, "\n\n");
}
appendStringLiteralConn(buf, pos + 1, conn);
appendPQExpBuffer(buf, ";\n");
- printf("%s", buf->data);
+ fprintf(OPF, "%s", buf->data);
destroyPQExpBuffer(buf);
free(mine);
}
if (verbose)
fprintf(stderr, _("%s: dumping database \"%s\"...\n"), progname, dbname);
- printf("\\connect %s\n\n", fmtId(dbname));
+ fprintf(OPF, "\\connect %s\n\n", fmtId(dbname));
+
+ if (filename)
+ fclose(OPF);
+
ret = runPgDump(dbname);
if (ret != 0)
{
fprintf(stderr, _("%s: pg_dump failed on database \"%s\", exiting\n"), progname, dbname);
exit(1);
}
+
+ if (filename)
+ {
+ OPF = fopen(filename, PG_BINARY_A);
+ if (!OPF)
+ {
+ fprintf(stderr, _("%s: could not re-open the output file \"%s\"\n"),
+ progname, filename);
+ exit(1);
+ }
+ }
+
}
PQclear(res);
* Win32 has to use double-quotes for args, rather than single quotes.
* Strangely enough, this is the only place we pass a database name on the
* command line, except "postgres" which doesn't need quoting.
+ *
+ * If we have a filename, use the undocumented plain-append pg_dump format.
*/
+ if (filename)
+ {
+#ifndef WIN32
+ appendPQExpBuffer(cmd, "%s\"%s\" %s -Fa '", SYSTEMQUOTE, pg_dump_bin,
+#else
+ appendPQExpBuffer(cmd, "%s\"%s\" %s -Fa \"", SYSTEMQUOTE, pg_dump_bin,
+#endif
+ pgdumpopts->data);
+ }
+ else
+ {
#ifndef WIN32
appendPQExpBuffer(cmd, "%s\"%s\" %s -Fp '", SYSTEMQUOTE, pg_dump_bin,
#else
appendPQExpBuffer(cmd, "%s\"%s\" %s -Fp \"", SYSTEMQUOTE, pg_dump_bin,
#endif
pgdumpopts->data);
+ }
+
/* Shell quoting is not quite like SQL quoting, so can't use fmtId */
for (p = dbname; *p; p++)
"%Y-%m-%d %H:%M:%S",
#endif
localtime(&now)) != 0)
- printf("-- %s %s\n\n", msg, buf);
+ fprintf(OPF, "-- %s %s\n\n", msg, buf);
}