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 :

  1. Go to Cpanel > Cron Jobs
  2. Select between Standard or Advanced, that's up to you !
  3. 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
  4. 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 (-p). Notice the date variable inserted in the filename. Finally our SQL file is gzipped using the gzip directive.
  5. You now have to set your backup frequency. And relax !

Note :

  • 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.
  • 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 ;-)

Pour développer votre projet WordPress ou Woocommerce, faites appel à mon expertise pour réaliser un site rapide, performant et fonctionnel.

Je soumets mon projet

Si vous avez trouvé une faute d’orthographe, informez-nous en sélectionnant le texte en question et en appuyant sur Ctrl + Entrée s’il vous plaît.

Articles en rapport:

Backup all your MySQL databases with one line of cron

par Matt Lecture: 1 min

Pin It on Pinterest

Share This

Spelling error report

The following text will be sent to our editors: