Pg_dump and rsync backup script not working in anacron

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

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!



Related Questions


Save WAL-files to remote Server continuously

Updated July 27, 2017 11:01 AM

Behavior of rsync with file that's still being written?

Updated November 03, 2015 16:00 PM