brando
brando

Reputation: 8431

Cron job not executing script with docker exec

I have created a bash script to run a backup of my containerized postgres database. This script is called "do-pg-backup.sh":

#! bin/bash
db=$(docker container ls -q --filter name=mydbcontainer* --format "{{.Names}}")
docker exec -it $db /etc/pg-backup/pg-backup.sh
echo "done pg backup"

If I execute the do-pg-backup.sh script in the ubuntu terminal ("bash do-pg-backup.sh") it runs perfectly as expected.

Now I have setup the following crontab so this script will run every minute ("sudo crontab -e"):

* * * * * bash /path/to/do-pg-backup.sh >> /tmp/cron.log
* * * * * echo "minute passed" >> /tmp/cron.log

My cron.log file looks like this after a few minutes:

minute passed
done pg backup
minute passed
done pg backup
...and so on...

However, the do-pg-backup.sh script is NOT successfully backing up the the db from the context of the cronjob. What am I doing wrong?

Upvotes: 0

Views: 436

Answers (1)

brando
brando

Reputation: 8431

Thanks to the helpful hint from @markp-fuso, I discovered that I was getting the following error:

the input device is not a TTY

After a little googling around, I made the following edit to do-pg-backup.sh script:

docker exec -i $db /etc/pg-backup/pg-backup.sh

Note: changed "-it" to "-i"

And it now works. I dont understand why it now works, but am happy it is fixed am my very important data is getting backed up.

Source

Upvotes: 1

Related Questions