Posts Tagged ‘Database Dump’

Dumping The MySQL database using PHP

If your web hosting provider does not keep the daily backups of your MySQL databases, then you need to think of taking the daily backups to avoid the risk of data loss. Backing up the database is very simple process if your do that using PhpMyAdmin or MYSQL administrator. However, this manual process sometimes may make you feel bored doing the same task daily. Hence, it is better to create the web page that takes the backups automatically. You just need to visit the backup generation page once a day. And it will dump the mysql backup for you. Here is the sample script that dumps the MYSQL database.

<?php
//~ Must edited values

//~ MySQL
$SQL_USER=’username’;
$SQL_PASS=’password’;
$SQL_DB=’database’;

//~ * Back up mail
$MAIL=’your@email.com’;
$SUBJECT=’backup’;
$MESSAGE=’backup’;

//~ * MySQL dump, then zip.
shell_exec(“mysqldump -u $SQL_USER -p$SQL_PASS $SQL_DB $SQL_DB.sql”);
shell_exec(“zip $SQL_DB $SQLDB.sql”);

//~ * Send mail with attachment.
$OB=’—-=_OuterBoundary_000?;
$IB=’—-=_InnerBoundery_001?;
$headers=”From: $MAIL\r\n”
.”MIME-Version: 1.0\r\n”
.”Content-Type: multipart/mixed;\n\tboundary=\”$OB\”\n”;
$Msg=”This is a multi-part message in MIME format.\n”
.”\n–$OB\n”
.”Content-Type: multipart/alternative;\n\tboundary=\”$IB\”\n\n”
.”\n–$IB\n”
.”Content-Type: text/plain;\n\tcharset=\”iso-8859-1\”\n”
.”Content-Transfer-Encoding: quoted-printable\n\n”
.$MESSAGE
.”\n–$IB–\n”
. “\n–$OB\n”
.”Content-Type: application/octet-stream;\n\tname=www.zip\n”
.”Content-Transfer-Encoding: base64\n”
.”Content-Disposition: attachment;\n\tfilename=www.zip\n\n”
.chunk_split(base64_encode(file_get_contents($SQL_DB.’.zip’)))
.”\n\n”
.”\n–$OB–\n”;
mail($MAIL, $SUBJECT, $Msg, $headers);
//~ * Completed message
echo ‘done’;
?>