Secure MySQL backup cron job – my.cnf is not being read

mikl picture mikl · Mar 2, 2009 · Viewed 7.5k times · Source

I have tried to make backup cron job on my webserver running FreeBSD. Currently it looks something like this:

/usr/local/bin/mysqldump --opt --single-transaction --comments --dump-date --no-autocommit --all-databases --result-file=/var/backups/mysql/all.sql

It works fine when I run it as root (since root has a .my.cnf with the username and password used to connect, but when the job is run by cron, the my.cnf file is not read.

Is there any way around that without having to put username and password into the command itself (since that's kinda insecure)?

Strangely, I have the same setup with PostgreSQL and a .pgpass file, and that works like a charm.

Answer

Alnitak picture Alnitak · Mar 2, 2009

Use the --defaults-extra-file option to tell it where to find the .my.cnf file (assuming it's readable by whichever user is running mysqldump.