Reputation: 4870
I'm trying to run a cronjob to backup my database every day. I have created the cronjob by logging in to root and doing crontab -e
, then pasting in my command:
* * * * * /bin/bash -l -c 'cd /var/lib/postgresql && sudo -u postgres pg_dump db_development -O -f "/var/lib/postgresql/backup/db-`date +\%Y-\%m-\%d\\%k:\%M:\%S`.sql"' > /var/lib/postgresql/log/cron.log 2>&1
It is terribly annoying because the command works PERFECTLY if i run it in terminal, with either the postgres user or with root. However, I get no data back at all from the cronjob task. The log file is blank and the backup file is never created.
I've been trying to fix this for 10+ hours and I'm out of ideas. Any help is appreciated. Thanks.
Upvotes: 0
Views: 910
Reputation: 30167
While you might have environment issues there, such a long command probably is better off to be in a separate script that's called from the cron.
Upvotes: 0
Reputation: 29266
Most likely an environment issue. Check for any DB environment settings in your shell.
You could also wrap your cron command line in a script (and call the script from cron). Then you can initially have the script dump the environment (so you can see what is missing), bu ultimately have it setup the correct env. vars so that it does work.
Upvotes: 1