I try to make automated (every night at 4) backups from a postgresql database running inside a docker container.
#!/bin/sh
CONTAINER=`docker ps|grep name_of_container|awk '{print $1}'`
USER='postgre_user'
PASSWORD='changed'
BUDIR='/some/path/backup/'
docker run -it --link $CONTAINER:db -v $BUDIR:/backup/ -e "PGPASSWORD=$PASSWORD" pg_dump -h db -U $USER -Fc -f /backup/$(date +%Y-%m-%d-%H-%M-%S).dump
My crontab looks like this:
0 4 * * * /path/to/script.sh
The script works fine when I execute it manually and it also get executed from cron (I tried * * * * * for debugging).
I can't figure out how to make cron and the script work together. So far I tried:
* * * * * [...] &>cron.log
)docker exec [...] > output.log
in script$CONTAINER
contains the correct docker id when run from cron, cron.log and output.log are created but empty.
Any ideas?
Can't use docker run -it --link [...]
when running from cron - I use docker run --link [...]
now.
To elaborate on Martin's answer, -it
is shorthand for -i -t
, ie run interactively on terminal (pseudo TTY), so it's not necessary for running in a cronjob.
If the command is suitable for automation, -it
should be not necessary, so removing it should let you run docker from a cron job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With