php代码升级更新机制,php实现⼀个简单的项⽬升级功能思路
1.制作升级包,增量更新
2.升级包上传验证上传⽂件,验证当前系统版本
3.升级之前要先备份原来的代码,升级失败要进⾏回滚
4.记录升级⽇志,返回升级进度
5.升级包应该是加密的(暂时没实现)
说明
1.升级包的⽬录结构必须如下
/**
* 升级包规定的⽬录结构
* xxx_版本号.zip(如:xxx_1.0.0.zip)
* |
* |————mysql
* | |
* | |___mysql_update.sql(更新脚本)
* | |___mysql_rockback.sql(回滚脚本)
* |
* |____php
*
*/
create table test(id init(11));
create table test2(id init(11));
mysql_rockback.sql
drop table test;
drop table test2;
4.代码
class UpgradeSys{
public $update_log = "/tmp/web/update_log.log"; //系统升级⽇志
public $return_log = "/tmp/web/return_log.log"; //系统回滚⽇志
public $progress_log = "/tmp/web/progress_log.log"; //记录进度
public $backup_dir = "/tmp/web/backup_dir";//备份⽬录
public $upload_dir = "/tmp/web/upload_dir";//升级包⽬录
public $sys_version_num = '1.0.0';//当前系统的版本 这个在实际应⽤中应该是⾍数据库获取得到的,这⾥只是举个例⼦/** 展⽰升级界⾯ */
public function index()
{
include("update.html");
}
/**
* 处理升级包上传
*/
public function upload()
{
$params = $_POST;
if($_FILES)
{
$name = $_FILES['file']['tmp_name'];
if(!$name || !is_uploaded_file($name))
{
echo json_encode(["status"=>0,"msg"=>"请上传升级包⽂件"]);
die;
}
}
//校验后缀
$astr = explode('.',$name);
$ext = array_pop($astr);
if($ext != 'zip')
{
echo json_encode(["status"=>0,"msg"=>"请上传⽂件格式不对"]);
die;
}
//校验升级密码
// echo json_encode(["status"=>0,"msg"=>"密码错误"]);
// die;
// }
//对⽐版本号
$astr = explode('_',$name);
$version_num = str_replace(".zip", '',array_pop($astr));
if(!$version_num)
{
echo json_encode(["status"=>0,"msg"=>"获取版本号失败"]); die;
}
//对⽐
if(!$this->compare_version($version_num))
{
echo json_encode(["status"=>0,"msg"=>"不能升级低版本的"]); die;
}
$package_name = $this->upload_dir.'/'.$version_num.'.zip'; if(!move_uploaded_file($name,$package_name))
{
echo json_encode(["status"=>0,"msg"=>"上传⽂件失败"]); die;
}
//记录下⽇志
$this->save_log("上传升级包成功!");
$this->update_progress("20%");
//备份code
$result = $this->backup_code();
if(!$result)
{
$this->save_log("备份失败!");
echo json_encode(["status"=>0,"msg"=>"备份失败"]);
$this->update_progress("30%");
//执⾏升级
$this->execute_update($package_name);
}
/**
* 升级操作
* @return [type] [description]
*/
private function execute_update($package_name)
{
//解压 如何使⽤zip加密压缩,这⾥解压缩的时候注意要解密
exec(" cd $upload_dir && unzip $package_name ");
$package_name = str_replace(".zip","",$package_name);
if(!is_dir($package_name))
{
$this->save_log("解压失败");
echo json_encode(["status"=>0,"msg"=>"解压失败"]);
die;
}
$this->update_progress("50%");
/
/升级mysql
if(file_exists($this->upload_dir.'/'.$package_name."/mysql/mysql_update.sql"))
{
$result = $this->database_operation($this->upload_dir.'/'.$package_name."/mysql/mysql_update.sql"); if(!$result['status'])
{
echo json_encode($result);die;
}
}
$this->update_progress("70%");
//升级PHP
if(is_dir($this->upload_dir.'/'.$package_name.'/php'))
if($status != 0 )
{
$this->save_log("php更新失败");
//数据库回滚
if(file_exists($this->upload_dir.'/'.$package_name."/mysql/mysql_rockback.sql"))
{
$this->save_log("数据库回滚");
$this->database_operation($this->upload_dir.'/'.$package_name."/mysql/mysql_rockback.sql");
}
//php代码回滚
php初学者项目$cmd = "cp -rf " .$this->backup_dir."/".$this->sys_version_num.'/'.basename($this->root_dir)."/* ".$this->root_dir; exec($cmd,$mdata,$status);
$this->save_log("php回滚");
echo json_encode(["status"=>0,"msg"=>"php更新失败"]);
die;
}
}
//把解压的升级包清除
exec("rm -rf $upload_dir/$package_name ");
$this->update_progress("100%");
//更新系统的版本号了
//更新php的版本号了(应该跟svn/git的版本号⼀致)
//更新数据库的版本号了(应该跟svn/git的版本号⼀致)
echo json_encode(["status"=>1,"msg"=>"升级成功"]);
die;
}
/**
* ⽐较代码版本
* @return [type] [description]
*/
private function compare_version($version_num='1.0.0')
{
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论