0

I have two cron jobs on my AWS server (triggered via crontab -e) to invoke pg_dump on specific databases.

The two scripts are identical with the exception of the target database they're backing up ($PG_SRC_DB). One fails at the pg_dump command but outputs no errors nor logs:

PGPASSWORD=$PG_SRC_PASSWORD $PG_DUMP_PATH --no-owner $PG_SRC_DB -h $PG_SRC_HOST -p $PG_SRC_PORT -U $PG_SRC_USER > dump.sql

I know this because I've forwarded all stderr and stdout to /tmp/cron.log and I get the error:

mv: cannot stat 'dump.sql': No such file or directory

This command happens after I've run the pg_dump and the dump.sql file should have been created, but fails because pg_dump fails

I've debugged for hours and have done the following:

  • re-directed stderr to stdout to an error log
  • invoked export <PGVAR> for PGPASSWORD, PGUSER, PGHOST, and PGDATABASE
  • set my PATH to whatever my PATH is when I run the script manually in terminal
  • use the absolute path to pg_dump
  • use #!/bin/bash -l login shell
  • output pg_dump error to a file with ... > dump.sql 2> error.log

Both scripts work in terminal when run manually. I have no idea how else to debug the pg_dump command as I'm not getting any logs from it.

2 Answers 2

1

Generally, this happens because environment variable values are different for terminal and cronjobs. You can try to import terminal environment to script. Let's say if you are running the script under user root. Try to add a line at top of the script.

#!/bin/bash
. /root/.profile
0

I'm not entirely sure what happened but it works now. I did these two things:

  1. chmod +x pg_backup.sh -- I had already done this, and the script was running in cron, since I could get some output in the logs
  2. run dos2unix on the file to transform any CR or CRLF to LF -- I'm not sure how this would solve the issue since I wrote the script in Vim on a Mac

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .