2

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?

JasonGenX
  • 4,952
  • 27
  • 106
  • 198
  • Maybe pg_dump is not in the PATH ? -->> use an absolute pathname, or set the PATH inside your cron script. – joop Mar 21 '16 at 18:49
  • And do you mean p**g**_dump or p**d**_dump? You keep using both, but postgres's command is **pg_dump**. – antiduh Mar 21 '16 at 18:53
  • I am running absolute path. pg_dump runs. it just craps out on errors which make the output 0. – JasonGenX Mar 21 '16 at 19:05
  • Can you show us the relevant parts of your script? What arguments are you sending to pg_dump? Is your script when run by cron not generating any output on stderr? – antiduh Mar 22 '16 at 00:38

3 Answers3

5

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>
JasonGenX
  • 4,952
  • 27
  • 106
  • 198
  • Finally, after one week – Edwinner Apr 28 '18 at 18:02
  • 1
    For security reasons, it's better to use a .pgpass file instead. So I suggest replacing `export PGPASSWORD=` with `export PGPASSFILE=`. See the documentation for more: https://www.postgresql.org/docs/9.1/static/libpq-envars.html – Christian Aug 01 '18 at 10:08
1

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.

Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78
0

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
Saman Salehi
  • 1,004
  • 1
  • 12
  • 19