JasonGenX
JasonGenX

Reputation: 5434

Can't run pg_dump from cron job

Our system is on AWS. Debian EC2 instance. RDS postgres instance. when I log into the "admin" account pd_dump works.

$ whom
admin

$pd_dump 
[it works...]

then I crontab -e in order to create a test backup event.

*/2 * * * * /home/admin/storage/db_backup.sh

and indeed, it runs every 2 minutes.

in db_backup.sh I have:

pg_dump > dbbackup.txt

the script and the folder I am running at belong to "admin".

The dbbackup.txt is always generated with a zero size.

why is that?

Upvotes: 2

Views: 5729

Answers (3)

Albert Agoya
Albert Agoya

Reputation: 1

According to my mac, the zero dump files were caused because crontab couldn't understand pg_dump.

Instead, I did the following. FYI I use connection string URI.

But what's important is the full path I set to the pg_dump

/Library/PostgreSQL/13/bin/pg_dump --dbname=postgresql://admin:D%40t%40sc13nc3@localhost:5432/digitaldairy | gzip > test1232323.gz

Upvotes: 0

Sergey Telshevsky
Sergey Telshevsky

Reputation: 12197

We can only guess here, but I have a suggestion. Try redirecting ALL output (including stderr) to the file to see if it gives you some information.

in your /home/admin/storage/db_backup.sh add 2>&1:

pg_dump > dbbackup.txt 2>&1

you may also use this and redirect your cron line in case your .sh file has some problems:

*/2 * * * * /home/admin/storage/db_backup.sh >> /home/admin/cron_log.txt 2>&1

EDIT: just noticed you post an answer already. Buy if you'll have time, try removing exports and see if you can get some errors in logs that may help.

Upvotes: 1

JasonGenX
JasonGenX

Reputation: 5434

I found out what the culprit was. Since I'm ran in the context of admin but in cron, my environment strings were not set.

setting this at the head of my db_backup.sh resolved this:

export PGPASSWORD=<value>
export PGHOST=<value>
export PGDATABASE=<value>
export PGUSER=<value>

Upvotes: 5

Related Questions