#native_company# #native_desc#
#native_cta#

mydbackup

By James Davis
on May 23, 2005

Version: 1.0

Type: Full Script

Category: Databases

License: GNU General Public License

Description: This is a backup script for MySQL using the mysqldump command. It was designed to be used with crontab to automate a database backup. It can be configured to switch on certain command line options that control the output of the backup.

<?php
/*

  **************************************************************
  *                    mydbackup.php 1.0                       *
  **************************************************************
  
  mydbackup.php is a utility which can be used to backup a MySQL
  database.  It requires PHP and of course, MySQL.  It can be
  used with crontab to automate a backup. For instance, the 
  following will run every Sunday at midnight.
  
  Edit the crontab, type:
    [foo@bar]$ crontab -e
    
  Insert the following line:
    0 0 * * 0 php -f /path/to/mydbackup.php
  
  If you are on a shared webhosting service, you probably need 
  to add to the crontab using Webmin or Cpanel.  This can also
  be loaded into a browser to run manually, or included into
  another PHP file.
  
  Copyright (C) 2005 James Davis <[email protected]>

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You can get a copy of the GNU General Public License from this
  address: http://www.gnu.org/copyleft/gpl.html#SEC1
  You can also write to the Free Software Foundation, Inc., 59 Temple
  Place - Suite 330, Boston, MA  02111-1307, USA.

===================================================================================================
Set error reporting off for security*/

error_reporting(0);

/*=================================================================================================
Configuration parameters ** THESE MUST BE SET PRIOR TO USE **/

$_MYPARMS = array();
$_MYPARMS['usrname']        = '';                        //MySQL username.
$_MYPARMS['pswd']           = '';                        //MySQL password.
$_MYPARMS['host']           = 'localhost';               //Host name.
$_MYPARMS['db_name']        = '';                        //Database name.

/*=================================================================================================
Optional parameters.*/

$_MYPARMS['path']           = '';                         //Path to mysqldump - if the bin directory 
                                                          //is not set in your path you will need to 
                                                          //set this variable.
$_MYPARMS['bak_dir']        = '';                         //Backup directory, use a preexisting folder,
                                                          //or leave blank to use same directory as 
                                                          //mybackup.php.
$_MYPARMS['use_gzip']      = FALSE;                       //Set to TRUE to gzip backup files.

/*=================================================================================================
Optional command line options.*/

$_MYPARMS['opts']['--add-locks']        = FALSE;          //Set to TRUE to add lock table statements.
$_MYPARMS['opts']['--skip-comment']     = FALSE;          //Set to TRUE to turn off commenting.
$_MYPARMS['opts']['--add-drop-table']   = FALSE;          //Set to TRUE to add drop table statements
$_MYPARMS['opts']['--complete-insert']  = FALSE;          //Set to TRUE to add complete inserts.
$_MYPARMS['opts']['--extended-insert']  = FALSE;          //Set to TRUE to use exetended inserts.
$_MYPARMS['opts']['--lock-tables']      = FALSE;          //Set to TRUE to lock tables during dump.
$_MYPARMS['opts']['--no-create-info']   = FALSE;          //Set to TRUE to suppress CREATE TABLE 
                                                          //statements. 

/*==================================================================================================
Do not alter any code beyond this point.*/

// Initialize variables.
$output = '';
$timestmp = date('m.d.Y-Hi');
$options = '';

foreach($_MYPARMS['opts'] as $key => $value)
{
     if ($value == TRUE)
     {     
          $options = $options." ".$key;`    
     }
}

// Create filename with path info (ex. /path/to/db_foobar-2005.05.19-1200.sql)     
$filename = "{$_MYPARMS['bak_dir']}{$_MYPARMS['db_name']}-backup-$timestmp.sql";

// Execute mysqldump and write data to the $lines array          
exec("{$_MYPARMS['path']}mysqldump --user={$_MYPARMS['usrname']} --password={$_MYPARMS['pswd']} --host={$_MYPARMS['host']}$options {$_MYPARMS['db_name']}", $lines, $retval);

// Roll $lines into $output.
foreach($lines as $line)
{
     $output .= $line."n";
}

// Create Backup file and write data to it.
if ($retval != 1)
{
     if($_MYPARMS['use_gzip'] == TRUE)
     {
          $filename = $filename.".gz";
          if(!$handle = gzopen($filename,"wb6"))
          { 
               print("Error: mysqldump cannot write to dump file");
          }
          else
          {
               gzdeflate($output, 6);
               gzwrite($handle, $output);
          }
     
          gzclose($handle);
     }
     else
     {
          if(!$handle = fopen($filename,"wb"))
          {
               print("Error: mysqldump cannot write to dump file");
          }
          else
          {
               fwrite($handle, $output);
          }
          
          fclose($handle);
     }
}
else
{
     print("Error: please check configuration for errors.<br>");
}
?>