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:


set -e

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

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


The content of the lb script is:


# 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 ]
    rm /home/chh1/Dropbox/postgres_backup/crewdb.backup.sql
fi >/dev/null 2>&1

echo "PostgreSQL database crewdb backed up on $(date)" >> /home/chh1  
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   
rsync -acu --delete --exclude-from='/home/chh1/rsync_exclude.txt' /home/    
/media/chh1/f86f92f7-bcb4-4b8e-8a64-8a210606d422/home_backup/ >/dev/null    

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

pg_dump -h localhost -p 5432 -U postgres -d crewdb -w -C -F p -b -v -f    
/postgres_backup/crewdb.backup.sql >/dev/null 2>&1


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


pg_dump -h localhost -p 5432 -U postgres -d crewdb -w -C -F p -b -v -f    
/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!

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