How to get database backup in your mailbox daily automatically


ew web host do not offer a daily backup of MySQL database while few offer extra charge for automatic backup.This PHP script will help you to get database backup in your mailbox daily automatically.
This script  performs three tasks.First it creates backup of MySQL database and save it server in .sql format.In second step it compress the .sql file and convert it in .zip format.At last ,it sends an email to provided email id with attached backup.To use this script you just need to assign the values for the variables $db_host ,$db_user,$db_pass,$db_name,$tables and $email.

<?php
//File name:backup.php
//Date:20 November 2012
//By: Amit Singh<admin@megarush.net>
//Title:How to get database backup in your mailbox daily automatically

//settings for MySQL database
//change these variables before using
$db_host=”localhost“;   //mysql host
$db_user=”rashed“;  //databse user name
$db_pass=”dbpassword“;  //database password
$db_name=”dbname“;  //database name
$tables=”tbl_name“;   // use * for all tables or use , to seperate table names
$email=”gsivaprabu@gmail.com“;     //your email id
///////////////////////////////////////////////////////////////////////////////////////////

/////////don’t need to change bellow //////

backup($db_host,$db_user,$db_pass,$db_name,$tables,$email);
function backup($db_host,$db_user,$db_pass,$db_name,$tables = ‘*’,$email)
{
 
  $con= mysql_connect($db_host,$db_user,$db_pass);
  mysql_select_db($db_name,$con);
 
  //get all of the tables
  if($tables == ‘*’)
  {
    $tables = array();
    $result = mysql_query(‘SHOW TABLES’);
    while($row = mysql_fetch_row($result))
    {
      $tables[] = $row[0];
    }
  }
  else
  {
    $tables = is_array($tables) ? $tables : explode(‘,’,$tables);
  }
 
  //cycle through
  foreach($tables as $table)
  {
    $result = mysql_query(‘SELECT * FROM ‘.$table);
    $num_fields = mysql_num_fields($result);
  
    $return.= ‘DROP TABLE ‘.$table.’;’;
    $row2 = mysql_fetch_row(mysql_query(‘SHOW CREATE TABLE ‘.$table));
    $return.= “nn”.$row2[1].”;nn”;
  
    for ($i = 0; $i < $num_fields; $i++)
    {
      while($row = mysql_fetch_row($result))
      {
        $return.= ‘INSERT INTO ‘.$table.’ VALUES(‘;
        for($j=0; $j<$num_fields; $j++)
        {
          $row[$j] = addslashes($row[$j]);
          $row[$j] = ereg_replace(“n”,”\n”,$row[$j]);
          if (isset($row[$j])) { $return.= ‘”‘.$row[$j].'”‘ ; } else { $return.= ‘””‘; }
          if ($j<($num_fields-1)) { $return.= ‘,’; }
        }
        $return.= “);n”;
      }
    }
    $return.=”nnn”;
  }
 
  //save file
  $filename=’db-backup-‘.time().’-‘.(md5(implode(‘,’,$tables))).’.sql’;
  $handle = fopen($filename,’w+’);
  fwrite($handle,$return);
  fclose($handle);
  compress($filename);
  send_mail($filename.”.zip”,$email);
}

function send_mail($filepath,$email)
{

$from = “Backup <admin@megarush.net>”;
$subject = “Database backup for your website”;
$message=”This attachment contains the backup of your database.”;
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = “backup.zip”;

//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode(file_get_contents($filepath)));

// main header
$headers  = “From: “.$from.$eol;
$headers .= “MIME-Version: 1.0”.$eol;
$headers .= “Content-Type: multipart/mixed; boundary=””.$separator.”””;

// no more headers after this, we start the body! //

$body = “–“.$separator.$eol;
$body .= “Content-Transfer-Encoding: 7bit”.$eol.$eol;
$body .= “This is a MIME encoded message.”.$eol;

// message
$body .= “–“.$separator.$eol;
$body .= “Content-Type: text/html; charset=”iso-8859-1″”.$eol;
$body .= “Content-Transfer-Encoding: 8bit”.$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= “–“.$separator.$eol;
$body .= “Content-Type: application/octet-stream; name=””.$filename.”””.$eol;
$body .= “Content-Transfer-Encoding: base64”.$eol;
$body .= “Content-Disposition: attachment”.$eol.$eol;
$body .= $attachment.$eol;
$body .= “–“.$separator.”–“;

// send message
if (mail($email, $subject, $body, $headers)) {
    echo “Your backup sent to your emmail id”;
} else {
    echo “Oops mail can not be send”;
}
}
function compress($filepath)
    
     {
        
         $zip = new ZipArchive();
  $file=$filepath.”.zip”;
 if($zip->open($file,1?ZIPARCHIVE::OVERWRITE:ZIPARCHIVE::CREATE)===TRUE)
 {
   // Add the files to the .zip file
   $zip->addFile($filepath);
 
   // Closing the zip file
   $zip->close();
 }
     }

Thanks to :- http://megarush.net