YII2备份恢复教程——完善版 [ 2.0 版本 ]
原文档出处:https://segmentfault.com/a/1190000006779817
具体如下:
spanjeta/yii2-backup
spanjeta/yii2-backup 是基于Yii2 的一款 第三方扩展,安装配置一下可以正常访问,也可以很方便自定义。在开发过程中,因为备份后在 phpMyAdmin 导入出现一些问题。所以做了一些修改。
安装spanjeta/yii2-backup
"require": {
"spanjeta/yii2-backup": "*"
}
打开cmd 命令 找到项目目录下 执行 (注意:必须先安装 composer ,否则失败)
composer update
配置spanjeta/yii2-backup
找到 \backend\config\main.php 打开加入以下代码
return[
'modules' => [
'backup' => [
'class' => 'spanjeta\modules\backup\Module',
],
],
]
确保你的项目有创建目录的权限,访问 index.php?r=backup 到此扩展安装完毕
修改spanjeta/yii2-backup
默认安装 在\vendor 目录下,找到 \spanjeta\yii2-backup\controllers\ 打开 DefaultController.php 文件,修改以下代码
找到 getData() 直接覆盖即可
public function getData($tableName) {
$sql = 'SELECT * FROM ' . $tableName;
$cmd = Yii::$app->db->createCommand ( $sql );
$dataReader = $cmd->query();
$data_string="";
foreach ( $dataReader as $data ) {
foreach ($data as $key => $value) {
if(empty($value)){
unset($data[$key]);
}else{
$datas[$key]=mysql_real_escape_string($value);
}
}
$itemNames = array_keys ( $datas );
$itemNames = array_map ( "addslashes", $itemNames );
$items = join ( '`,`', $itemNames );
$itemValues = array_values ( $datas );
$valueString = join ( "','", $itemValues );
$valueString = "('" . $valueString . "'),";
$values = "\n" . $valueString;
if ($values != "") {
$data_string .= "INSERT INTO `$tableName` (`$items`) VALUES" . rtrim ( $values, "," ) . ";" . PHP_EOL;
}
}
if (empty($data_string)){
return null;
}
if ($this->fp) {
$this->writeComment ( 'TABLE DATA ' . $tableName );
$final = $data_string . PHP_EOL . PHP_EOL . PHP_EOL;
fwrite ( $this->fp, $final );
} else {
$this->tables [$tableName] ['data'] = $data_string;
return $data_string;
}
}
搜索以下代码,并注释掉
fwrite ( $this->fp, 'SET AUTOCOMMIT=0;' . PHP_EOL );
找到 actionCreate() 直接覆盖即可
public function actionCreate() {
$tables = $this->getTables ();
if (! $this->StartBackup ()) {
Yii::$app->user->setFlash ( 'success', "Error" );
return $this->render ( 'index' );
}
foreach ( $tables as $tableName ) {
$this->getColumns ( $tableName );
$this->getData ( $tableName );
}
$this->EndBackup ();
$this->redirect ( array (
'index'
) );
}
okokad
注册时间:2016-02-29
最后登录:2017-07-01
在线时长:49小时26分
最后登录:2017-07-01
在线时长:49小时26分
- 粉丝12
- 金钱25
- 威望80
- 积分1315
共 1 条评论
此文档虽对spanjeta/yii2-backup进行了修补,但是还是有一些问题
现修改如下:
1.unset($data[$key]); 这一句要去掉。
2.数据备分时,null和空要区分开。$value=is_null($value)?'NULL':$value;
可参考一下如下代码:
public function getRecord($table, $record,$key) { global $op; // sql字段逗号分割 $insert = ''; $comma = ""; $insert .= "INSERT INTO `" . $table."`" . $key. " VALUES("; // 循环每个子段下面的内容 foreach ($record as $key => $value) { $conn= mysql_connect($op['host'],$op['username'],$op['password']); mysql_select_db($op['dbname'],$conn); mysql_set_charset('utf8', $conn) or die('set charset failure.'); $value=is_null($value)?'NULL':$value; $value=mysql_real_escape_string($value,$conn); $value=$value=='NULL'?'NULL':"'".$value."'"; $insert .= ($comma . $value ); $comma = ","; } $insert .= ");" . $this->ds; return $insert; }