Pg_dump 和 rsync 备份脚本在 anacron 中不起作用

Pg_dump 和 rsync 备份脚本在 anacron 中不起作用

我有两个 sh 脚本,分别称为 Z-backup 和 lb。Z-backup 是我放在 cron.daily 中的脚本,因此它作为 anacron 作业运行并调用 lb 脚本。Z-backup 脚本的内容为:

#!/bin/sh


set -e

# Anacron script to run a daily backup at boot time.

exec /home/chh1/bin/lb >/dev/null 2>&1

exit

lb脚本的内容为:

#!/bin/sh

# Script to do a backup of PostgreSQL database crewdb and /home folder.
# ~/.pgpass file has been created to support pg_dump -w flag.

# The following if statement removes crewdb.backup.sql if it exists.

if [ -e /home/chh1/Dropbox/postgres_backup/crewdb.backup.sql ]
then
    rm /home/chh1/Dropbox/postgres_backup/crewdb.backup.sql
fi >/dev/null 2>&1


echo "PostgreSQL database crewdb backed up on $(date)" >> /home/chh1  
/logs/postgres_bak_log.txt
pg_dump -h localhost -p 5432 -U postgres -d crewdb -w -C -F p -b -v -f    
~/Dropbox/postgres_backup/crewdb.backup.sql >/dev/null 2>&1

echo "Home folder backed up on $(date)" >> /home/chh1   
/logs/home_bak_log.txt
rsync -acu --delete --exclude-from='/home/chh1/rsync_exclude.txt' /home/    
/media/chh1/f86f92f7-bcb4-4b8e-8a64-8a210606d422/home_backup/ >/dev/null    
2>&1


if [ -e /media/chh1/f86f92f7-bcb4-4b8e-8a64-8a210606d422/home_backup   
/chh1/Dropbox/postgres_backup/crewdb.backup.sql ]
then
    rm /media/chh1/f86f92f7-bcb4-4b8e-8a64-8a210606d422/home_backup  
/chh1/Dropbox/postgres_backup/crewdb.backup.sql
fi >/dev/null 2>&1


pg_dump -h localhost -p 5432 -U postgres -d crewdb -w -C -F p -b -v -f    
/media/chh1/f86f92f7-bcb4-4b8e-8a64-8a210606d422/home_backup/chh1/Dropbox   
/postgres_backup/crewdb.backup.sql >/dev/null 2>&1


exit

如果我只是在终端中运行 lb,脚本就会按照我想要的方式正常工作。(运行 lb 后,我运行 echo $? 并获得退出状态 0)。但是,计划的 anacron 作业仅部分执行,我无法弄清楚为什么会这样。日志条目已创建,rsync 命令有效,因此主文件夹是通过 anacron 而不是 pg_dump 备份的。当我在 lb 中运行 pg_dump 命令并将 stderr 保存到文件时,例如:

pg_dump -h localhost -p 5432 -U postgres -d crewdb -w -C -F p -b -v -f    
~/Dropbox/postgres_backup/crewdb.backup.sql 2>/pg_dump_errors.txt

pg_dump -h localhost -p 5432 -U postgres -d crewdb -w -C -F p -b -v -f    
/media/chh1/f86f92f7-bcb4-4b8e-8a64-8a210606d422/home_backup/chh1/Dropbox   
/postgres_backup/crewdb.backup.sql 2>/pg_dump_errors.txt

/pg_dump_errors.txt 文件为两个 pg_dump 命令生成相同的输出,并显示数据库转储,但没有错误,例如

head pg_dump_errors.txt && tail pg_dump_errors.txt

pg_dump: last built-in OID is 16383
pg_dump: reading extensions
pg_dump: identifying extension members
pg_dump: reading schemas
pg_dump: reading user-defined tables
pg_dump: reading user-defined functions
pg_dump: reading user-defined types
pg_dump: reading procedural languages
pg_dump: reading user-defined aggregate functions
pg_dump: reading user-defined operators
pg_dump: creating FK CONSTRAINT "public.timesheet    
          timesheet_date_linkid_fkey"
pg_dump: creating FK CONSTRAINT "public.timesheet   
          timesheet_employee_functionid_fkey"
pg_dump: creating FK CONSTRAINT "public.timesheet 
          timesheet_employeeid_fkey"
pg_dump: creating FK CONSTRAINT "public.work work_categoryid_fkey"
pg_dump: creating FK CONSTRAINT "public.work work_clientid_fkey"
pg_dump: creating FK CONSTRAINT "public.work work_date_linkid_fkey"
pg_dump: creating FK CONSTRAINT "public.work_expenses  
          work_expenses_date_linkid_fkey"
pg_dump: creating FK CONSTRAINT "public.work_expenses 
          work_expenses_employeeid_fkey"
pg_dump: creating FK CONSTRAINT "public.work_expenses 
          work_expenses_expense_categoryid_fkey"
pg_dump: creating FK CONSTRAINT "public.work_expenses   
          work_expenses_workid_fkey"

我在这里的假设是 anacron(因为 Z-backup 中的 set -e)由于这个原因而未执行,但我不确定,因为我确实已将 stderr 转储到 /dev/null(在 lb 中)。lb 中的所有其他命令都不会生成任何 stderr 输出。

附加信息

-rwxr-xr-x 1 root root 119 Oct 13 13:57 /etc/cron.daily/z-backup
-rwxrwxr-x 1 chh1 chh1 1279 Oct 20 17:26 /home/chh1/bin/lb
-rw------- 1 chh1 chh1 68 Oct 21 06:00 /home/chh1/.pgpass

cat .pgpass
#hostname:port:database:username:password
*:*:crewdb:*:pg#$as(

postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+----------------------- crewdb | postgres | UTF8 | en_NZ.UTF-8 | en_NZ.UTF-8 | =Tc/postgres + | | | | | postgres=CTc/postgres+ | | | | | chh1=CTc/postgres postgres | postgres | UTF8 | en_NZ.UTF-8 | en_NZ.UTF-8 | template0 | postgres | UTF8 | en_NZ.UTF-8 | en_NZ.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_NZ.UTF-8 | en_NZ.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres

sudo cat /etc/postgresql/10/main/pg_hba.conf

# Database administrative login by Unix domain socket
local   all             postgres                                md5

# TYPE  DATABASE        USER            ADDRESS                 METHOD

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

有人知道为什么这不起作用吗?为什么即使备份似乎进行得很顺利,我仍会从 pg_dump 获得 stderr 输出。非常感谢任何帮助!

答案1

.pgpass是每个用户。运行计划脚本的 Unix 用户与手动测试脚本的用户是同一个用户吗?

为什么即使备份似乎进行得很顺利,我仍会从 pg_dump 获得 stderr 输出

因为-v选项。您应该将其删除,同时删除>/dev/null 2>&1导致可能有用的错误消息消失的,包括在上层调用者中。

答案2

答案是将 pg_hba.conf 更改为:

sudo grep ^[^#] /etc/postgresql/10/main/pg_hba.conf
[sudo] password for chh1: 
local   all             all                                     trust 
local   all             all                                     trust
host    all             all             127.0.0.1/32            trust
host    all             all             ::1/128                 md5
local   replication     all                                     md5
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5

现在 anacron 脚本运行正常。

相关内容