数据库备份是一个好看的功能,支持sae的数据库备份的类库
// +----------------------------------------------------------------------
// | author: 左边 (加群:366504956(刚建,欢迎) 交流thinkphp下微信开发)
// +----------------------------------------------------------------------
//类库放在application\common\libs目录下
use \common\libs\mysqlreback;
//数据备份
public function sqlback(){
$datadir = databak;
if(!file_exists($datadir))mkdir($datadir) ;
$opt = trim(i('get.opt'));
$file = trim(i('get.file'));
$mr = new mysqlreback();
$mr->setdbname(c('db_name'));
$mr->setpath($datadir);
if ($opt) {
if ($opt == 'backup') {
$mr->backup()?$alert = '数据库备份成功!':$alert = $mr->error();
}
if($opt == 'optimize'){
$mr->optimize()?$alert = '数据库优化成功!':$alert = $mr->error();
}
if ($opt == 'redo' && $file) {
$r = $mr->recover($file);
$alert = '数据库还原成功'.$r['qty'] .条;
}
if ($opt== 'del' && $file) {
$mr->remove($file)?$alert = '数据库删除成功!':$alert = $mr->error();
}
if ($opt == 'download' && $file ) $mr->downloadfile($file);
}
$lists = $mr->datalist($datadir);
$this->assign(list, $lists);
if($alert)
$this->assign('alert',alert('.$alert.'););
$this->display();
}页面
系统管理 > 数据库管理
编号
文件名
备份时间
文件大小
操作
{$key+1}
{$vo.filename}
{$vo.filetime}
{:$vo['filesize']/1000} kb
类文件path = 'databak';
}
public function setdbname($dbname) {
$this->dbname = $dbname;
}
public function setpath($path) {
$this->path = $path;
}
public function setiscompress($iscompress){
$this->iscompress = $iscompress;
}
private function getfile($filename) {
if(strtolower(storage_type) == 'sae'){
$s = new storage();
$r = json_encode($s -> getobject(strtolower( $this->path ), $filename) );
$r = json_decode($r,true);
return $r['body'];
}
$filename = $this->path . self::dir_sep . $filename;
if (is_file($filename)) {
$ext = strrchr($filename, '.');
if ($ext == '.sql') {
return file_get_contents($filename);
} elseif ($ext == '.gz') {
return implode('', gzfile($filename));
} else {
$this->error = '_无法识别的文件格式!';
return;
}
} else {
$this->error = '文件不存在!';
return;
}
}
private function setfile($content) {
$recognize = $this->dbname;
$filename = $recognize . '_' . date('ymdhis') . '_' . mt_rand(100000000, 999999999) . '.sql';
if(strtolower(storage_type) == 'sae'){
$s = new storage();
$root_path = strtolower($this->$path);
//不存在就创建
$patharr = $s->listbuckets();
if(!in_array($root_path,$patharr) ){
$s->putbucket($root_path,'.r:*');
}
//写入
$path = $s->putobject($content, $root_path, $filename);
if($path !== true){
$this->error = 写入文件失败;
return;
}
return $path;
}
$filename = $this->path . self::dir_sep .$filename;
$path = $this->cetpath($filename);
if ($path !== true) {
$this->error = 无法创建备份目录,目录 '$path';
return;
}
if ($this->iscompress == 0) {
if (!file_put_contents($filename, $content, lock_ex)) {
$this->error = '写入文件失败,请检查磁盘空间或者权限!';
return;
}
} else {
if (function_exists('gzwrite')) {
$filename .= '.gz';
$gz = gzopen($filename, 'wb');
if ($gz) {
$gw = gzwrite($gz, $content);
gzclose($gz);
return $gw;
} else {
$this->error = '写入文件失败,请检查磁盘空间或者权限!';
return;
}
} else {
$this->error = '没有开启gzip扩展!';
return;
}
}
}
private function cetpath($filename) {
$dirs = explode(self::dir_sep, dirname($filename));
$tmp = '';
foreach ($dirs as $dir) {
$tmp .= $dir . self::dir_sep;
if (!file_exists($tmp) && !@mkdir($tmp, 0777))
return;
}
return true;
}
//备份
public function backup(){
$db = db::getinstance();
$tables = $db->query('show table status');
$tables = array_map('array_change_key_case', $tables);
$sql = '/* this file is created by mysqlreback ' . date('y-m-d h:i:s') . ' */';
foreach ($tables as $value) {
$table = $value['name'];
$result = $db->query(show create table `{$table}`);
$create = $result[0]['create table'];
$sql .= \r\n /* 创建表结构 {$table} */;
$sql .= \r\n drop table if exists {$table};. $this->sign . {$create}; . $this->sign;
$result = $db->query(select count(*) as count from `{$table}`);
$count = $result['0']['count'];
if($count){
$sql .= \r\n /* 插入数据 {$table} */;
$result = $db->query(select * from {$table});
foreach ($result as $row) {
$row = array_map('addslashes', $row);
$sql .= \r\n insert into {$table} values ( ' . str_replace( array(\r,\n) , array('\r','\n') ,implode(', ', $row) ) . ' );.$this->sign;
}
}
}
if ($sql) {
return $this->setfile($sql);
}else{
$this->error = '数据为空';
}
return;
}
//删除备份
public function remove($filename){
if(strtolower(storage_type) == 'sae'){
$s = new storage();
$r = $s->deleteobject(strtolower($this->path), $filename);
if(!$r){
$this->error = '删除失败';
return;
}
return true;
}
if(!@unlink($this->path . self::dir_sep . $filename)){
$this->error = '删除失败';
return;
}
return true;
}
//表优化
public function optimize(){
if(strtolower(storage_type) == 'sae'){
$this->error='sae数据库不支持表优化';
return ;
}
$db = db::getinstance();
$list = $db->query('show table status');
$list = array_map('array_change_key_case', $list);
foreach ($list as $key => $value) {
$tables[] = $value['name'];
}
if($tables) {
if(is_array($tables)){
$tables = implode('`,`', $tables);
$list[] = $db->execute(optimize table `{$tables}`);
}
}
return $list;
}
//恢复
public function recover($filename) {
$content = $this->getfile($filename);
if (!$content) {
if($this->error)return;
return '数据为空';
}
$content = explode($this->sign, $content);
$db = db::getinstance();
foreach ($content as $i => $sql) {
$sql = trim($sql);
if (!empty($sql)){
$res = $db->execute($sql);
if($res){
$rs['qty']++;
}else{
$rt[] = $sql;
}
}
}
$rs['error'] = $rt;
return $rs;
}
public function downloadfile($filename) {
ob_end_clean();
header(cache-control: must-revalidate, post-check=0, pre-check=0);
header('content-description: file transfer');
header('content-type: application/octet-stream');
if(strtolower(storage_type) == 'sae'){
$s = new storage();
$r = json_encode($s -> getobject(strtolower( $this->path ), $filename) );
$r = json_decode($r,true);
header('content-disposition: attachment; filename=' . $filename . '');
echo $r['body'];
}else{
$filename = $this->path . self::dir_sep . $filename;
$stat = stat($this->path . self::dir_sep . $filename);
header('content-disposition: attachment; filename=' . $filename . '');
header('content-length: ' . $stat['size']);
readfile($filename);
}
}
public function datalist() {
if(strtolower(storage_type) == 'sae'){
$s = new storage();
$f = $s->getbucket( strtolower($this->path) );
foreach ($f as $v) {
$rt['filename'] = $v['name'];
$rt['filetime'] = date('y-m-d h:i:s',strtotime($v['last_modified']) );
$rt['filesize'] = $v['bytes'];
$fileandfolderayy[] = $rt;
}
}else{
$filepath = opendir($this->path);
while (false !== ($filename = readdir($filepath))) {
if ($filename!=. && $filename!=..){
$stat = stat($this->path .'/'. $filename);
$rt['filename'] = $filename;
$rt['filetime'] = date('y-m-d h:i:s', $stat['mtime'] );
$rt['filesize'] = $stat['size'];
$fileandfolderayy[] = $rt;
}
}
}
$order == 0 ? sort($fileandfolderayy) : rsort($fileandfolderayy);
return $fileandfolderayy;
}
public function error(){
return $this->error;
}
}
?>
mysqlreback.class.zip ( 2.71 kb 下载:14 次 )
ad:真正免费,域名+虚机+企业邮箱=0元
