Christian Mayne

Simple MySQL Command line DB dump for cron

by | Sep 3, 2014 | Work | 0 comments

If it doesn’t already exists, create the file .my.cnf in your home directory.

Add the following to it, replacing db_username and db_password with the database username and password. make sure the file permissions are set to 600 (chmod 600 .my.cnf) so it is readable by you alone.

[mysqldump]
user=db_username
password=db_password

The following command will dump the database db_name to the db_backups directory with the filename yyyymmdd.sql where yyyy is the current year, mm is the current montyh and dd the current day:

mysqldump -u db_username db_name > db_backups/"$(date +%Y%m%d).sql"

This will not ask for a password, so can be used in a shell script or in cron.

Christian Mayne

Christian Mayne

Web developer and internet consultant, Anglo-Concertina player, fan of traditional music and record collector.