文件没有编写mySQL备份CRON

时间:2015-12-21 22:05:44

标签: mysql codeigniter

我正在尝试进行MySQL数据库备份并保存.sql文件。实用程序似乎处理得很好,但在编写文件时。什么都没发生。不确定有什么问题?路径或代码

public function mysql_backup()
{

// Load the DB utility class
$this->load->dbutil();

$date = new DateTime();
$time = $date->format('Y-m-d_H-i-s');

// Backup your entire database and assign it to a variable
$prefs = array(
                'tables'      => array(),  // Array of tables to backup.
                'ignore'      => array(),           // List of tables to omit from the backup
                'format'      => 'txt',             // gzip, zip, txt
                'filename'    => 'sql_backup_'.$time.'.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); 

// Load the file helper and write the file to your server
write_file('./backup/sql_backup_'.$time.'.sql', $backup);

    echo '<h2>Cron Successfully Runned.</h2>'; 

    }

1 个答案:

答案 0 :(得分:0)

在我看来,你并没有加载file助手。 尝试在代码

之前添加$this->load->helper('file');
public function mysql_backup()
{

// Load the DB utility class
$this->load->helper('file');
$this->load->dbutil();

$date = new DateTime();
$time = $date->format('Y-m-d_H-i-s');

// Backup your entire database and assign it to a variable
$prefs = array(
                'tables'      => array(),  // Array of tables to backup.
                'ignore'      => array(),           // List of tables to omit from the backup
                'format'      => 'txt',             // gzip, zip, txt
                'filename'    => 'sql_backup_'.$time.'.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); 

// Load the file helper and write the file to your server
write_file('./backup/sql_backup_'.$time.'.sql', $backup);

    echo '<h2>Cron Successfully Runned.</h2>'; 

    }