数据库备份是一个好看的功能,支持SAE的数据库备份的类库// +----------------------------------------------------------------------<br>
// | Author: 左边 (加群:366504956(刚建,欢迎) 交流thinkphp下微信开发)<br>
// +----------------------------------------------------------------------<br>
<br>
<br>
<br>
//类库放在Application\Common\Libs目录下<br>
use \Common\Libs\MySQLReback;<br>
<br>
<br>
<br>
//数据备份<br>
public function sqlback(){<br>
$DataDir = "Databak";<br>
if(!file_exists($DataDir))mkdir($DataDir) ;<br>
<br>
$opt = trim(I('get.opt'));<br>
$file = trim(I('get.file'));<br>
$mr = new MySQLReback();<br>
$mr->setDBName(C('DB_NAME'));<br>
$mr->setPath($DataDir);<br>
<br>
if ($opt) {<br>
if ($opt == 'backup') {<br>
$mr->backup()?$alert = '数据库备份成功!':$alert = $mr->error();<br>
}<br>
if($opt == 'optimize'){<br>
$mr->optimize()?$alert = '数据库优化成功!':$alert = $mr->error();<br>
}<br>
if ($opt == 'redo' && $file) {<br>
$r = $mr->recover($file);<br>
$alert = '数据库还原成功'.$r['qty'] ."条";<br>
}<br>
if ($opt== 'del' && $file) {<br>
$mr->remove($file)?$alert = '数据库删除成功!':$alert = $mr->error();<br>
}<br>
if ($opt == 'download' && $file ) $mr->DownloadFile($file);<br>
}<br>
$lists = $mr->dataList($DataDir);<br>
$this->assign("list", $lists);<br>
if($alert)<br>
$this->assign('alert',"alert('".$alert."');");<br>
$this->display();<br>
}
页面<extend></extend><br>
<block><br>
<script> <br />
$(function(){<br />
$('.opt').click(function(){<br />
opt = $(this).data('opt');<br />
file = $(this).parent().data('f');<br />
location.href = "__ACTION__/opt/"+opt+'/file/'+file;<br />
})<br />
{$alert}<br />
<br />
})<br />
</script><br>
<br>
<br>
</block><br>
<br>
<block>系统管理 > 数据库管理</block><br>
<br>
<block><br>
<br>
<table>
<br>
<br>
<tr>
<br>
<td>
<br>
<table>
<br>
<tr>
<br>
<td>编号</td>
<br>
<td>文件名</td>
<br>
<td>备份时间</td>
<br>
<td>文件大小</td>
<br>
<td>操作</td>
<br>
<br>
</tr>
<br>
<volist><br>
<tr>
<br>
<td>
<br>
{$key+1}<br>
</td>
<br>
<td>
<br>
<a>{$vo.filename}</a><br>
</td>
<br>
<td>
<br>
{$vo.filetime}<br>
</td>
<br>
<td>
<br>
{:$vo['filesize']/1000} KB<br>
</td>
<br>
<td>
<br>
<input><br>
<input> <br>
<input><br>
</td>
<br>
</tr>
<br>
<br>
</volist><br>
<br>
<tr>
<br>
<td>
<br>
<input> <input><br>
</td>
<br>
<br>
</tr>
<br>
<br>
</table> <br>
</td>
<br>
</tr>
<br>
<br>
</table>
<br>
<br>
</block>
类文件<?php <br />
<br>
// +----------------------------------------------------------------------<br>
// | Author: 左边 (加群:366504956(刚建,欢迎) 交流thinkphp下微信开发)<br>
// +----------------------------------------------------------------------<br>
<br>
<br>
namespace Common\Libs;<br>
use Think\Db;<br>
use sinacloud\sae\Storage as Storage;<br>
<br>
class MySQLReback {<br>
<br>
private $path;<br>
private $isCompress;<br>
private $content;<br>
private $dbName;<br>
private $error;<br>
private $sign = '/*++$*/';<br>
<br>
const DIR_SEP = DIRECTORY_SEPARATOR;<br>
<br>
public function __construct() {<br>
$this->path = 'Databak';<br>
<br>
}<br>
<br>
public function setDBName($dbName) {<br>
$this->dbName = $dbName;<br>
}<br>
<br>
public function setPath($path) {<br>
$this->path = $path;<br>
}<br>
<br>
public function setIsCompress($isCompress){<br>
$this->isCompress = $isCompress;<br>
}<br>
<br>
private function getFile($fileName) {<br>
<br>
if(strtolower(STORAGE_TYPE) == 'sae'){<br>
$s = new Storage(); <br>
$r = json_encode($s -> getObject(strtolower( $this->path ), $fileName) );<br>
$r = json_decode($r,true);<br>
return $r['body'];<br>
<br>
}<br>
$fileName = $this->path . self::DIR_SEP . $fileName;<br>
if (is_file($fileName)) {<br>
$ext = strrchr($fileName, '.');<br>
if ($ext == '.sql') {<br>
return file_get_contents($fileName);<br>
} elseif ($ext == '.gz') {<br>
return implode('', gzfile($fileName));<br>
} else {<br>
$this->error = '_无法识别的文件格式!';<br>
return;<br>
}<br>
} else {<br>
$this->error = '文件不存在!';<br>
return;<br>
}<br>
}<br>
<br>
<br>
private function setFile($content) {<br>
<br>
$recognize = $this->dbName;<br>
$fileName = $recognize . '_' . date('YmdHis') . '_' . mt_rand(100000000, 999999999) . '.sql';<br>
<br>
if(strtolower(STORAGE_TYPE) == 'sae'){<br>
$s = new Storage();<br>
<br>
$root_path = strtolower($this->$path);<br>
//不存在就创建<br>
$pathArr = $s->listBuckets();<br>
if(!in_array($root_path,$pathArr) ){<br>
$s->putBucket($root_path,'.r:*');<br>
}<br>
//写入<br>
$path = $s->putObject($content, $root_path, $fileName);<br>
if($path !== true){<br>
$this->error = "写入文件失败";<br>
return;<br>
}<br>
return $path;<br>
}<br>
<br>
$fileName = $this->path . self::DIR_SEP .$fileName;<br>
$path = $this->cetPath($fileName);<br>
if ($path !== true) {<br>
$this->error = "无法创建备份目录,目录 '$path'";<br>
return;<br>
}<br>
if ($this->isCompress == 0) {<br>
if (!file_put_contents($fileName, $content, LOCK_EX)) {<br>
$this->error = '写入文件失败,请检查磁盘空间或者权限!';<br>
return;<br>
}<br>
} else {<br>
if (function_exists('gzwrite')) {<br>
$fileName .= '.gz';<br>
$gz = gzopen($fileName, 'wb');<br>
if ($gz) {<br>
$gw = gzwrite($gz, $content);<br>
gzclose($gz);<br>
return $gw;<br>
} else {<br>
$this->error = '写入文件失败,请检查磁盘空间或者权限!';<br>
return;<br>
}<br>
} else {<br>
$this->error = '没有开启gzip扩展!';<br>
return;<br>
}<br>
}<br>
}<br>
<br>
private function cetPath($fileName) {<br>
$dirs = explode(self::DIR_SEP, dirname($fileName));<br>
$tmp = '';<br>
foreach ($dirs as $dir) {<br>
$tmp .= $dir . self::DIR_SEP;<br>
if (!file_exists($tmp) && !@mkdir($tmp, 0777))<br>
return;<br>
}<br>
return true;<br>
}<br>
<br>
<br>
//备份<br>
public function backup(){<br>
$db = Db::getInstance();<br>
$tables = $db->query('SHOW TABLE STATUS');<br>
$tables = array_map('array_change_key_case', $tables);<br>
$sql = '/* This file is created by MySQLReback ' . date('Y-m-d H:i:s') . ' */';<br>
foreach ($tables as $value) {<br>
$table = $value['name']; <br>
$result = $db->query("SHOW CREATE TABLE `{$table}`");<br>
$create = $result[0]['create table'];<br>
$sql .= "\r\n /* 创建表结构 {$table} */";<br>
$sql .= "\r\n DROP TABLE IF EXISTS {$table};". $this->sign ." {$create};" . $this->sign;<br>
<br>
$result = $db->query("SELECT COUNT(*) AS count FROM `{$table}`");<br>
$count = $result['0']['count'];<br>
if($count){<br>
$sql .= "\r\n /* 插入数据 {$table} */";<br>
<br>
$result = $db->query("SELECT * FROM {$table}");<br>
<br>
foreach ($result as $row) {<br>
$row = array_map('addslashes', $row);<br>
$sql .= "\r\n INSERT INTO {$table} VALUES ( '" . str_replace( array("\r","\n") , array('\r','\n') ,implode("', '", $row) ) . "' );".$this->sign;<br>
}<br>
}<br>
<br>
}<br>
<br>
if ($sql) {<br>
return $this->setFile($sql);<br>
}else{<br>
$this->error = '数据为空';<br>
}<br>
return;<br>
<br>
}<br>
<br>
//删除备份<br>
public function remove($fileName){<br>
if(strtolower(STORAGE_TYPE) == 'sae'){<br>
$s = new Storage();<br>
$r = $s->deleteObject(strtolower($this->path), $fileName);<br>
if(!$r){<br>
$this->error = '删除失败';<br>
return;<br>
}<br>
return true;<br>
}<br>
if(!@unlink($this->path . self::DIR_SEP . $fileName)){<br>
$this->error = '删除失败';<br>
return;<br>
}<br>
return true;<br>
}<br>
<br>
//表优化<br>
public function optimize(){<br>
if(strtolower(STORAGE_TYPE) == 'sae'){<br>
$this->error='SAE数据库不支持表优化';<br>
return ;<br>
}<br>
$db = Db::getInstance();<br>
$list = $db->query('SHOW TABLE STATUS');<br>
$list = array_map('array_change_key_case', $list);<br>
foreach ($list as $key => $value) {<br>
$tables[] = $value['name'];<br>
}<br>
<br>
if($tables) {<br>
if(is_array($tables)){<br>
$tables = implode('`,`', $tables);<br>
$list[] = $db->execute("OPTIMIZE TABLE `{$tables}`");<br>
}<br>
}<br>
return $list;<br>
}<br>
<br>
//恢复<br>
public function recover($fileName) {<br>
$content = $this->getFile($fileName);<br>
if (!$content) {<br>
if($this->error)return;<br>
return '数据为空';<br>
}<br>
$content = explode($this->sign, $content);<br>
$db = Db::getInstance();<br>
foreach ($content as $i => $sql) {<br>
$sql = trim($sql);<br>
if (!empty($sql)){<br>
<br>
$res = $db->execute($sql);<br>
<br>
if($res){<br>
$rs['qty']++;<br>
}else{<br>
$rt[] = $sql;<br>
}<br>
}<br>
}<br>
$rs['error'] = $rt;<br>
return $rs;<br>
}<br>
<br>
public function DownloadFile($fileName) {<br>
ob_end_clean();<br>
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");<br>
header('Content-Description: File Transfer');<br>
header('Content-Type: application/octet-stream');<br>
<br>
if(strtolower(STORAGE_TYPE) == 'sae'){<br>
$s = new Storage(); <br>
$r = json_encode($s -> getObject(strtolower( $this->path ), $fileName) );<br>
$r = json_decode($r,true);<br>
header('Content-Disposition: attachment; filename="' . $fileName . '"');<br>
echo $r['body'];<br>
<br>
}else{<br>
$fileName = $this->path . self::DIR_SEP . $fileName;<br>
$stat = stat($this->path . self::DIR_SEP . $filename);<br>
header('Content-Disposition: attachment; filename="' . $fileName . '"');<br>
header('Content-Length: ' . $stat['size']);<br>
readfile($fileName);<br>
}<br>
<br>
}<br>
<br>
public function dataList() {<br>
<br>
if(strtolower(STORAGE_TYPE) == 'sae'){<br>
$s = new Storage();<br>
$f = $s->getBucket( strtolower($this->path) );<br>
foreach ($f as $v) {<br>
$rt['filename'] = $v['name'];<br>
$rt['filetime'] = date('Y-m-d H:i:s',strtotime($v['last_modified']) );<br>
$rt['filesize'] = $v['bytes'];<br>
$FileAndFolderAyy[] = $rt;<br>
}<br>
<br>
}else{<br>
$FilePath = opendir($this->path);<br>
while (false !== ($filename = readdir($FilePath))) {<br>
if ($filename!="." && $filename!=".."){<br>
$stat = stat($this->path .'/'. $filename);<br>
$rt['filename'] = $filename;<br>
$rt['filetime'] = date('Y-m-d H:i:s', $stat['mtime'] );<br>
$rt['filesize'] = $stat['size'];<br>
$FileAndFolderAyy[] = $rt;<br>
}<br>
}<br>
}<br>
$Order == 0 ? sort($FileAndFolderAyy) : rsort($FileAndFolderAyy);<br>
return $FileAndFolderAyy;<br>
}<br>
<br>
<br>
<br>
<br>
public function error(){<br>
return $this->error;<br>
}<br>
<br>
<br>
}<br>
<br>
?>
MySQLReback.class.zip ( 2.71 KB 下载:14 次 )
AD:真正免费,域名+虚机+企业邮箱=0元