php数据库备份类程序代码
今天没事收集了两款php数据库备份程序,这里可以完成功能有:1.备份指定数据表、2.打包成zip文件、3.发送到指定邮箱地址,基本功能就这些了.
下面看下使用方法,代码如下:
- <?php
- error_reporting(0);
-
- $options = array('email' => array('email1', 'email2'),
- 'folder' => './backup/',
- 'mysql' => array('localhost', 'user', 'password', 'db'));
-
- $b = new Backup($options);
-
-
- if(isset($_POST['backup']))
- {
-
- $b->backupDB();
- }
-
- $b->outputForm();
- ?>
具体类实现,代码如下:
- <?php
-
- class Backup
- {
-
-
-
- var $config;
-
-
-
-
- var $dump;
-
-
-
-
- var $struktur = array();
-
-
-
-
- var $datei;
-
-
-
-
-
-
- public function Backup($options)
- {
-
- foreach($options AS $name => $value)
- {
- $this->config[$name] = $value;
- }
-
-
- 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());
- }
-
-
-
-
-
- public function backupDB()
- {
-
- if(isset($_POST['backup']))
- {
-
- if(emptyempty($_POST['table']))
- {
- die("请选择一个数据表。");
- }
-
-
- $tables = array();
- $insert = array();
- $sql_statement = '';
-
-
- foreach($_POST['table'] AS $table)
- {
- mysql_query("LOCK TABLE $table WRITE");
-
-
- $res = mysql_query('SHOW CREATE TABLE '.$table.'');
- $createtable = mysql_result($res, 0, 1);
- $str = "nn".$createtable."nn";
-
- array_push($tables, $str);
-
-
- $sql = 'SELECT * FROM '.$table;
- $query = mysql_query($sql) or die(mysql_error());
- $feld_anzahl = mysql_num_fields($query);
-
- $sql_statement = '--
- -- Data Table `$table`
- --
- ';
-
-
- 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 (emptyempty($ds->$name)){
- $ds->$name = 'NULL';
- }
- if ($i ==$feld_anzahl-1){
- $sql_statement .= '"'.$ds->$name.'"';
- } else {
- $sql_statement .= '"'.$ds->$name.'", ';
- }
- }
- $sql_statement .= ");n";
- }
-
-
- if(!in_array($sql_statement, $insert))
- {
- array_push($insert, $sql_statement);
- unset($sql_statement);
- }
-
- unset($sql_statement);
-
- }
-
-
- $this->struktur = array_combine($tables, $insert);
-
-
- $this->createDUMP($this->struktur);
-
-
- $this->createZIP();
-
-
-
-
- if(isset($this->config['email']) && !emptyempty($this->config['email']))
- {
- $this->sendEmail();
- }
-
-
- echo '<h3 style="color:green;">备份完成啦</h3><a href="'.
- $this->datei.'">下载备份</a>
- <br />
- <br />';
- }
- }
-
-
-
-
-
- protected function sendEmail()
- {
-
- foreach($this->config['email'] AS $email)
- {
- $to = $email;
-
- $from = $this->config['email'][0];
-
- $message_body = "本邮件中包含的zip压缩包为数据库备份";
-
- $msep = strtoupper (md5 (uniqid (time ())));
-
-
- $header =
- "From: $fromrn" .
- "MIME-Version: 1.0rn" .
- "Content-Type: multipart/mixed; boundary=".$msep."rnrn" .
- "--$mseprn" .
- "Content-Type: text/plainrn" .
- "Content-Transfer-Encoding: 8bitrnrn" .
- $message_body . "rn";
-
-
- $dateiname = $this->datei;
-
-
- $dateigroesse = filesize ($dateiname);
-
-
- $f = fopen ($dateiname, "r");
-
- $attached_file = fread ($f, $dateigroesse);
-
- fclose ($f);
-
- $attachment = chunk_split (base64_encode ($attached_file));
-
-
- $header .=
- "--" . $msep . "rn" .
- "Content-Type: application/zip; name='Backup'rn" .
- "Content-Transfer-Encoding: base64rn" .
- "Content-Disposition: attachment; filename='Backup.zip'rn" .
- "Content-Description: Mysql Datenbank Backup im Anhangrnrn" .
- $attachment . "rn";
-
-
- $header .= "--$msep--";
-
-
- $subject = "数据库备份";
-
-
- if(mail($to, $subject, '', $header) == FALSE)
- {
- die("无法发送邮件,请检查邮箱地址");
- }
-
- echo "<p><small>邮件发送成功</small></p>";
- }
- }
-
-
-
-
-
- protected function createZIP()
- {
-
-
- chmod($this->config['folder'], 0777);
-
-
- $zip = new ZipArchive();
-
- $this->datei = $this->config['folder'].$this->config['mysql'][3]."_"
- .date("j_F_Y_g_i_a").".zip";
-
-
- if ($zip->open($this->datei, ZIPARCHIVE::CREATE)!==TRUE) {
- exit("无法打开 <".$this->datei.">n");
- }
-
-
- $zip->addFromString("dump.sql", $this->dump);
-
- $zip->close();
-
-
- if(!file_exists($this->datei))
- {
- die("无法生成压缩包");
- }
-
- echo "<p><small>数据库备份压缩包成功生成</small></p>";
- }
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
- public function outputForm()
- {
-
- $result = mysql_list_tables($this->config['mysql'][3]);
-
- $buffer = '
- <fieldset>
- <legend>选择需要备份的数据表</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="备份选定数据表" />
- </form>
- </fieldset>';
-
- echo $buffer;
- }
- }
-
- ?>
通用数据库备份类,代码如下:
- <?php
-
- Class Back_up_databaseextendsdbstuff{
-
- var $HOST;
- var $USERNAME;
- var $PASSWORD;
- var $DATABASE;
- function Back_up_database($host,$username,$password,$database){
-
- $this->HOST=$host;
- $this->USERNAME=$username;
- $this->ASSWORD=$password;
- $this->DATABASE=$database;
- $Connection=$this->connect($this->HOST,$this->USERNAME,$this->ASSWORD,$this->DATABASE,$pconnect);
- $this->Connection=$Connection;
- }
-
- function get_table_name($database){
- $this->Connection;
- $result=mysql_list_tables($database);
- $i=0;
- while($i<mysql_num_rows($result)){
- $tb_name[$i]=mysql_tablename($result,$i);
- $table_name.=$tb_name[$i].",";
- $i++;
- }
- $this->table_name=substr($table_name,0,-1);
- return$this->table_name;
- }
-
- function get_table_fields($table_name){
- $this->Connection;
- $createtable=dbstuff::query("SHOWCREATETABLE$table_name");
- $create=dbstuff::fetch_row($createtable);
- $tabledump.="DROPTABLEIFEXISTS$table_name;\n";
- $tabledump.=$create[1].";\n\n";
- $this->$table_name=$tabledump;
- return$this->$table_name;
- }
-
- function get_insert($table_insert_name){
- $this->Connection;
- $rows=dbstuff::query("SELECT*FROM$table_insert_name");
- $numfields=dbstuff::num_fields($rows);
- $numrows=dbstuff::num_rows($rows);
- while($row=dbstuff::fetch_row($rows)){
- $comma="";
- $tabledump.="INSERTINTO$table_insert_nameVALUES(";
- for($i=0;$i<$numfields;$i++){
- $tabledump.=$comma."'".mysql_escape_string($row[$i])."'";
- $comma=",";
- }
- $tabledump.=");\n";
- }
- $this->tabledump=$tabledump;
- return$this->tabledump;
- }
-
- function get_string($database_name,$file_path_name){
- $time=date("Y-m-dH:j");
- $date_time=date("YmdHis");
- $file_path_name=$file_path_name.$date_time.".sql";
- $version="Antsent_Web_StudioDatabaseBackUpV1.01";
- $idstring='#Identify:'.base64_encode("$time,$version")."\n";
- $head_info="$idstring".
- "#\n".
- "#Antsnet_Web!TheBasicClassOfBackUpDataBase\n".
- "#Version:AntsnetWeb!$version\n".
- "#Timetime\n".
- "#Type:ClassOfBackUpDataBase\n".
- "#Antsnet_Web_Studio!Home:http://www.111cn.net \n".
- "#PleasevisitourwebsitefornewestinfomationaboutAntsnet_Web_Studio!\n".
- "#--------------------------------------------------------\n\n\n";
- $table_name=$this->get_table_name($database_name);
- $array_table=explode(",",$table_name);
- for($i=0;$i<count($array_table);$i++){
- $table_string.=$this->get_table_fields($array_table[$i]);
- $table_insert.=$this->get_insert($array_table[$i]);
- }
- $count_string=$head_info.$table_string.$table_insert;
-
- $write_status=$this->write_file($file_path_name,$count_string);
- return$write_status;
- }
-
- function write_file($file_path,$file_contents){
- if(@!$fp=fopen($file_path,'w')){
- $status="<fontcolor=\"red\">ThisFileCouldNotOpenOrRead.</font>";
- }else{
- flock($fp,3);
- fwrite($fp,$file_contents);
- fclose($fp);
- window.google_render_ad();
- ?>
|