This article describes how to set up a cron job that automatically backs up a MySQL database to a file at specific intervals.
There are two ways to run cron jobs that back up a MySQL database. You can either include MySQL login information in the cron job command itself, or you can use a configuration file to store the MySQL login information.
You can run scheduled backups of a MySQL database by creating a cron job that runs the following command:
/usr/bin/mysqldump -u dbusername -p'dbpassword' dbname > /path/backup.sql
Replace dbusername with the database user, dbpassword with the database user's password, dbname with the database to back up, and path with the path where you want to store the backup file. This example uses backup.sql for the backup's filename, but you can use any filename you want.
For information about how to use cPanel to create a cron job, please see this article.
Alternatively, you can create a configuration file in your home directory that stores MySQL login information. Using this method, you do not have to include login information in your cron job commands. To do this, follow these steps:
[client] user = dbusername password = "dbpassword" host = localhost
Create a cron job that runs the following command. Replace dbname with the name of the database to back up, and replace path with the path to store the backup file. This example uses backup.sql for the backup's filename, but you can use any filename you want:
mysqldump dbname > /path/backup.sql
For information about how to use cPanel to create a cron job, please see this article.
For more information about the mysqldump program, please visit http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html.