Automating MySQL Backups with Cron

Devolio

Search

What I'm Doing

    Thursday, November 15. 2007

    Automating MySQL Backups with Cron

    Thousands of web applications use MySQL for their databases, and almost all of them store some sort of valuable information. You never know when a hard drive will fail, a server will die, or in the absolute worst case, when someone will find a vulnerability that allows them to delete your databases. Sometimes we just take for granted how well everything just... works. We're going to look at two simple ways to backup your databases, and how to automate them using crontabs.
    First, we're going to set up the command or script that will back up the database(s). There's a few things you're going to need to configure before you start backing up, so let's get to it...

    There are a few different ways you can do these backups, so I've provided a method to use both PHP (requires php-cli) and a regular Linux shell command with MySQL to do the same thing.

    Using the command line

    You can use the following command to back up your MySQL database.:

    mysqldump -uusername -ppassword dbname | gzip > /home/username/folder/mysql_backup.gz

    You'll have to set the username, password, dbname, and path to save in. Also note, the -u and -p preceding the username and password must be there, or it will not work correctly.

    To run it automatically, save your configured command from above into a file named anything you want, with as a shell script (.sh). We'll talk about automating them in just a minute, but for a little more flexibility, I prefer...

    Using a PHP Script

    You can also back up your databases using a PHP script. Setting it up takes a little longer, but it is more configurable if you aren't as proficient with shell scripting. You can download the PHP Class (Based on MySQLDump - written by Daniele Vigan, GPL) To configure the PHP script, you'll have to do the following:

    Set your database settings) All of this information is available in your hosting providers Control Panel, or can be managed easily using phpMyAdmin or a similar MySQL management program. If you're not sure about the host, you can leave it as localhost most of the time.
    /* Database details */
    $dbname = "databasename";
    $dbhost = "localhost";
    $dbuser = "user";
    $dbpw = "pass";

    Compress backed up files) If you want to compress the backups that you make, you can set this to true to gzip the files before saving them.
    /* Compress as gzip? */
    $compression = false;

    Dump as hex) Choose if you want to dump the database as hex.
    /* Dump in hex? */
    $hex = false;

    Filename to back up to) Set the filename that you want to save the backup as. You don't have to put an extension on the end of it. You can also select if you want to add a date and timestamp in the filename. I would recommend this, as it makes keeping them together easy, while still being able to sort find them fairly quickly.
    /* Filename (No extension) */
    $filename = "filename";

    /* add date/timestamp to filename? */
    $filedate = true;

    Keep in mind, (mentioned by resmo in the comments) that if your database is bigger than PHP's default memory limit of 8MB, you'll have to change your php.ini, or add ini_set ( "memory_limit", "20M") to your script to change the limit. Once you've set all of those details, I would highly suggest that you test it out a few times, to make sure that it is dumping all of the information correctly and to your preferences, and then we can proceed with automating your MySQL backups.

    Automating with cPanel, Plesk, or similar


    Now, if you're using a host that provides you with a program like cPanel or Plesk, you'll have an area in your control panel for setting up crontabs. Here's an example of how cPanel handles crontabs:

    Adding backup scripts in crontabs

    If you're going to run the command version of the backup, you can paste your configured command into the command field, choose when you want it to run, and click go. If you're running the PHP version, you must first copy your files into a directory, and then enter php /home/username/folder/script.php for the command. Remember, the PHP version will only work with php-cli installed on the server.

    Automating without a cPanel (crontabs)


    If your hosting provider does not provide you with a Control Panel, and you have shell access, you can set crontabs using the command line. (using crontabs)

    Should you choose to go the PHP route, I would advise that you make sure nobody but that machine can access that folder, so that nobody can download your backups. (covered briefly here).

    Now you can be certain that your MySQL databases are backed up properly – as often as you'd like. How do you keep your databases backed up? How often have do you usually back up your databases? Hopefully you'll never have to go through the pain of losing a whole database as I have in the past. Be safe, be smart, keep regular backups in multiple locations.


    Trackbacks

    No Trackbacks

    Comments
    Display comments as (Linear | Threaded)

    #1 - resmo said:
    2007-11-16 02:42 - (Reply)

    When using the php script, remember that php has a default memory limit of 8 mb configured. if your mysql backup data is more than 8 mb, you'll have to extend memory_limit in php.ini or copy and paste ini_set ( "memory_limit", "20M") in your script.

    #1.1 - Joey said:
    2007-11-16 15:16 - (Reply)

    That's definitely a good thing to remember, resmo. Added into the article.

    #2 - Michigan Programmers said:
    2007-11-16 14:14 - (Reply)

    Whoa .. I read you lost a whole database. My heart feels for you! I use MySQLADMIN .. but I should really put it in a CRON job like you said.
    Mary

    #3 - güzel sözler said:
    2008-01-16 22:08 - (Reply)

    Thanks for the nice read

    #4 - Anonymous 2008-02-29 19:11 - (Reply)

    How about this instead for a cron'd twice a day mysql dump?

    mysqldump -Q -uuser -ppw dbname | gzip -9 > /mysql/dump/location/mysql-dbname.$(date '+%a%p').dump.gz

    -Q quotes field names (in case you have someone naming a field "key" or "group," not that anyone really does that, right?)

    The $(date '+%a%p') part gives you the 3 letter day of the week (Mon, Tue, etc.) and the %p gives you AM and PM.

    Since these values repeat every week, you have at most 14 files at any given time. If a backup is corrupted, you still have the other 13.

    mysql-dbname.MonAM.dump.gz
    mysql-dbname.MonPM.dump.gz
    mysql-dbname.TueAM.dump.gz
    ...

    #5 - Zerik 2008-05-13 10:59 - (Reply)

    it works great, just one thing, it doesnt make any gzip files although i set that parameter to true.

    do i need some library to have it working?

    thanks.


    Add Comment

    Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
    Standard emoticons like :-) and ;-) are converted to images.
    E-Mail addresses will not be displayed and will only be used for E-Mail notifications