php 实现完整备份数据库,或者备份数据库指定表的类

前端之家收集整理的这篇文章主要介绍了php 实现完整备份数据库,或者备份数据库指定表的类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP类实现完整备份数据库,或者备份数据库中指定表感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!
  1. /**
  2. * PHP类实现完整备份数据库,或者备份数据库中指定表
  3. *
  4. * @param
  5. * @arrange 512-笔记网: www.jb51.cc
  6. **/
  7. <?PHP
  8. class Backup
  9. {
  10. /**
  11. * @var stores the options
  12. */
  13. var $config;
  14. /**
  15. * @var stores the final sql dump
  16. */
  17. var $dump;
  18. /**
  19. * @var stores the table structure + inserts for every table
  20. */
  21. var $struktur = array();
  22. /**
  23. * @var zip file name
  24. */
  25. var $datei;
  26. /**
  27. * this function is the constructor and phrase the options
  28. * and connect to the database
  29. * @return
  30. */
  31. public function Backup($options)
  32. {
  33. // write options
  34. foreach($options AS $name => $value)
  35. {
  36. $this->config[$name] = $value;
  37. }
  38. // check MysqL connection
  39. MysqL_connect($this->config['MysqL'][0],$this->config['MysqL'][1],$this->config['MysqL'][2]) or die(MysqL_error());
  40. MysqL_select_db($this->config['MysqL'][3]) or die(MysqL_error());
  41. }
  42. /**
  43. * this function start the backup progress its the core function
  44. * @return
  45. */
  46. public function backupDB()
  47. {
  48. // start backup
  49. if(isset($_POST['backup']))
  50. {
  51. // check if tables are selected
  52. if(empty($_POST['table']))
  53. {
  54. die("Please select a table.");
  55. }
  56. /** start backup **/
  57. $tables = array();
  58. $insert = array();
  59. $sql_statement = '';
  60. // lock tables
  61. foreach($_POST['table'] AS $table)
  62. {
  63. MysqL_query("LOCK TABLE $table WRITE");
  64. // Read table structure
  65. $res = MysqL_query('SHOW CREATE TABLE '.$table.'');
  66. $createtable = MysqL_result($res,1);
  67. $str = "\n\n".$createtable."\n\n";
  68. array_push($tables,$str);
  69. // Read table "inserts"
  70. $sql = 'SELECT * FROM '.$table;
  71. $query = MysqL_query($sql) or die(MysqL_error());
  72. $feld_anzahl = MysqL_num_fields($query);
  73. $sql_statement = '--
  74. -- Data Table `$table`
  75. --
  76. ';
  77. // start reading progress
  78. while($ds = MysqL_fetch_object($query)){
  79. $sql_statement .= 'INSERT INTO `'.$table.'` (';
  80. for ($i = 0;$i <$feld_anzahl;$i++){
  81. if ($i ==$feld_anzahl-1){
  82. $sql_statement .= MysqL_field_name($query,$i);
  83. } else {
  84. $sql_statement .= MysqL_field_name($query,$i).',';
  85. }
  86. }
  87. $sql_statement .= ') VALUES (';
  88. for ($i = 0;$i <$feld_anzahl;$i++){
  89. $name = MysqL_field_name($query,$i);
  90. if (empty($ds->$name)){
  91. $ds->$name = 'NULL';
  92. }
  93. if ($i ==$feld_anzahl-1){
  94. $sql_statement .= '"'.$ds->$name.'"';
  95. } else {
  96. $sql_statement .= '"'.$ds->$name.'",';
  97. }
  98. }
  99. $sql_statement .= ");\n";
  100. }
  101. // insert "Inserts" into an array if not exists
  102. if(!in_array($sql_statement,$insert))
  103. {
  104. array_push($insert,$sql_statement);
  105. unset($sql_statement);
  106. }
  107. unset($sql_statement);
  108. }
  109. // put table structure and inserts together in one var
  110. $this->struktur = array_combine($tables,$insert);
  111. // create full dump
  112. $this->createDUMP($this->struktur);
  113. // create zip file
  114. $this->createZIP();
  115. /** end backup **/
  116. // send an email with the sql dump
  117. if(isset($this->config['email']) && !empty($this->config['email']))
  118. {
  119. $this->sendEmail();
  120. }
  121. // output
  122. echo '<h3 style="color:green;">Backup war erfolgreich</h3><a href="'.$this->datei.'">Download Backup</a>
  123. <br />
  124. <br />';
  125. }
  126. }
  127. /**
  128. * this function generate an email with attachment
  129. * @return
  130. */
  131. protected function sendEmail()
  132. {
  133. // start sending emails
  134. foreach($this->config['email'] AS $email)
  135. {
  136. $to = $email;
  137. $from = $this->config['email'][0];
  138. $message_body = "This email contains the database backup as a zip file.";
  139. $msep = strtoupper (md5 (uniqid (time ())));
  140. // set email header (only text)
  141. $header =
  142. "From: $from\r\n" .
  143. "MIME-Version: 1.0\r\n" .
  144. "Content-Type: multipart/mixed; boundary="$msep"\r\n\r\n" .
  145. "--$msep\r\n" .
  146. "Content-Type: text/plain\r\n" .
  147. "Content-Transfer-Encoding: 8bit\r\n\r\n" .
  148. $message_body . "\r\n";
  149. // file name
  150. $dateiname = $this->datei;
  151. // get filesize of zip file
  152. $dateigroesse = filesize ($dateiname);
  153. // open file to read
  154. $f = fopen ($dateiname,"r");
  155. // save content
  156. $attached_file = fread ($f,$dateigroesse);
  157. // close file
  158. fclose ($f);
  159. // create attachment
  160. $attachment = chunk_split (base64_encode ($attached_file));
  161. // set attachment header
  162. $header .=
  163. "--" . $msep . "\r\n" .
  164. "Content-Type: application/zip; name='Backup'\r\n" .
  165. "Content-Transfer-Encoding: base64\r\n" .
  166. "Content-Disposition: attachment; filename='Backup.zip'\r\n" .
  167. "Content-Description: MysqL Datenbank Backup im Anhang\r\n\r\n" .
  168. $attachment . "\r\n";
  169. // mark end of attachment
  170. $header .= "--$msep--";
  171. // eMail Subject
  172. $subject = "Database Backup";
  173. // send email to emails^^
  174. if(mail($to,$subject,'',$header) == FALSE)
  175. {
  176. die("The email could not be sent. Please check the email address.");
  177. }
  178. echo "<p><small>Email was successfully sent.</small></p>";
  179. }
  180. }
  181. /**
  182. * this function create the zip file with the database dump and save it on the ftp server
  183. * @return
  184. */
  185. protected function createZIP()
  186. {
  187. // Set permissions to 777
  188. chmod($this->config['folder'],0777);
  189. // create zip file
  190. $zip = new ZipArchive();
  191. // Create file name
  192. $this->datei = $this->config['folder'].$this->config['MysqL'][3]."_".date("j_F_Y_g:i_a").".zip";
  193. // Checking if file could be created
  194. if ($zip->open($this->datei,ZIPARCHIVE::CREATE)!==TRUE) {
  195. exit("cannot open <".$this->datei.">\n");
  196. }
  197. // add MysqL dump to zip file
  198. $zip->addFromString("dump.sql",$this->dump);
  199. // close file
  200. $zip->close();
  201. // Check whether file has been created
  202. if(!file_exists($this->datei))
  203. {
  204. die("The ZIP file could not be created.");
  205. }
  206. echo "<p><small>The zip was created.</small></p>";
  207. }
  208. /**
  209. * this function create the full sql dump
  210. * @param object $dump
  211. * @return
  212. */
  213. protected function createDUMP($dump)
  214. {
  215. $date = date("F j,Y,g:i a");
  216. $header = <<<HEADER
  217. -- sql Dump
  218. --
  219. -- Host: {$_SERVER['HTTP_HOST']}
  220. -- Erstellungszeit: {$date}
  221. --
  222. -- Datenbank: `{$this->config['MysqL'][3]}`
  223. --
  224. -- --------------------------------------------------------
  225. HEADER;
  226. foreach($dump AS $name => $value)
  227. {
  228. $sql .= $name.$value;
  229. }
  230. $this->dump = $header.$sql;
  231. }
  232. /**
  233. * this function displays the output form to select tables
  234. * @return
  235. */
  236. public function outputForm()
  237. {
  238. // select all tables from database
  239. $result = MysqL_list_tables($this->config['MysqL'][3]);
  240. $buffer = '
  241. <fieldset>
  242. <legend>Select some tables</legend>
  243. <form method="post" action="">
  244. <select name="table[]" multiple="multiple" size="30">';
  245. while($row = MysqL_fetch_row($result))
  246. {
  247. $buffer .= '<option value="'.$row[0].'">'.$row[0].'</option>';
  248. }
  249. $buffer .= '</select>
  250. <br /><br />
  251. <input type="submit" name="backup" value="Backup Tables" />
  252. </form>
  253. </fieldset>';
  254. echo $buffer;
  255. }
  256. }
  257. ?>
  258. /*** 来自编程之家 jb51.cc(jb51.cc) ***/

猜你在找的PHP相关文章