php - 使用 OHO Codeigniter 备份 MySQL 数据库

标签 php mysql codeigniter-3 database-backups

我在确定应该下载 zip 备份文件的目的地时遇到问题,并且 zip 文件的名称总是随机生成的,就像我得到的 0ca26f32-b90c-4198-b078-ed2778a23c0b.zip 但压缩文件夹中的 sql 文件使用给定名称 backup_2019-01-24-22-33-03.sql

PHP备份功能

下面的函数是下载一个压缩文件夹,其中包含 Windows 默认下载文件夹中的数据库 sql 文件。

public function backup_get(){
        $prefs = array(
            'tables' => array('convt', 'invoice', 'item', 'order_inv', 'person', 'return_details'), // Array of tables to backup.
            'ignore' => array(), // List of tables to omit from the backup
            'format' => 'zip', // gzip, zip, txt
            'filename' => 'backup_' . date("Y-m-d-H-i-s") . '.sql', // File name - NEEDED ONLY WITH ZIP FILES
            'add_drop' => true, // Whether to add DROP TABLE statements to backup file
            'add_insert' => true, // Whether to add INSERT data to backup file
            'newline' => "\n", // Newline character used in backup file
        );

        $backup = $this->dbutil->backup($prefs);
        $db_name = 'backup-on-' . date("Y-m-d-H-i-s") . '.zip';
        $save = './backup/' . $db_name;
        $this->load->helper('file');
        write_file($save, $backup);


        http_response_code(200);
        header('Content-Length: ' . filesize($save));
        header("Content-Type: application/zip");
        header('Content-Disposition: attachment; filename="backup.zip"');
        readfile($save);
        exit;
    }

问题

我想将默认目标表单 downloads 文件夹更改为我手动/自动生成的文件夹,并能够为 zip 文件命名。

感谢任何帮助

最佳答案

尝试像这样修改 backup_get() 函数:

public function backup_get(){
    $prefs = array(
        'tables' => array('convt', 'invoice', 'item', 'order_inv', 'person', 'return_details'), // Array of tables to backup.
        'ignore' => array(), // List of tables to omit from the backup
        'format' => 'zip', // gzip, zip, txt
        'filename' => 'backup_' . date("Y-m-d-H-i-s") . '.sql', // File name - NEEDED ONLY WITH ZIP FILES
        'add_drop' => true, // Whether to add DROP TABLE statements to backup file
        'add_insert' => true, // Whether to add INSERT data to backup file
        'newline' => "\n", // Newline character used in backup file
    );

    $backup = $this->dbutil->backup($prefs);
    $db_name = 'backup-on-' . date("Y-m-d-H-i-s") . '.zip';
    $backup_path = './backup/'; // this is the destination directory name
    if (!file_exists($backup_path)) {
        mkdir($backup_path, 0755, true);
    }
    $save = $backup_path . $db_name;
    $this->load->helper('file');
    write_file($save, $backup);
}

$backup_path 是您要设置的自定义目录。

关于php - 使用 OHO Codeigniter 备份 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54356410/

相关文章:

php - 从android studio到php到mysql的连接存储在phpmyadmin中

MySQL 右连接计数无法正常工作

php - 访问被禁止怎么解决! xampp 服务器的 codeigniter 错误

java.lang.String 无法从 Android 转换为 JSONObject

php - 阅读问候包时如何解决错误?

php - 类似于 C++ 的 PHP 类模板

php - MySQL - ORDER BY STR_TO_DATE 不工作

mysql - 从 mysql 中不同 ID 的计数结果中选择 max()

php - 将excel文件导入PHP并存储在MySQL中时如何使用PHPExcel库读取下标/上标/换行

php - 来自 codeigniter 中输入插入的 NULL 变量值