我尝试让自动备份脚本运行。
如果我通过在终端中键入“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!
Cron Log:
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用户,他也要求输入密码......但是空的密码会停止脚本)
有人对我有线索吗?正如我所说它完全手动完成,机器人没有与cron。
问候
马丁
答案 0 :(得分: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
答案 1 :(得分:1)
当以交互方式运行时,你有一个tty,但是当从cron运行时没有连接tty,即你的脚本没有stdin可供读取(这会阻止脚本在等待输入时永远无法从任何地方输入)。
您应该在没有密码的情况下使您的脚本可以运行,例如在特定用户帐户下运行并配置postgres以允许该用户在没有密码的情况下进行连接(即编辑pg_hba.conf
)。