码迷,mamicode.com
首页 > Web开发 > 详细

PHP-执行外部程序

时间:2019-01-27 21:38:06      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:mysq   dex   function   创建   directory   hand   data   script   mes   

备份/恢复数据库

exec — 执行一个外部程序(在php文件所在目录进行执行)

很久以前写的,很多方法是项目中的直接复制粘体用不了,只能提供下思路。
用到执行外部程序的就这一句:
exec("mysql -u$username -p$password $database < $backup_path$backup_name");

<?php
defined(‘BASEPATH‘) OR exit(‘No direct script access allowed‘);

class Database extends MY_Controller {
    private $backup_path;
    public function __construct(){
        parent::__construct();
        $this->backup_path = dirname($_SERVER[‘SCRIPT_FILENAME‘]).DIRECTORY_SEPARATOR.‘db_backup‘.DIRECTORY_SEPARATOR;
    }

    public function index(){
        $this->loadViewhf(‘back/database.html‘);
    }

    public function list_backup(){
        
        //打开文件夹
        $dir_handle = opendir($this->backup_path);
        //读取文件夹
        $backup_names = array(); 
        while(false !== $file = readdir($dir_handle)){
            if ($file == ‘.‘ || $file == ‘..‘) continue;
            // 创建每一行数据
            $table_row_map = (object)array(
                ‘backup_name‘ => $file,
                ‘backup_path‘ => ‘~/db_backup‘
                );
            // 加入每一行数据到data
            $result[‘data‘][] = $table_row_map;
        }
        //返回结果  
        $this->returnResult($result);
    }


    public function backup(){

        // 选择备份路径(网站根目录下的db_backup文件夹)
        $backup_path = $this->backup_path;
        // 生成备份文件名
        $backup_name = date("Y-m-d_H-i-s").‘_backup.sql‘;;
        // 进行备份
        $username = $this->db->username;
        $password = $this->db->password;
        $database = $this->db->database;
        exec("mysqldump -u$username -p$password $database > $backup_path$backup_name");
        // 返回结果
        $result[‘status‘] = true;
        $this->returnResult($result);
    }

    public function del_backup(){
        // 备份文件名
        $backup_name = $this->input->post(‘backup_name‘, true);
        // 选择备份路径(网站根目录下的db_backup文件夹)
        $backup_path = $this->backup_path;
        // 删除
        if (unlink($backup_path.$backup_name)) {
            $result[‘status‘] = true;
        }else{
            $result[‘status‘] = false;
        }
        // 返回结果
        $this->returnResult($result);
    }

    public function recover_backup(){
        // 备份文件名
        $backup_name = $this->input->post(‘backup_name‘, true);
        // 选择备份路径(网站根目录下的db_backup文件夹)
        $backup_path = $this->backup_path;
        // 恢复备份
        $username = $this->db->username;
        $password = $this->db->password;
        $database = $this->db->database;
        exec("mysql -u$username -p$password $database < $backup_path$backup_name");
        // 返回结果
        $result[‘status‘] = true;
        $this->returnResult($result);
    }

}

PHP-执行外部程序

标签:mysq   dex   function   创建   directory   hand   data   script   mes   

原文地址:https://www.cnblogs.com/jffun-blog/p/10327442.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!