1

I have shell script executed by cron.

*/5 * * * * /home/foo/backups/backup.sh

An backup script:

#!/bin/bash

FILE_NAME=$(date +%Y%m%d%H%M) PATH1="/home/foo/backups/data-$FILE_NAME.sql.gz"

pg_dump -U XXX -E utf8 --no-acl -h localhost --no-owner XXX | gzip -c > $PATH1 aws s3 cp $PATH1 s3://XXX

Cron is executed correctly and local backup is created. But dump file is not copied to Amazon S3 bucket. When I run /home/foo/backups/backup.sh manually, file is transfered to S3.

Is there any reason why S3 cli could not work when aws s3 cp is executed from cron?

jnemecz
  • 115
  • 5
  • I don't know anything about aws, but I know that cron does not have all the path environment. Try adding the path to the aws command in the script and see if works. If aws calls cp you may need to add the path to cp as well. This is just a guess. – user68186 Mar 17 '21 at 15:53
  • @user68186 Adding full path to aws helped, you can post it as an answer. – jnemecz Mar 17 '21 at 15:58

1 Answers1

4

Path and Other Environment Variables are not in cron

cron does not have many environment variables set. The default value of the path variable in cron is just /usr/bin:/bin. Try adding the full /path/to/the/aws command in the script.

See this answer for a different solution: cronjob cannot find environment variables defined in .bashrc

Also see Crontab execution of multiple commands but last command is omitted

Hope this helps

user68186
  • 33,360