Home Backend Development PHP Problem How to implement update function in php

How to implement update function in php

Sep 04, 2020 am 09:36 AM
php

php method to implement the update function: first make an upgrade package and incrementally update; then verify the uploaded file and verify the current system version; then back up the original code and roll back when the upgrade fails; finally record Upgrade log, just return the upgrade progress.

How to implement update function in php

Recommended: "PHP Video Tutorial"

php implements a simple project upgrade function

Ideas

1. Make an upgrade package, incremental update

2. Upload the upgrade package, verify the uploaded file, and verify the current system version

3. Before upgrading, you must back up the original code. If the upgrade fails, roll back

4. Record the upgrade log and return the upgrade progress

5. The upgrade package should be encrypted (not implemented yet)

Instructions

1. The directory structure of the upgrade package must be as follows

/**
 *  升级包规定的目录结构
 *  xxx_版本号.zip(如:xxx_1.0.0.zip)
 *   |
 *   |————mysql
 *   |    |
 *   |    |___mysql_update.sql(更新脚本)
 *   |    |___mysql_rockback.sql(回滚脚本)
 *   |    
 *   |____php
 * 
*/
Copy after login

2.mysql_update.sql

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 $root_dir = "/Users/feng/Documents/work/test"; //站点代码的根目录
    public $aFile = ["log","runtime"];//忽略文件夹相对路径
    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;
        }
        //校验升级密码
        // if(!isset($params['password']) || $params['password'] != $this->password)
        // {
        //     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"=>"备份失败"]);
            die;
        }
        $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'))
        {
            exec("cd {$this->upload_dir}/{$package_name}/php && cp -rf ./* $this->root_dir ",$mdata,$status);
            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代码回滚
                $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')
    {
        
        return version_compare($version_num,$this->sys_version_num,'>');
    }
    /**
     * 备份代码
     */
    private function backup_code()
    {
        //rsync 要确定系统是否已经安装
        $cmd = "cd $root_dir && cd ..  && rsync -av ";
        foreach ($this->aFile as $key => $value) {
            $cmd ."--exclude ". basename($this->root_dir) ."/".$value ." ";
        }
        $cmd .= basename($this->root_dir)." ".$this->backup_dir."/".$this->sys_version_num;
        exec($cmd,$mdata,$status);
        if($status != 0)
        {
            return false;
        }
        //这里还可以对备份的文件进行压缩
        return true;
    }
    /**
     * 数据库操作
     */
    public function database_operation($file)
    {
        $mysqli = new mysqli("localhost","root","root","test");
        if($mysqli->connect_errno)
        {
            return ["status"=>0,"msg"=>"Connect failed:".$mysqli->connect_error];
        }
        $sql = file_get_contents($file);
        $a = $mysqli->multi_query($sql);
        return ["status"=>1,"msg"=>"数据库操作OK"];
    }
    /**
     * 返回系统升级的进度
     */
    public function update_progress($progress)
    {
        exec(" echo '".$progress."' > $this->progress_log ");
    }
    /**
     * 记录日志
     */
    public function save_log($msg,$action="update")
    {
        $msg .= date("Y-m-d H:i:s").":".$msg."\n";
        if($action == "update")
        {
            exec(" echo '".$msg."' >>  $this->update_log ");
        }else
        {
            exec(" echo '".$msg."' >>  $this->return_log ");
        }
    }
}
Copy after login

The above is the detailed content of How to implement update function in php. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles