前端之家收集整理的这篇文章主要介绍了
php 实现完整备份数据库,或者备份数据库指定表的类,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对
PHP类实现完整备份
数据库,或者备份
数据库中指定表感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!
/**
* PHP类实现完整备份数据库,或者备份数据库中指定表
*
* @param
* @arrange 512-笔记网: www.jb51.cc
**/
<?PHP
class Backup
{
/**
* @var stores the options
*/
var $config;
/**
* @var stores the final sql dump
*/
var $dump;
/**
* @var stores the table structure + inserts for every table
*/
var $struktur = array();
/**
* @var zip file name
*/
var $datei;
/**
* this function is the constructor and phrase the options
* and connect to the database
* @return
*/
public function Backup($options)
{
// write options
foreach($options AS $name => $value)
{
$this->config[$name] = $value;
}
// check MysqL connection
MysqL_connect($this->config['MysqL'][0],$this->config['MysqL'][1],$this->config['MysqL'][2]) or die(MysqL_error());
MysqL_select_db($this->config['MysqL'][3]) or die(MysqL_error());
}
/**
* this function start the backup progress its the core function
* @return
*/
public function backupDB()
{
// start backup
if(isset($_POST['backup']))
{
// check if tables are selected
if(empty($_POST['table']))
{
die("Please select a table.");
}
/** start backup **/
$tables = array();
$insert = array();
$sql_statement = '';
// lock tables
foreach($_POST['table'] AS $table)
{
MysqL_query("LOCK TABLE $table WRITE");
// Read table structure
$res = MysqL_query('SHOW CREATE TABLE '.$table.'');
$createtable = MysqL_result($res,1);
$str = "\n\n".$createtable."\n\n";
array_push($tables,$str);
// Read table "inserts"
$sql = 'SELECT * FROM '.$table;
$query = MysqL_query($sql) or die(MysqL_error());
$feld_anzahl = MysqL_num_fields($query);
$sql_statement = '--
-- Data Table `$table`
--
';
// start reading progress
while($ds = MysqL_fetch_object($query)){
$sql_statement .= 'INSERT INTO `'.$table.'` (';
for ($i = 0;$i <$feld_anzahl;$i++){
if ($i ==$feld_anzahl-1){
$sql_statement .= MysqL_field_name($query,$i);
} else {
$sql_statement .= MysqL_field_name($query,$i).',';
}
}
$sql_statement .= ') VALUES (';
for ($i = 0;$i <$feld_anzahl;$i++){
$name = MysqL_field_name($query,$i);
if (empty($ds->$name)){
$ds->$name = 'NULL';
}
if ($i ==$feld_anzahl-1){
$sql_statement .= '"'.$ds->$name.'"';
} else {
$sql_statement .= '"'.$ds->$name.'",';
}
}
$sql_statement .= ");\n";
}
// insert "Inserts" into an array if not exists
if(!in_array($sql_statement,$insert))
{
array_push($insert,$sql_statement);
unset($sql_statement);
}
unset($sql_statement);
}
// put table structure and inserts together in one var
$this->struktur = array_combine($tables,$insert);
// create full dump
$this->createDUMP($this->struktur);
// create zip file
$this->createZIP();
/** end backup **/
// send an email with the sql dump
if(isset($this->config['email']) && !empty($this->config['email']))
{
$this->sendEmail();
}
// output
echo '<h3 style="color:green;">Backup war erfolgreich</h3><a href="'.$this->datei.'">Download Backup</a>
<br />
<br />';
}
}
/**
* this function generate an email with attachment
* @return
*/
protected function sendEmail()
{
// start sending emails
foreach($this->config['email'] AS $email)
{
$to = $email;
$from = $this->config['email'][0];
$message_body = "This email contains the database backup as a zip file.";
$msep = strtoupper (md5 (uniqid (time ())));
// set email header (only text)
$header =
"From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary="$msep"\r\n\r\n" .
"--$msep\r\n" .
"Content-Type: text/plain\r\n" .
"Content-Transfer-Encoding: 8bit\r\n\r\n" .
$message_body . "\r\n";
// file name
$dateiname = $this->datei;
// get filesize of zip file
$dateigroesse = filesize ($dateiname);
// open file to read
$f = fopen ($dateiname,"r");
// save content
$attached_file = fread ($f,$dateigroesse);
// close file
fclose ($f);
// create attachment
$attachment = chunk_split (base64_encode ($attached_file));
// set attachment header
$header .=
"--" . $msep . "\r\n" .
"Content-Type: application/zip; name='Backup'\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-Disposition: attachment; filename='Backup.zip'\r\n" .
"Content-Description: MysqL Datenbank Backup im Anhang\r\n\r\n" .
$attachment . "\r\n";
// mark end of attachment
$header .= "--$msep--";
// eMail Subject
$subject = "Database Backup";
// send email to emails^^
if(mail($to,$subject,'',$header) == FALSE)
{
die("The email could not be sent. Please check the email address.");
}
echo "<p><small>Email was successfully sent.</small></p>";
}
}
/**
* this function create the zip file with the database dump and save it on the ftp server
* @return
*/
protected function createZIP()
{
// Set permissions to 777
chmod($this->config['folder'],0777);
// create zip file
$zip = new ZipArchive();
// Create file name
$this->datei = $this->config['folder'].$this->config['MysqL'][3]."_".date("j_F_Y_g:i_a").".zip";
// Checking if file could be created
if ($zip->open($this->datei,ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <".$this->datei.">\n");
}
// add MysqL dump to zip file
$zip->addFromString("dump.sql",$this->dump);
// close file
$zip->close();
// Check whether file has been created
if(!file_exists($this->datei))
{
die("The ZIP file could not be created.");
}
echo "<p><small>The zip was created.</small></p>";
}
/**
* this function create the full sql dump
* @param object $dump
* @return
*/
protected function createDUMP($dump)
{
$date = date("F j,Y,g:i a");
$header = <<<HEADER
-- sql Dump
--
-- Host: {$_SERVER['HTTP_HOST']}
-- Erstellungszeit: {$date}
--
-- Datenbank: `{$this->config['MysqL'][3]}`
--
-- --------------------------------------------------------
HEADER;
foreach($dump AS $name => $value)
{
$sql .= $name.$value;
}
$this->dump = $header.$sql;
}
/**
* this function displays the output form to select tables
* @return
*/
public function outputForm()
{
// select all tables from database
$result = MysqL_list_tables($this->config['MysqL'][3]);
$buffer = '
<fieldset>
<legend>Select some tables</legend>
<form method="post" action="">
<select name="table[]" multiple="multiple" size="30">';
while($row = MysqL_fetch_row($result))
{
$buffer .= '<option value="'.$row[0].'">'.$row[0].'</option>';
}
$buffer .= '</select>
<br /><br />
<input type="submit" name="backup" value="Backup Tables" />
</form>
</fieldset>';
echo $buffer;
}
}
?>
/*** 来自编程之家 jb51.cc(jb51.cc) ***/
原文链接:https://www.f2er.com/php/528173.html