2014-02-05 40 views
0

我有下面的postgres備份腳本,它是一個shell腳本並且被編寫爲運行ans postgres用戶。但是問題是postgres用戶沒有權限寫這些目錄。我作爲用戶在這些機器上沒有sudo,但我已將目錄更改爲擁有755並添加到具有讀寫執行權限的組之一。由於postgres用戶不是unix用戶組的一部分,我想我遇到了這個問題。權限問題:創建postgres回作爲postgres用戶

我的目標是把這個cron的標籤,但在此之前,我需要拿到劇本具有適當權限運行:

#!/bin/bash 
# location to store backups 
backup_dir="/location/to/dir" 
# name of the backup file has the date 
backup_date=`date +%d-%m-%Y` 
# only keep the backup for 30 days (maintain low storage) 
number_of_days=30 
databases=`psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'` 
for i in $databases; do 
    if [ "$i" != "template0" ] && [ "$i" != "template1" ]; then 
    echo Dumping $i to $backup_dir$i\_$backup_date 
    pg_dump -Fc $i > $backup_dir$i\_$backup_date 
    fi 
done 
find $backup_dir -type f -prune -mtime +$number_of_days -exec rm -f {} \; 

回答

1

這樣做,一定要登錄爲超級用戶之前(sudo su ),並嘗試執行這些:

  1. useradd -G unix postgres(添加Postgres的用戶UNIX組)

  2. su postgres(登錄作爲Postgres的用戶)

  3. mkdir folder(去哪裏Postgres的需要寫文件)

目錄***從這個線下來是我的回答@發現,失蹤的分號問題

只是爲了舉例說明一個shell腳本,您可以使用read命令捕獲密碼並將其放入一個變量中。在這裏,我將密碼存儲在密碼之後並在之後回顯。我希望這有幫助。

`#!/bin/bash` 

`read -s -p "Password: " password` 
`echo $password` 
+0

保羅帕諾我想出了一個辦法做到這一點實際上並沒有使用Postgres的用戶,我可以作爲數據庫管理員,但這個問題上運行pg_dump的是'的pg_dump -Fc -Uga_db_admin MyDatabase的 - 密碼> $ BACKUP_DIR \ _ $ backup_date' shell腳本會提示輸入密碼,以避免這種情況。 –

+0

@ find-missing-semicolon請參閱我編輯的答案。 –