Home  >  Article  >  Backend Development  >  PHP implements MYSQL backup and restore

PHP implements MYSQL backup and restore

WBOY
WBOYOriginal
2016-07-25 08:59:211080browse
  1. class dbBackup {
  2. public $host='localhost'; //Database address
  3. public $user='root'; //Login name
  4. public $pwd=''; //Password
  5. public $database; //Database name
  6. public $charset='utf8'; //Database connection encoding: mysql_set_charset
  7. }
Copy code

2. Add database connection function.

  1. /**
  2. * Connect to database...
  3. */
  4. function db() {
  5. $con = mysql_connect($this->host,$this->user,$this->pwd);
  6. if (!$con){
  7. die('Could not connect');
  8. }
  9. $db_selected = mysql_select_db($this->database, $con);
  10. if (!$db_selected) {
  11. die('Can' t use select db');
  12. }
  13. mysql_set_charset($this->charset); //Set encoding
  14. return $con;
  15. }
Copy code

3. Query database table collection

  1. /**
  2. * Table collection ...
  3. */
  4. function tblist() {
  5. $list=array();
  6. $rs=mysql_query("SHOW TABLES FROM $this->database");
  7. while ($temp=mysql_fetch_row($rs)) {
  8. $list[]=$temp[0];
  9. }
  10. return $list;
  11. }
Copy code

4. Query table structure

  1. /**

  2. *Table structure SQL...
  3. */
  4. function sqlcreate() {
  5. $sql='';
  6. $tb=$this->tblist();
  7. foreach ($ tb as $v) {
  8. $rs=mysql_query("SHOW CREATE TABLE $v");
  9. $temp=mysql_fetch_row($rs);
  10. $sql.="-- Table structure: {$temp[0]} --rn";
  11. $sql.="{$temp[1]}";
  12. $sql.=";-- --rnrn";
  13. }

  14. return $sql;

  15. }
Copy code

Note: $sql.=";-- --rnrn"; Each SQL statement must be separated by a semicolon (;) , MYSQL import can be recognized. -- -- is the program's identifier for splitting SQL statements. It can be customized but must be a comment statement, otherwise it will affect the SQL statement. rn has no practical meaning and is used for text beauty.

5. INSERT INTO statement

  1. /**
  2. * Data insertion into SQL...
  3. */
  4. function sqlinsert() {
  5. $sql='';
  6. $tb=$this->tblist();
  7. foreach ($tb as $v) {
  8. $rs=mysql_query("SELECT * FROM $v");
  9. if (!mysql_num_rows($rs)) {//No data returned
  10. continue;
  11. }
  12. $sql.="-- Table data: $ v --rn";
  13. $sql.="INSERT INTO `$v` VALUESrn";
  14. while ($temp=mysql_fetch_row($rs)) {
  15. $sql.='(';
  16. foreach ($temp as $ v2) {
  17. if ($v2===null) {
  18. $sql.="NULL,";
  19. }
  20. else {
  21. $v2=mysql_real_escape_string($v2);
  22. $sql.="'$v2', ";
  23. }
  24. }
  25. $sql=mb_substr($sql, 0, -1);
  26. $sql.="),rn";
  27. }
  28. $sql=mb_substr($sql, 0, -3);
  29. $sql.=";-- --rnrn";
  30. }
  31. return $sql;
  32. }
Copy code

Notes: 1. When no data is returned, this loop must be jumped out to avoid generating redundant code. 2. When the field value is (NULL), the inserted character is (NULL) instead of ('NULL'), without single quotes. 3.$v2=mysql_real_escape_string($v2), this is necessary escape 4.mb_substr($sql, 0, -1), mb_substr($sql, 0, -3), the last comma (,) must be removed, otherwise the SQL statement will error 5.$sql.=";-- --rnrn", see the note in step 4 for details

6. Backup operation

  1. /**
  2. * Backup...
  3. * @param $filename file path
  4. */
  5. function beifen($filename) {
  6. $this->db(); //Connect to the database
  7. $sql=$this->sqlcreate();
  8. $sql2=$this->sqlinsert();
  9. $data=$sql.$sql2;
  10. return file_put_contents($filename, $data);
  11. }
Copy code

7. Restore operation

  1. /**
  2. * Restore...
  3. * @param $filename file path
  4. */
  5. function huanyuan($filename) {
  6. $this->db(); //Connect to the database
  7. //Delete the data table
  8. $list=$this- >tblist();
  9. $tb='';
  10. foreach ($list as $v) {
  11. $tb.="`$v`,";
  12. }
  13. $tb=mb_substr($tb, 0, - 1);
  14. if ($tb) {
  15. $rs=mysql_query("DROP TABLE $tb");
  16. if ($rs===false) {
  17. return false;
  18. }
  19. }
  20. //Execute SQL
  21. $str=file_get_contents($filename);
  22. $arr=explode('-- --', $str);
  23. array_pop($arr);
  24. foreach ($arr as $v) {
  25. $ rs=mysql_query($v);
  26. if ($rs===false) {
  27. return false;
  28. }
  29. }
  30. return true;
  31. }
Copy the code

to see the calling example. 1. Backup example:

  1. $x=new dbBackup();
  2. $x->database='test';
  3. $rs=$x->beifen('db.sql');
  4. var_dump($rs) ;
Copy code

2, restore example:

  1. $x=new dbBackup();
  2. $x->database='test';
  3. $rs=$x->huanyuan('db.sql');
  4. var_dump($rs) ;
Copy code

Full code:

  1. /**

  2. * PHP implements mysql backup and restore
  3. * Organized Programmer’s Home bbs.it-home.org
  4. */
  5. class dbBackup {
  6. public $host='localhost'; //Database address
  7. public $user='root'; //Login name
  8. public $pwd=''; //Password
  9. public $database; //Database name
  10. public $charset='utf8'; //Database connection encoding: mysql_set_charset
  11. /**
  12. * Backup...
  13. * @param $filename file path
  14. */
  15. function beifen($filename) {
  16. $this->db(); //Connect to the database
  17. $sql=$this->sqlcreate();
  18. $sql2=$this->sqlinsert();
  19. $data=$sql.$sql2;
  20. return file_put_contents($filename, $data);
  21. }
  22. /**
  23. * Restore...
  24. * @param $filename file path
  25. */
  26. function huanyuan($filename) {
  27. $this->db() ; //Connect to the database
  28. //Delete the data table
  29. $list=$this->tblist();
  30. $tb='';
  31. foreach ($list as $v) {
  32. $tb.="`$ v`,";
  33. }
  34. $tb=mb_substr($tb, 0, -1);
  35. if ($tb) {
  36. $rs=mysql_query("DROP TABLE $tb");
  37. if ($rs== =false) {
  38. return false;
  39. }
  40. }
  41. //Execute SQL
  42. $str=file_get_contents($filename);
  43. $arr=explode('-- --', $str);
  44. array_pop($arr);
  45. foreach ($arr as $v) {
  46. $rs=mysql_query($v);
  47. if ($rs===false) {
  48. return false;
  49. }
  50. }
  51. return true ;
  52. }
  53. /**
  54. * Connect to database...
  55. */
  56. function db() {
  57. $con = mysql_connect($this->host,$this->user,$this->pwd);
  58. if ( !$con){
  59. die('Could not connect');
  60. }
  61. $db_selected = mysql_select_db($this->database, $con);
  62. if (!$db_selected) {
  63. die('Can't use select db');
  64. }
  65. mysql_set_charset($this->charset); //Set encoding
  66. return $con;
  67. }
  68. /**
  69. * Table collection ...
  70. */
  71. function tblist() {
  72. $ list=array();
  73. $rs=mysql_query("SHOW TABLES FROM $this->database");
  74. while ($temp=mysql_fetch_row($rs)) {
  75. $list[]=$temp[0] ;
  76. }
  77. return $list;
  78. }
  79. /**
  80. *Table structure SQL...
  81. */
  82. function sqlcreate() {
  83. $sql='';
  84. $tb=$this->tblist();
  85. foreach ($tb as $v) {
  86. $rs=mysql_query("SHOW CREATE TABLE $v");
  87. $temp=mysql_fetch_row($rs);
  88. $sql.="-- Table structure: {$temp[0 ]} --rn";
  89. $sql.="{$temp[1]}";
  90. $sql.=";-- --rnrn";
  91. }

  92. < p> return $sql;
  93. }
  94. /**
  95. * Data insertion into SQL...
  96. */
  97. function sqlinsert() {
  98. $sql='';
  99. $tb=$this->tblist();
  100. foreach ($tb as $v) {
  101. $rs=mysql_query("SELECT * FROM $v");
  102. if (!mysql_num_rows($rs)) {//No data returned
  103. continue;
  104. }
  105. $sql.="-- table Data: $v --rn";
  106. $sql.="INSERT INTO `$v` VALUESrn";
  107. while ($temp=mysql_fetch_row($rs)) {
  108. $sql.='(';
  109. foreach ( $temp as $v2) {
  110. if ($v2===null) {
  111. $sql.="NULL,";
  112. }
  113. else {
  114. $v2=mysql_real_escape_string($v2);
  115. $sql.="' $v2',";
  116. }
  117. }
  118. $sql=mb_substr($sql, 0, -1);
  119. $sql.="),rn";
  120. }
  121. $sql=mb_substr($sql, 0, - 3);
  122. $sql.=";-- --rnrn";
  123. }
  124. return $sql;
  125. }
  126. }

  127. //Backup

  128. //$ x=new dbBackup();
  129. //$x->database='test';
  130. //$rs=$x->beifen('db.sql');
  131. //var_dump($rs);

  132. //Restore

  133. //$x=new dbBackup();
  134. //$x->database='test';
  135. //$rs=$x->huanyuan ('db.sql');
  136. //var_dump($rs);
  137. ?>

Copy code
Articles you may be interested in: php database backup class Share a good php database backup class php complete backup database and backup classes of specified tables in the database php mysql database backup class and calling method php implements mysql backup and restore example code php mysql backup code (xml application) php data backup: single table backup, whole table backup, import database


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn