Backup MySQL database & email results using PHP



	Quickly and easily backup your MySQL database and have the
	tgz emailed to you.

	You need PEAR installed with the Mail and Mail_Mime packages
	installed.  Read more about PEAR here:

	This will work in any *nix enviornment.  Make sure you have
	write access to your /tmp directory.



	// location of your temp directory
	$tmpDir = "/tmp/";
	// username for MySQL
	$user = "root";
	// password for MySQl
	$password = "pass";
	// database name to backup
	$dbName = "db";
	// the zip file emailed to you will have this prefixed
	$prefix = "db_";

	// email settings...
	$to = "";
	$from = "";
	$subject = "db - backup";
	$sqlFile = $tmpDir.$'Y_m_d').".sql";
	$attachment = $tmpDir.$'Y_m_d').".tgz";

	$creatBackup = "mysqldump -u ".$user." --password=".$password." ".$dbName." > ".$sqlFile;
	$createZip = "tar cvzf $attachment $sqlFile";

	$headers = array('From'    => $from, 'Subject' => $subject);
	$textMessage = $attachment;
	$htmlMessage = "";

	$mime = new Mail_Mime("\n");
	$mime->addAttachment($attachment, 'text/plain');
	$body = $mime->get();
	$hdrs = $mime->headers($headers);
	$mail = &Mail::factory('mail');
	$mail->send($to, $hdrs, $body);



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s