2014-02-05 37 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请参阅我编辑的答案。 –