Cron job failing to backing up a Postgres database

Posted by user705142 on Server Fault See other posts from Server Fault or by user705142
Published on 2012-04-04T05:22:40Z Indexed on 2012/04/04 5:31 UTC
Read the original article Hit count: 192

Filed under:
|
|

I'm unsure what's going on here:

I've got a backup script which runs fine under root. It produces a >300kb database dump in the proper directory.

When running it as a cron job with exactly the same command however, an empty gzip file appears with nothing in it.

The cron log shows no error, just that the command has been run.

This is the script:

#! /bin/bash

DIR="/opt/backup"
YMD=$(date "+%Y-%m-%d")
su -c "pg_dump -U postgres mydatabasename | gzip -6 > "$DIR/database_backup.$YMD.gz" " postgres

# delete backup files older than 60 days
OLD=$(find $DIR -type d -mtime +60)
if [ -n "$OLD" ] ; then
    echo deleting old backup files: $OLD
    echo $OLD | xargs rm -rfv
fi

And the cron job:

01 10 * * * root sh /opt/daily_backup_script.sh

It produces a database_backup file, just an empty one. Anyone know what's going on here?

© Server Fault or respective owner

Related posts about backup

Related posts about postgresql