As I’ve lost some data in my MySQL database recently, I’ve decided to make backups more regularly and I’ve been playing with Cron in Cpanel in order to set up automatic backups of all my databases.
Here’s a short how-to which might help some people out :
Go to Cpanel > Cron Jobs
Select between Standard or Advanced, that’s up to you !
In the command field, type this :
date=`date -I`; mysqldump -u yourusername -p yourpassword --all-databases > /home/LOGIN/backups/xbackup_$date.sql; gzip /home/LOGIN/backups/xbackup_$date.sql
Now let’s see what this all means : the date line formats the date so that we can append it to our backup filename.
We then ask mysql to dump all databases into /home/LOGIN/public_html/backups/xbackup_$date.sql, where :
LOGIN is your Cpanel name, using “yourusername” as user (
-u) and “yourpassword” as the password (
Notice the date variable inserted in the filename. Finally our SQL file is gzipped using the gzip directive.
You now have to set your backup frequency. And relax !
1. I’ve created a user “backup” with all privileges in Cpanel > MySQL databases and associated him with all my databases so that I can backup everything in one single file with just one line of cron.
2. Protect your backup directory so that people cannot download the whole of you SQL data. You can enable directory protection with Cpanel > Password protect directories > *select your backup directory* > *check the box and enter a username/password* > *validate*.
That’s it ! Have fun, you should never have to worry about your databases’ backups again ;-)