1

I have set a cron job to take backup the database every hour.

/etc/crontab

0 */1 * * * sh /opt/post.sh

/opt/post.sh

pg_dump -U postgres db_name > /home/golp/Desktop/backup.sql

For authentication, I am using .pgpass file.

localhost:5432:db_name:postgres:password

Regarding permissions,

chmod 0600 ~/.pgpass
chown postgres:postgres /home/golp/Desktop/

The cron job seems to run every hour (I checked the log). I find that the backup file created by cron job is empty. I am using CentOS 7 1503 and Postgresql 9.5

NOTE: When I run pg_dump -U postgres db_name > /home/golp/Desktop/backup.sql as root user in terminal, backup is created as expected.

Kindly help.

1

Try putting the full path to pg_dump in your script.

0

If you are modifying the file /etc/crontab manually, you need to add entry like the following (for user root):

0 * * * * root sh /opt/post.sh

You are missing the username root to run cron job as. This is needed because this is a global file (not like the per-user files under: /var/spool/cron/).

Edit: To help diagnosing cron job errors, you can change the cron job line to read:

0 * * * * root sh /opt/post.sh >> /tmp/cron.log 2>&1

This will redirect all stdout and stderr messages to file /tmp/cron.log (append mode to not overwrite it each time cron job is run).

  • I have tried using 0 * * * * root sh /opt/post.sh but the backup file is empty again. – Rajkumar R Jan 31 '17 at 12:27
  • @RajkumarRajendran: I updated my answer. Maybe, you are getting some DB error. – Khaled Jan 31 '17 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.