1

我尝试运行自动备份脚本。

如果我通过在终端中键入“bash backup.su”来运行它,该脚本可以完美运行。对于每个查询和操作,我都必须输入密码(数据库用户),但它工作正常。

但只要我不使用终端(例如通过 cron),它就不会。它只是打印出我的日志文件,没有任何信息。

我认为这与用户权限有关

终端输出:

Making backup directory in /scripts/2013-09-26-daily 

Performing schema-only backups
------------------------------------------
Password for user backupuser: <- Query getting all databases with certain string in name
The following databases were matched for schema-only backup:

Performing full backups
------------------------------------------
Password for user backupuser: <- Query getting all databases
Plain backup of DB1
Password:
Plain backup of DB2
Password:
Plain backup of AndSoOn
Password:
All database backups complete!

定时日志:

Making backup directory in /scripts/2013-09-26-daily/

Performing schema-only backups
------------------------------------------
The following databases were matched for schema-only backup:

Performing full backups
------------------------------------------

All database backups complete!

如您所见,似乎没有执行任何查询。现在我正在使用一个特殊的备份用户,但它也不适用于 postgres。(对于 postgres 用户,他还要求输入密码......但是一个空的会停止脚本)

有人对我有线索吗?正如我所说,它可以完美地手动运行,bot 不支持 cron。

问候

马丁

4

2 回答 2

3

您可以在要用于 cron 作业的用户主目录中创建一个 .pgpass。.pgpass 格式应该是:

hostname:port:database:username:password

在实践中,我发现您需要为主机名、端口和数据库使用通配符 ("*") 才能使 cron 作业正常工作。例如,如果您以 postgres 用户身份运行 cron 作业,并且用户主目录是 /home/postgres,则在 /home/postgres 中创建一个名为“.pgpass”的文件,其内容如下:

*:*:*:postgres:password

您在 postgres 用户下运行的 cron 作业应该可以在不需要密码的情况下运行。这个密码文件的官方文档在这里: http ://www.postgresql.org/docs/8.4/static/libpq-pgpass.html

于 2013-11-07T23:25:04.127 回答
1

When running interactively you have a tty, but when running from cron there is no tty attached, i.e. your script has no stdin to read from (that would block the script while waiting for input that can never be entered from anywhere).

You should make your script runnable without a password, e.g. run it under a specific user account and configure postgres to allow that user to connect without a password (i.e. edit pg_hba.conf).

于 2013-09-24T18:34:17.133 回答