Backup and email your MySQL database.

Discussion in 'Site & Server Administration' started by thuankkk, Jul 21, 2009.

  1. #1
    If you encountered data loss when your web hosting has troubles or you wanna transfer site, you may think about backup your database. It's always better to have a database copy in your mail box.

    To do that, create a .php file, e.g backup.php with this code:
    
    <?php
      //~ Must edited values
      
      //~ MySQL
      $SQL_USER='username';
      $SQL_PASS='password';
      $SQL_DB='database';
    
      //~ * Back up mail
      $MAIL='your@email.com';
      $SUBJECT='backup';
      $MESSAGE='backup';
      
      //~ * MySQL dump, then zip.
      shell_exec("mysqldump -u $SQL_USER -p$SQL_PASS $SQL_DB $SQL_DB.sql");
      shell_exec("zip $SQL_DB $SQLDB.sql");
      
      //~ * Send mail with attachment.
      $OB='----=_OuterBoundary_000';
      $IB='----=_InnerBoundery_001';
      $headers="From: $MAIL\r\n"
        ."MIME-Version: 1.0\r\n"
        ."Content-Type: multipart/mixed;\n\tboundary=\"$OB\"\n";
      $Msg="This is a multi-part message in MIME format.\n"
        ."\n--$OB\n"
        ."Content-Type: multipart/alternative;\n\tboundary=\"$IB\"\n\n"
        ."\n--$IB\n"
        ."Content-Type: text/plain;\n\tcharset=\"iso-8859-1\"\n"
        ."Content-Transfer-Encoding: quoted-printable\n\n"
        .$MESSAGE
        ."\n--$IB--\n"
        . "\n--$OB\n"
        ."Content-Type: application/octet-stream;\n\tname=www.zip\n"
        ."Content-Transfer-Encoding: base64\n"
        ."Content-Disposition: attachment;\n\tfilename=www.zip\n\n"
        .chunk_split(base64_encode(file_get_contents($SQL_DB.'.zip')))
        ."\n\n"
        ."\n--$OB--\n";
      mail($MAIL, $SUBJECT, $Msg, $headers);
      //~ * Completed message
      echo 'done';
    ?>
    
    PHP:
    This script will dump out mysql database, zip it, then send it to your mailbox.
    Remember to edit must-edited values such as MySQL username, password, database, your email address.
    Recommended mailbox is GMail.

    Then, set a cron job to call that file every day, or every 2, 3, 5 days, etc.
    Go to www.setcronjob.com for a free one, if your web hosting doesn't support cron jobs feature.

    Hope this help :)
     
    thuankkk, Jul 21, 2009 IP
  2. prohost

    prohost Member

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #2
    Is this work also for php 5 with new mysql version ? Some older backup script only works with php4 / older mysql version
     
    prohost, Jul 21, 2009 IP
  3. bombaymobiles

    bombaymobiles Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for sharing.........
     
    bombaymobiles, Jul 21, 2009 IP
  4. jaysmyhero

    jaysmyhero Member

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #4
    thanks looks useful
     
    jaysmyhero, Jul 21, 2009 IP
  5. jaysmyhero

    jaysmyhero Member

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    hmm do i put this in the directory?
     
    jaysmyhero, Jul 22, 2009 IP
  6. thuankkk

    thuankkk Active Member

    Messages:
    503
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    95
    #6
    @jaysmyhero: go to your file manager, or FTP client to access your server, create file backup.php under web root, e.g /home/yourname/www, then edit and save that file with the script above. Then set a cron job to that file URL, e.g http://example.com/backup.php

    Hope this helps.
     
    thuankkk, Jul 22, 2009 IP
  7. jokarl

    jokarl Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks and i think most people dont do this. Sooner or later something will happen and I would never trust a webhotel to recover the data. I backup database weekly on all my sites and I do a filebackup now and then.
     
    jokarl, Jul 22, 2009 IP
  8. thuankkk

    thuankkk Active Member

    Messages:
    503
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    95
    #8
    You mean 'should'? :D This script will zip and then send database to your email, e.g gmail, which is always better than store it in your web hosting. My friend has some trouble with his web hosting provider, and can't take back his backup in his server.

    I back up my database every 2 hours :D However, it's small. Less than 300 KB.

    When I tested with shared host, I can't send zip file that is larger than 2 MB, so, zip and email only database can help a bit. Files and folders are rarely changed, while database is bigger and bigger.
     
    thuankkk, Jul 23, 2009 IP