使用CodeIgniter备份MySQL数据库 [英] Backup MySQL database with CodeIgniter
问题描述
我一直在研究CodeIgniter附带的用户指南。我对 dbutil()
方法感兴趣。特别是以下代码:
I have been looking into the user guide which came with CodeIgniter. I became very interested with the dbutil()
method. Particularly the following line of code:
// Load the DB utility class
$this->load->dbutil();
// Backup your entire database and assign it to a variable
$backup =& $this->dbutil->backup();
// Load the file helper and write the file to your server
$this->load->helper('file');
write_file('/path/to/mybackup.gz', $backup);
// Load the download helper and send the file to your desktop
$this->load->helper('download');
force_download('mybackup.gz', $backup);
它应该备份当前加载的MySQL数据库。但不幸的是,它不工作,我得到以下消息:
It is supposed to backup the currently loaded MySQL database. But unfortunately, it is not working and I get the following message:
遇到PHP错误
A PHP Error was encountered
严重性:公告
消息:未定义属性:CI_Loader :: $ dbutil
Message: Undefined property: CI_Loader::$dbutil
文件名:views / view.php
Filename: views/view.php
行号:10
致命错误:调用成员函数备份)在
中的非对象上C:\xampp\htdocs\CodeIgniter\application\views\view.php在第10行上
Fatal error: Call to a member function backup() on a non-object in C:\xampp\htdocs\CodeIgniter\application\views\view.php on line 10
我在这里缺少什么?
推荐答案
试试这个,你可以改变格式zip为gz如果你喜欢:)
Try this, You can change format zip to gz if you like :)
$this->load->dbutil();
$prefs = array(
'format' => 'zip',
'filename' => 'my_db_backup.sql'
);
$backup =& $this->dbutil->backup($prefs);
$db_name = 'backup-on-'. date("Y-m-d-H-i-s") .'.zip';
$save = 'pathtobkfolder/'.$db_name;
$this->load->helper('file');
write_file($save, $backup);
$this->load->helper('download');
force_download($db_name, $backup);
这篇关于使用CodeIgniter备份MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!