Search code examples
postgresqldockerdocker-composecron

Crontab doesn't backup postgre DB in docker, but if i run script by hand it work prorely


I'm trying to set up automatic backup for postgre database. Postgre running in docker, so my script for backup is:

docker-compose exec postgres -U user database_name | gzip > "/var/server/my_service/data/backup-db/db_backup.sql.gz"

And its working fine, if I run it manually. I wrote the following job for the crontab (every 5 minutes just for testing):

*/5 * * * * cd /var/server/my_service && sh /var/server/my_service/data/backup/backup_script

This command also working great, if i run it manually it create valid DB backups that i can use. But crontab just create empty archive, without any data. I just cant understand why. My guess is that the output stream that catches the gzip is normally generated in manual mode, but completely empty when the crontab trying to run command

I thought there were problems with access rights and put the in the root crontab but it didn't help


UPD: so... problem in backup_script, error in logs says the input device is not a TTY

I tried google it and add -T, but it didn't help as well


Solution

  • Update your /var/server/my_service/data/backup/backup_script with the following:

    Prefix the first 3 line in your script:

    #!/bin/bash
    source ~/.bash_profile
    cd /var/server/my_service
    
    #
    # rest of your script
    #
    

    Your crontab line should be (At 04:44 on every day-of-month):

    44 4 */1 * * /var/server/my_service/data/backup/backup_script