How to configure a MySQL cronjob backup without exposing your password

Create a .my.cnf file in the home directory, (note the leading ‘.’ this hides the file)
nano ~/.my.cnf

Add the following section to your file:
[mysqldump]
user = mysqluser
password = yourpassword

change the file permission to 600 to secure the cnf file.
chmod 600 ~/.my.cnf

configure your cronjob as normal, note that you do not need to pass the -p switch as crond will read in your .my.cnf
crontab -e
0 30 * * * mysqldump databasename -u mysqluser > pathtofile.sql

thats it nice and simple

Further crontab syntax info can be found here

Leave a Reply

Your email address will not be published. Required fields are marked *