Backup.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace mall\basic;
  10. use mall\utils\Tool;
  11. use think\facade\Cache;
  12. use think\facade\Config;
  13. use think\facade\Db;
  14. class Backup {
  15. public function getDir(){
  16. return Tool::getRootPath() . "runtime/backup";
  17. }
  18. public function getDirFile(){
  19. $path = $this->getDir();
  20. $fileArray = glob($path . '/*.sql');
  21. return is_array($fileArray) ? $fileArray : [];
  22. }
  23. public function deleteFile($data=null){
  24. $path = $this->getDir();
  25. if(is_array($data)){
  26. foreach($data as $file){
  27. $filePath = $path . '/' . $file;
  28. file_exists($filePath) && unlink($filePath);
  29. }
  30. return true;
  31. }else if(is_string($data)){
  32. $file = $path . '/' . $data;
  33. return file_exists($file) && unlink($file);
  34. }
  35. return false;
  36. }
  37. /**
  38. * 导入备份
  39. */
  40. public function import($file="",$setp=null){
  41. $path = $this->getDir() . '/' . $file;
  42. if(!file_exists($path)){
  43. throw new \Exception("导入失败,文件不存在");
  44. }
  45. $data = Cache::remember('backup', function () use ($path) {
  46. $sql = file_get_contents($path);
  47. $sql = str_replace("\r", "\n", $sql);
  48. $sql = explode(";\n", $sql);
  49. Cache::tag('backup_data', 'backup');
  50. return $sql;
  51. });
  52. if(is_null($setp)){
  53. foreach($data as $val){
  54. !empty(trim($val)) && substr(ltrim($val),0,3) != '-- ' && Db::query($val);
  55. }
  56. Cache::delete('backup');
  57. }else if(array_key_exists($setp, $data)){
  58. $sql = trim($data[$setp]);
  59. Db::query($sql);
  60. }
  61. return count($data);
  62. }
  63. public function export(){
  64. $tables = Db::query("SHOW TABLES");
  65. $sql = "";
  66. foreach($tables as $value){
  67. $sql .= $this->exportTable(current(array_values($value)));
  68. }
  69. $fileInfo = "-- A3Mall SQL Dump\n";
  70. $fileInfo .= "-- VERSION - " . Config::get("version.version") . "\n";
  71. $fileInfo .= "-- date:" . date("Y-m-d H:i:s") . ";\n\n";
  72. //$fileInfo .= 'SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";' . "\n";
  73. //$fileInfo .= "SET AUTOCOMMIT = 0;\n";
  74. //$fileInfo .= "START TRANSACTION;\n";
  75. //$fileInfo .= 'SET time_zone = "+00:00";' . "\n\n";
  76. $fileInfo .= $sql;
  77. //$fileInfo .= "COMMIT;\n";
  78. $fileInfo .= "-- the end of backup";
  79. $path = $this->getDir();
  80. return file_put_contents($path . '/' . date("YmdHis").".sql",$fileInfo);
  81. }
  82. private function exportTable($table=""){
  83. $sql = "DROP TABLE IF EXISTS `$table`;\n";
  84. $result = Db::query("SHOW CREATE TABLE $table");
  85. $sql .= $result[0]['Create Table'] . ";\n\n";
  86. $data = Db::table($table)->select()->toArray();
  87. if(empty($data[0])){
  88. return $sql;
  89. }
  90. $fields = '`'.implode("`,`",array_keys($data[0])).'`';
  91. $sql .= 'INSERT INTO `'.$table.'`('.$fields.') VALUES ';
  92. foreach($data as $value){
  93. $sql .= "(";
  94. $sql .= "'" . implode("','",array_map('addslashes',array_values($value))) . "'";
  95. $sql .= "),";
  96. }
  97. $sql = rtrim($sql,',') . ";\n\n\n\n";
  98. return $sql;
  99. }
  100. }