Pg_dump and rsync backup script not working in anacron

by Christian Hick   Last Updated October 20, 2019 04:01 AM

I have two sh scripts called Z-backup and lb. Z-backup is a script that I have placed in cron.daily so it is run as an anacron job and calls the lb script. The content of the Z-backup script is:

#!/bin/sh


set -e

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

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

exit

The content of the lb script is:

#!/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

If I just run lb in the terminal the script works just fine the way I want it to. However the scheduled anacron job only gets partly executed and I can not figure out why this is the case. The log files are created and the rsync command works, thus the home folder is backed up via anacron but not pg_dump. When I run the pg_dump commands in lb and save stderr to a file such as:

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

and

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>/rsync_errors.txt

The /rsync_errors file shows the database dump but no errors such as

$ head rsync_log.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
.
.
.

My assumption here is that anacron (because of set -e in Z-backup) is not executing because of this but am not sure as I have really dumped stderr to /dev/null (in lb). All the other commands in lb don't generate any stderr output.

Does anyone have a clue why this is not working? And why I am getting stderr output from pg_dump even when the backup seems to go ahead just fine. I much appreciate any help!



Answers 2


.pgpass is per user. Is the Unix user that runs the scheduled script the same user as the one that's testing the script manually?

And why I am getting stderr output from pg_dump even when the backup seems to go ahead just fine

Because of the -v option. You should remove it, and also remove the >/dev/null 2>&1 that cause potentially useful error messages to disappear, including in the upper caller.

Daniel Vérité
Daniel Vérité
October 20, 2019 10:47 AM

The answer is to change of pg_hba.conf to:

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

Now the anacron script works just fine.

Christian Hick
Christian Hick
October 27, 2019 21:29 PM

Related Questions


Updated July 27, 2017 11:01 AM

Updated November 03, 2015 16:00 PM

Updated March 13, 2017 14:01 PM

Updated December 01, 2015 15:00 PM

Updated December 05, 2017 01:01 AM