Help troubleshooting cronjob to automate database backup


I set up a cronjob by running the command crontab -e and inserting this command

0 * * * * docker-compose exec db mysqldump -u vikunja -pPASSWORDGOESHERE vikunja > backup.sql

I also tried, but the behavior is the same

0 * * * * sudo docker-compose exec db mysqldump -u vikunja -pPASSWORDGOESHERE vikunja > backup.sql

The cronjob appears to run successfully because backup.sql is being created, but it has size of 0.
If I manually run the command, the backup.sql much larger.

I am sure this is something simple, but it is my first time setting up cronjobs,so any help would be appreciated. Thank you!

something to note is that when I run the command manually I do sudo before the command

I’m on Ubuntu 20.04.3 LTS

I think that’s because you don’t have a folder set. The docker-compose command does not know what compose stack you’re referring to and fails. I’d use the full name of the container (From docker ps) and run it with a plain docker command. Something like this:

0 * * * * docker exec vikunja_db mysqldump -u vikunja -pPASSWORDGOESHERE vikunja > /path/to/backups/backup.sql

The backup.sql file is empty because the error output is going to stderr but you’re only piping stdout to the file. Check out this article for information about how to change that:

If you’re looking for a simple solution to create backups, you might be interested in this thing I recently built: konrad/docker-db-backup - docker-db-backup - Gitea

That worked, thanks for your help. I will also be checking out your script when I get a bit more time. Thanks!

1 Like