PostgreSQL数据库备份是空的 - 出了什么问题?

时间:2017-02-21 15:28:01

标签: linux bash postgresql cron backup

我想配置Alfresco数据库的备份(PostgreSQL rdbms)。

为此,我有一个这样的脚本:

的psql创建备份:

#!/bin/sh
pg_dump -h localhost -U postgres -Fc -Z9 -c -f /home/bykovan/alfresco-data-backup/"`date +%d-%m-%Y`".alfresco.backup alfresco

我将其保存在/home/bykovan并授予执行权:

[bykovan@docflow ~]$ sudo chmod 711 psql-create-backup

然后我把工作添加到了cron:

[bykovan@docflow ~]$ sudo crontab -e
...

0 0 * * * /home/bykovan/psql-create-backup

pg_hba.conf我添加了这一行host all all 127.0.0.1/32 trust

[bykovan@docflow ~]$ sudo vi /var/lib/pgsql/9.5/data/pg_hba.conf

-- SKIPPED --


# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
#local   all             all                                     peer
local   all             all                                     md5
# IPv4 local connections:
#host    all             all             127.0.0.1/32            ident
host     all             all             127.0.0.1/32            trust
host     all             all             127.0.0.1/32            md5
# IPv6 local connections:
#host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident

host    all             all             192.168.0.0/16            md5
-- VISUAL BLOCK --

备份为空:

[bykovan@docflow alfresco-data-backup]$ ls -lh

-rw-r--r-- 1 root root 0 feb 18 00:00 18-02-2017.alfresco.backup
-rw-r--r-- 1 root root 0 feb 19 00:00 19-02-2017.alfresco.backup
-rw-r--r-- 1 root root 0 feb 20 00:00 20-02-2017.alfresco.backup
-rw-r--r-- 1 root root 0 feb 21 00:00 21-02-2017.alfresco.backup

pg_hba.conf中,我添加了这一行:

host    all  all       ::1/128      md5
host    all  postgres  127.0.0.1/32 trust 

然后重启服务:

[bykovan@docflow ~]$ sudo service postgresql-9.5 stop
Redirecting to /bin/systemctl stop  postgresql-9.5.service
[bykovan@docflow ~]$ sudo service postgresql-9.5 start
Redirecting to /bin/systemctl start  postgresql-9.5.service

然后在提示符中直接运行命令(我被提示输入密码)并看到以下内容:

[bykovan@docflow alfresco-data-backup]$ ls -lh

-rw-r--r-- 1 root root    0 feb 17 18:39 17-02-2017.alfresco.backup
-rw-r--r-- 1 root root    0 feb 18 00:00 18-02-2017.alfresco.backup
-rw-r--r-- 1 root root    0 feb 19 00:00 19-02-2017.alfresco.backup
-rw-r--r-- 1 root root    0 feb 20 00:00 20-02-2017.alfresco.backup
-rw-r--r-- 1 root root    0 feb 21 19:41 21-02-2017.alfresco.backup
-rw-r--r-- 1 root root 667K feb 22 07:04 22-02-2017.alfresco.backup

但是通过cron运行任务给了我空文件......

我哪里错了?..

我将非常感谢这些信息。谢谢大家。

1 个答案:

答案 0 :(得分:1)

我在脚本中设置了密码,现在看起来像这样:

PGPASSWORD="some_password" pg_dump -h localhost -U postgres -Fc -Z9 -c -f /home/bykovan/alfresco-data-backup/"`date +%d-%m-%Y`".alfresco.backup alfresco

现在一切都好。