2013-09-24 90 views
1

我試着讓自動備份腳本運行。Ubuntu上的Postgres備份腳本+ cron

如果我通過在終端中鍵入「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日誌:

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。

問候

馬丁

回答

1

交互方式運行時,你有一個TTY,而是從cron運行時沒有附加TTY,即你的腳本有沒有標準輸入進行讀操作(在等待這將阻止腳本用於無法從任何地方輸入的輸入)。

您應該讓腳本無需密碼即可運行,例如在特定用戶帳戶下運行它並配置postgres以允許該用戶無需密碼即可連接(即編輯pg_hba.conf)。

2

您可以在用戶的​​主目錄中創建要用於cron作業的.pgpass。該.pgpass格式應該是:

hostname:port:database:username:password 

在實踐中,我發現,你需要使用通配符(「*」)的主機名,端口和數據庫cron作業工作。例如,如果你正在運行的cron作業的Postgres的用戶和用戶的主目錄是/ home/Postgres的,然後創建一個名爲在/ home「.pgpass」文件/包含以下內容的Postgres:

*:*:*:postgres:password 

您的cron作業在postgres用戶下運行,然後應該無需密碼即可工作。 此密碼文件的官方文檔位於: http://www.postgresql.org/docs/8.4/static/libpq-pgpass.html