Mysqldump backup class

0
Your rating: None

Here is a simple class to create backup of your mysql database and email it as an attachment.

<?php

class mysqldumpbackup
{
var $host;
var $user;
var $password;
var $dbName;
var $emailTo;
var $emailFrom;
var $subject;
var $message;
var $fileName;
var $dateNtime;

function attachment_mail()
{
$file_type = "application/octet-stream";
$file = $this->pathToAttach;
$start= strrpos("$this->pathToAttach", '/') == -1 ? strrpos("$this->pathToAttach", '//') : strrpos("$this->pathToAttach", '/')+1;
$file_name = substr($this->pathToAttach, $start, strlen($this->pathToAttach));

$email_from = $this->emailFrom;
$email_subject = $this->subject;
$email_txt = $this->message;
$email_to = $this->emailTo;

$headers = "From: ".$email_from;
$fileToOpen = fopen($file,'rb');
$data = fread($fileToOpen,filesize($file));
fclose($fileToOpen);

$email_txt = "\n\nDatabase Backup using mysqldump";

$semi_rand = md5(time());
$mime_boundary ="==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .=
"This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_txt .
"\n\n";
$data = chunk_split(base64_encode($data));
$email_message .=
"--{$mime_boundary}\n" .
"Content-Type: {$file_type};\n" .
" name=\"{$file_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .
"\n\n" .
"--{$mime_boundary}--\n";

if(mail($email_to, $email_subject, $email_message, $headers))
{
return true;
}
else
{
return false;
}
}

function create_backup()
{
$this->dateNtime = date("-F-j-Y-g:i-a");
$this->fileName = strtolower($this->dbName.$this->dateNtime);
$this->pathToAttach = "./".$this->fileName.".sql";
$createString ="mysqldump -u ".$this->user." -h ".$this->host." --password=".$this->password." ".$this->dbName." > ".$this->pathToAttach;
shell_exec("$createString");
$createArchieve = "tar cvzf ".$this->pathToAttach.".tgz ".$this->pathToAttach;
shell_exec("$createArchieve");
unlink ($this->pathToAttach);
$this->pathToAttach = $this->pathToAttach.".tgz";
}

function Mail_finally()
{
$this->create_backup();
$this->subject = "Backup of ".$this->dbName." on ".$this->dateNtime;
$this->message = "Backup of ".$this->dbName." as on ".$this->dateNtime." by user: ".$this->user;
$this->attachment_mail();
unlink ($this->pathToAttach);
}
}
?>

To use the above class just follow this simple step:

<?php

$createbackup = new mysqldumpbackup;
$createbackup->emailTo = "toemail@mail.com";//receiver email
$createbackup->emailFrom = "fromemail@mail.com";//Sender email
$createbackup->host = "localhost";//host name
$createbackup->user = "user";//mysql user name
$createbackup->password = "pasword";//mysql password
$createbackup->dbName = "mysqldb";//database name
$createbackup->Mail_finally();
?>

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.

Cumulus Tag Cloud

Kiran Says

I love work environment which is:

Informal yet professional
Demanding yet rewarding
Challenging yet inspiring
Mediocrity is not an option
Having fun is serious business
Making mistakes is human
Forgiveness is Company Policy

Travelling Sucks