使用php和Codeigniter备份SQL表

时间:2014-03-07 13:53:20

标签: php mysql sql codeigniter backup

我想使用codeigniter备份数据库中的表。我找到了this Guide,并根据指南对功能进行了编程。问题是,我没有收到错误,我无法在我的服务器上的任何地方找到备份。

我的功能如下:

function backup_Eventtable() {
    $this->load->dbutil();
    $prefs = array(
            'tables'      => array('MYTABLE'),  // Array of tables to backup.
            'ignore'      => array(),           // List of tables to omit from the backup
            'format'      => 'txt',             // gzip, zip, txt
            'filename'    => 'mybackup.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);
    //I tried it with and without the next 2 Lines.
    $this->load->helper('file');
    write_file('/uploads/EventBackup/mybackup.sql', $backup); 
}

我刚检查了appache错误日志,发现了这一行:

[Fri Mar 07 14:28:37 2014] [error] [client 192.168.242.116] PHP Fatal error:  Call to undefined method CI_DB_mysql_driver::backup() in /var/www/html/kaufleuten/admin/system/application/models/eventmodel.php on line 764, referer: http://devzh2.energy.local:8093/admin/events/edit/4002

database.php看起来像这样:

$active_group = "default";
$active_record = TRUE;

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "myusername";
$db['default']['password'] = "mypassword";
$db['default']['database'] = "kaufleuten_typo3";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "/var/www/html/kaufleuten/admin/cache";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

1 个答案:

答案 0 :(得分:1)

我必须在config.php中启用Codeigniter日志。 ($ config ['log_threshold'] = 2;)我找到了错误日志:

ERROR - 2014-03-07 15:33:35 --> Severity: Warning  --> fopen(uploads/EventBackup/mybackup.sql): failed to open stream: No such file or directory /var/www/html/kaufleuten/admin/system/helpers/file_helper.php 90

将路径更改为:

 write_file('/var/www/html/kaufleuten/uploads/EventBackup/mybackup.sql', $backup);

解决了问题。