Mailing MySql Backup from a Website

Facebooktwittergoogle_pluslinkedinmail

Recently I was working with a website who requires their  database backup generation  on a regular interval basis. Summing up many articles , I came to the solution.

1.First this I have to do dumping  sql  export from mysql database.

2. Next I have save it to a file.

3.Third part is attaching this file to a email and send email.

4.Final thing is set a cron job to send this email on regular interval.

Export MySql Database

<?php ob_start();
$fileName=date("Y-m-d")."_databasename".".sql";

$username = "db_login"; 
$password = "db_pswd"; 
$hostname = "db_host"; 
$dbname   = "db_name";
 
//$_SERVER['MYSQL_HOME']  helps finding path 
$command = $_SERVER['MYSQL_HOME'] ."\\mysqldump --add-drop-table --host=".$hostname." --user=".$username ." ";
if ($password) 
        $command.= "--password=". $password ." "; 
$command.= $dbname;
system($command);
?>

Save It To a File

We can output into file with command.But sometimes there are issues like creates emty back up file and so we created file and write exported sql in that.

<?php $exportcontent = ob_get_contents(); 
ob_end_clean();
 
$backup = fopen($fileName, "w") or die("Unable to open file!");

fwrite($backup, $exportcontent );

fclose($backup);
?>

Mail MysSql Backup File

We will use PHPMailer to send mail.You can download it from GitHUB. Place PHPMailer library inside project and include class.phpmailer.php.  Code will look like this…

 <?php //mailing
require_once('PHPMailer/class.phpmailer.php');


$email = new PHPMailer();
$email->From      = 'fromemail';
$email->FromName  = 'xyz';
$email->Subject   = 'Db back Up of  website';
$email->Body      = "Please find the attachment";
$email->AddAddress( "to email");
$email->addCC("cc email");


$file_to_attach = $fileName;

$email->AddAttachment($file_to_attach);

$email->Send();
unlink($fileName); //deleting back up file from server
?>
  

Final Code is Like This….

<?php ob_start();
$fileName=date("Y-m-d")."_databasename".".sql";

$username = "db_login"; 
$password = "db_pswd"; 
$hostname = "db_host"; 
$dbname   = "db_name";
 
//$_SERVER['MYSQL_HOME']  helps finding path 
$command = $_SERVER['MYSQL_HOME'] ."\\mysqldump --add-drop-table --host=".$hostname." --user=".$username ." ";
if ($password) 
        $command.= "--password=". $password ." "; 
$command.= $dbname;
system($command);

$exportcontent = ob_get_contents(); 
ob_end_clean();
 
$backup = fopen($fileName, "w") or die("Unable to open file!");

fwrite($backup, $exportcontent );

fclose($backup);
//mailing
require_once('PHPMailer/class.phpmailer.php');

$email = new PHPMailer();
$email->From      = 'fromemail';
$email->FromName  = 'xyz';
$email->Subject   = 'Db back Up of  website';
$email->Body      = "Please find the attachment";
$email->AddAddress( "to email");
$email->addCC("cc email");
$file_to_attach = $fileName;
$email->AddAttachment($file_to_attach);
$email->Send();
unlink($fileName); //deleting back up file from server
?>
 

Now Set a Cron Job From Cpanel

Now save this code in a file and execute it on a regular interval by cron job. If set 24 hour interva ,l you can mail this back up at a particular time of the day.

 

Facebooktwittergoogle_pluslinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *