Newbie ? Mot de passe ?



h1

Backup all your MySQL databases with one line of cron

le Samedi 29 mai 2004 à 00:28 par Matt

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 -uyourusername -pyourpassword ––all-databases > /home/LOGIN/public_html/backups/xbackup_$date.sql ; gzip /home/LOGIN/public_html/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 ;-)

Lien permanent

12 commentaires à “Backup all your MySQL databases with one line of cron”

  1. 1
    Stephen dit :

    It’s working better. Except I get “date : invalid option — I
    BusyBox v0.60.5 (2003.01.24-22:44+0000) multi-call binary

    Usage : date [OPTION]… [+FORMAT]” Let me check the manual for MySQL. I might figure it out.


  2. 2
    Stephen dit :

    I don’t think that -I is an option on my Redhat linux box.

    I’ve got the file created now. The file isn’t named by date though.

    Thanks for your help.


  3. 3
    Matt dit :

    Stephen,

    Just found this page on shell scripting and date formatting. I have no idea if it will run on your box but it may be worth trying ;-)

    Matt


  4. 4
    Mike dit :

    How would I get the minutes and seconds in the above date formula ?


  5. 5
    Matt dit :

    Hi Mike,

    Mike,

    According to the Unix date manual, this should be something like : date %D %M %S (date formatted mm/dd/yy with minutes and seconds).

    Not tested but should be along these lines :-)

    -Matt


  6. 6
    cyberdyne dit :

    Hi there,
    Been trying to add backup TIME to :
    date=`date -I` ; mysqldump -uyourusername -pyourpassword –all-databases >
    /home/LOGIN/public_html/backups/xbackup_$date.sql ; gzip /home/LOGIN/public_html/backups/xbackup_$date.sql

    for multiple backups in one day for eg : Wordpress.
    Any ideas please ?
    Thank you.


  7. 7
    Nick Humphrey dit :

    Matt, you friggin genius ! This is exactly what i needed. Thanks a lot =) Here is the mysqldump syntax reference :
    http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html

    This is how my working version of my cron script looks :
    date=`date -I` ; mysqldump -u my_username -pmyPassword my_databaseName >
    /home/myAccountUserName/public_html/dbbackup_$date.sql ;
    gzip /home/myAccountUserName/public_html/dbbackup_$date.sql

    Note the space between -u and the username.
    My webhost uses cpanel so my_username and my_databaseName can easily be found in the MySQL Databases section of the cpanel home.


  8. 8
    Matt dit :

    My pleasure Nick, I’m glad it was of help ;-)


  9. 9
    Jeff Joseph dit :

    This works on my cpanel (note the space after “-u” and you have to have two “––” before the “all-databases”) also you should re-type everything yourself - DO NOT COPY AND PASTE !!!

    date=`date -I` ; mysqldump -u USERNAME -pPASSWORD ––all-databases | gzip > /home/LOGIN/public_html/backups/xbackup_$date.sql.gz


  10. 10
    Matt dit :

    Thanks for the heads-up Jeff, I’ve updated the article to reflect the changes.


  11. 11
    Mieke Janssens dit :

    03;3;March

    Thanks !
    Any tip on how i can check if the created file indeeds restores my databases ?


  12. 12
    Matt dit :

    Hello Mieke,

    You can check your backup on a test install via PHPMyadmin : simply upload your backup and see how it goes. Or you can open the SQL file and check its data.
    I’ve been using this backup strategy for years now (one daily backup of this site every night). Works fine :)


3 trackbacks

  1. pingme: johnny's brain dump
  2. phpBBhost Supportforum :: Bekijk onderwerp - database backup via CRON job
  3. How to backup your mysql based site using CRON in cpanel - Affiliate Marketing

Laisser un commentaire



Quicktags: THINK. Then write. No SMS.

 

 

Go to: Article | Commenter!