Anton wrote:
>> Как можно сделать backup базы из скрипта ?.
>> При бэкапе требуется ввод пароля ручками, а как его можно передать с
>> помощью
>> скрипта ?
> На тему .psqlrc смотрел?
Или вот патчик сделал хороший человек.
diff -Naur postgresql-8.2.3/src/bin/pg_dump/pg_backup_archiver.c
postgresql-8.2.3.WORK/src/bin/pg_dump/pg_backup_archiver.c
--- postgresql-8.2.3/src/bin/pg_dump/pg_backup_archiver.c 2006-11-22 01:19:46.000000000 +0300
+++ postgresql-8.2.3.WORK/src/bin/pg_dump/pg_backup_archiver.c 2007-04-19 11:25:42.000000000 +0400
@@ -162,7 +162,7 @@
AHX->maxRemoteVersion = 999999;
ConnectDatabase(AHX, ropt->dbname,
- ropt->pghost, ropt->pgport, ropt->username,
+ ropt->pghost, ropt->pgport, ropt->username, ropt->userpass,
ropt->requirePassword, ropt->ignoreVersion);
/*
diff -Naur postgresql-8.2.3/src/bin/pg_dump/pg_backup_db.c postgresql-8.2.3.WORK/src/bin/pg_dump/pg_backup_db.c
--- postgresql-8.2.3/src/bin/pg_dump/pg_backup_db.c 2006-10-04 04:30:05.000000000 +0400
+++ postgresql-8.2.3.WORK/src/bin/pg_dump/pg_backup_db.c 2007-04-19 10:04:08.000000000 +0400
@@ -209,11 +209,12 @@
const char *pghost,
const char *pgport,
const char *username,
+ const char *userpass,
const int reqPwd,
const int ignoreVersion)
{
ArchiveHandle *AH = (ArchiveHandle *) AHX;
- char *password = NULL;
+ char *password = userpass ? strdup(userpass) : NULL;
bool need_pass = false;
if (AH->connection)
@@ -254,7 +255,7 @@
}
} while (need_pass);
- if (password)
+ if (password && reqPwd)
free(password);
/* check to see that the backend connection was successfully made */
diff -Naur postgresql-8.2.3/src/bin/pg_dump/pg_backup.h postgresql-8.2.3.WORK/src/bin/pg_dump/pg_backup.h
--- postgresql-8.2.3/src/bin/pg_dump/pg_backup.h 2006-10-15 03:07:22.000000000 +0400
+++ postgresql-8.2.3.WORK/src/bin/pg_dump/pg_backup.h 2007-04-19 10:01:22.000000000 +0400
@@ -108,6 +108,7 @@
char *pgport;
char *pghost;
char *username;
+ char *userpass;
int ignoreVersion;
int noDataForFailedTables;
int requirePassword;
@@ -136,6 +137,7 @@
const char *pghost,
const char *pgport,
const char *username,
+ const char *userpass,
const int reqPwd,
const int ignoreVersion);
diff -Naur postgresql-8.2.3/src/bin/pg_dump/pg_dump.c postgresql-8.2.3.WORK/src/bin/pg_dump/pg_dump.c
--- postgresql-8.2.3/src/bin/pg_dump/pg_dump.c 2006-10-10 03:36:59.000000000 +0400
+++ postgresql-8.2.3.WORK/src/bin/pg_dump/pg_dump.c 2007-04-19 10:07:15.000000000 +0400
@@ -201,6 +201,7 @@
const char *pghost = NULL;
const char *pgport = NULL;
const char *username = NULL;
+ const char *userpass = NULL;
const char *dumpencoding = NULL;
const char *std_strings;
bool oids = false;
@@ -246,6 +247,7 @@
{"table", required_argument, NULL, 't'},
{"exclude-table", required_argument, NULL, 'T'},
{"password", no_argument, NULL, 'W'},
+ {"userpass", no_argument, NULL, 'w'},
{"username", required_argument, NULL, 'U'},
{"verbose", no_argument, NULL, 'v'},
{"no-privileges", no_argument, NULL, 'x'},
@@ -296,7 +298,7 @@
}
}
- while ((c = getopt_long(argc, argv, "abcCdDE:f:F:h:in:N:oOp:RsS:t:T:uU:vWxX:Z:",
+ while ((c = getopt_long(argc, argv, "abcCdDE:f:F:h:in:N:oOp:RsS:t:T:uU:vWxX:Z:w:1",
long_options, &optindex)) != -1)
{
switch (c)
@@ -406,6 +408,11 @@
force_password = true;
break;
+ case 'w':
+ force_password = false;
+ userpass = optarg;
+ break;
+
case 'x': /* skip ACL dump */
aclsSkip = true;
break;
@@ -525,7 +532,7 @@
* death.
*/
g_conn = ConnectDatabase(g_fout, dbname, pghost, pgport,
- username, force_password, ignore_version);
+ username, userpass, force_password, ignore_version);
/* Set the client encoding if requested */
if (dumpencoding)
@@ -768,6 +775,7 @@
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"));
+ printf(_(" -w, --userpass give an password in command line\n"));
printf(_("\nIf no database name is supplied, then the PGDATABASE environment\n"
"variable value is used.\n\n"));
diff -Naur postgresql-8.2.3/src/bin/pg_dump/pg_restore.c postgresql-8.2.3.WORK/src/bin/pg_dump/pg_restore.c
--- postgresql-8.2.3/src/bin/pg_dump/pg_restore.c 2006-10-15 03:07:22.000000000 +0400
+++ postgresql-8.2.3.WORK/src/bin/pg_dump/pg_restore.c 2007-04-19 10:07:41.000000000 +0400
@@ -97,6 +97,7 @@
{"no-reconnect", 0, NULL, 'R'},
{"port", 1, NULL, 'p'},
{"password", 0, NULL, 'W'},
+ {"userpass", 0, NULL, 'w'},
{"schema", 1, NULL, 'n'},
{"schema-only", 0, NULL, 's'},
{"superuser", 1, NULL, 'S'},
@@ -137,7 +138,7 @@
}
}
- while ((c = getopt_long(argc, argv, "acCd:ef:F:h:iI:lL:n:Op:P:RsS:t:T:uU:vWxX:1",
+ while ((c = getopt_long(argc, argv, "acCd:ef:F:h:iI:lL:n:Op:P:RsS:t:T:uU:vWxX:w:1",
cmdopts, NULL)) != -1)
{
switch (c)
@@ -240,6 +241,11 @@
opts->requirePassword = true;
break;
+ case 'w':
+ opts->requirePassword = false;
+ opts->userpass = optarg;
+ break;
+
case 'x': /* skip ACL dump */
opts->aclsSkip = 1;
break;
@@ -414,6 +420,7 @@
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"));
+ printf(_(" -w, --userpass give an password in command line\n"));
printf(_(" -e, --exit-on-error exit on error, default is to continue\n"));
printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
diff -Naur postgresql-8.2.3/src/bin/psql/startup.c postgresql-8.2.3.WORK/src/bin/psql/startup.c
--- postgresql-8.2.3/src/bin/psql/startup.c 2006-10-04 04:30:06.000000000 +0400
+++ postgresql-8.2.3.WORK/src/bin/psql/startup.c 2007-04-19 11:37:57.000000000 +0400
@@ -70,6 +70,7 @@
char *host;
char *port;
char *username;
+ char *userpass;
char *logfilename;
enum _actions action;
char *action_string;
@@ -200,6 +201,8 @@
if (pset.getPassword)
password = simple_prompt(password_prompt, 100, false);
+ else if ( options.userpass )
+ password = pg_strdup(options.userpass);
/* loop until we have a password if requested by backend */
do
@@ -437,6 +440,7 @@
{"variable", required_argument, NULL, 'v'},
{"version", no_argument, NULL, 'V'},
{"password", no_argument, NULL, 'W'},
+ {"userpass", no_argument, NULL, 'w'},
{"expanded", no_argument, NULL, 'x'},
{"no-psqlrc", no_argument, NULL, 'X'},
{"help", no_argument, NULL, '?'},
@@ -451,7 +455,7 @@
memset(options, 0, sizeof *options);
- while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:HlL:no:p:P:qR:sStT:uU:v:VWxX?1",
+ while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:HlL:no:p:P:qR:sStT:uU:v:w:VWxX?1",
long_options, &optindex)) != -1)
{
switch (c)
@@ -598,6 +602,10 @@
case 'W':
pset.getPassword = true;
break;
+ case 'w':
+ pset.getPassword = false;
+ options->userpass = optarg;
+ break;
case 'x':
pset.popt.topt.expanded = true;
break;