Search code examples
postgresqldocker-composecrondump

docker-compose: dumping postgres database in a cron job not working


I wrote a script to dump a database from host machine:

#!/bin/bash
cd /mydir/
echo "Dumping database .."
/usr/local/bin/docker-compose exec pg-service pg_dump -U pg -d "MYDB" -f /var/lib/postgresql/dbback_ 2>&1 | tee -a /backup/backup.log
date=`date +%F_%Hh_%Mm`
mv pg_data/dbback_ /backup/dbback_$date.sql
cd /backup/
echo "compressing backup .."
tar -czvf dbback_$date.tar.gz  dbback_$date.sql
rm dbback_$date.sql

/var/lib/postgresql/ is exposed in MYDIR/pg_data/

when running the script manually it does the task perfectly, but when I create a cron job for the script it create an empty tar.gz file (the docker-compose command fail)

50 22 * * * root /backup/backup.sh

any suggestions!


Solution

  • I solved the issue using docker instead of docker-compose and by specifying the container name|id.

    /usr/bin/docker exec -u root CONTAINER-NAME pg_dump -v -Fc -U pg -d "MY-DATABASE" -f /var/lib/postgresql/MYDB_BACKUP.dump