php 使用 CodeIgniter 备份 MySQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14093020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Backup MySQL database with CodeIgniter
提问by muttalebm
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:
我一直在研究 CodeIgniter 附带的用户指南。我对这个dbutil()方法非常感兴趣。特别是以下代码行:
// 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);
It is supposed to backup the currently loaded MySQL database. But unfortunately, it is not working and I get the following message:
它应该备份当前加载的 MySQL 数据库。但不幸的是,它不起作用,我收到以下消息:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$dbutil
Filename: views/view.php
Line Number: 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
遇到 PHP 错误
严重性:注意
消息:未定义的属性:CI_Loader::$dbutil
文件名:views/view.php
行号:10
致命错误:调用第 10 行 C:\xampp\htdocs\CodeIgniter\application\views\view.php 中非对象的成员函数 backup()
What am I missing here? Any help would be really appreciated.
我在这里缺少什么?任何帮助将非常感激。
回答by Saleem
Try this, You can change format zip to gz if you like :)
试试这个,如果你愿意,你可以将格式 zip 更改为 gz :)
$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);
回答by Niko Sams
doing that using php will only work for very small databases. You will very fast run into memory limits - if you increase that other performance problems.
使用 php 这样做只适用于非常小的数据库。您将很快遇到内存限制 - 如果您增加其他性能问题。
What works best is to create a dump using mysqldump:
最好的方法是使用 mysqldump 创建转储:
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename="dbbackup.sql.gz"');
passthru("mysqldump --user=xx --host=xx --password=xx dbname | gzip");
of course you must have the required permissions to do that.
当然,您必须具有所需的权限才能执行此操作。
回答by user3260138
function backup($fileName='db_backup.zip'){
// 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(FCPATH.'/downloads/'.$fileName, $backup);
// Load the download helper and send the file to your desktop
$this->load->helper('download');
force_download($fileName, $backup);
}
Easy way to backup database using codeigniter
使用codeigniter备份数据库的简单方法
回答by Priyanshu Choudhary
//load helpers
$this->load->helper('file');
$this->load->helper('download');
$this->load->library('zip');
//load database
$this->load->dbutil();
//create format
$db_format=array('format'=>'zip','filename'=>'backup.sql');
$backup=& $this->dbutil->backup($db_format);
// file name
$dbname='backup-on-'.date('d-m-y H:i').'.zip';
$save='assets/db_backup/'.$dbname;
// write file
write_file($save,$backup);
// and force download
force_download($dbname,$backup);
回答by Rahul patel
public function db_backup()
{
$this->load->helper('url');
$this->load->helper('file');
$this->load->helper('download');
$this->load->library('zip');
$this->load->dbutil();
$db_format=array('format'=>'zip','filename'=>'my_db_backup.sql');
$backup=& $this->dbutil->backup($db_format);
$dbname='backup-on-'.date('Y-m-d').'.zip';
$save='assets/db_backup/'.$dbname;
write_file($save,$backup);
force_download($dbname,$backup);
}`
回答by Sandeep Yadav
public function backup(){
$this->load->dbutil();
$config = array(
'format' => 'zip',
'filename' => 'insert-file-name.sql'
);
$backup =& $this->dbutil->backup($config);
$db_name = 'backup-on-'. date("Y-m-d-H-i-s") .'.zip';
$save = 'uploads/'.$db_name;
$this->load->helper('file');
write_file($save, $backup);
$this->load->helper('download');
force_download($db_name, $backup);
}
回答by Kamran Ahmed
If you are lucky enough to have one of the exec(), shell_exec(), system()or passthru()enabled on your server. Maybe you would want to use the following:
如果你足够幸运,有的一个exec(),shell_exec(),system()或passthru()您的服务器上启用。也许您想使用以下内容:
public function db_backup()
{
$DBUSER=$this->db->username;
$DBPASSWD=$this->db->password;
$DATABASE=$this->db->database;
$filename = $DATABASE . "-" . date("Y-m-d_H-i-s") . ".sql.gz";
$mime = "application/x-gzip";
header( "Content-Type: " . $mime );
header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
// $cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best";
$cmd = "mysqldump -u $DBUSER --password=$DBPASSWD --no-create-info --complete-insert $DATABASE | gzip --best";
passthru( $cmd );
exit(0);
}
回答by Raham
Try this one...it has been tested...if you are going to use mysqli then it will works fine...you may put your code in your controller or model but i suggest to keep this one in your_model & call this function from your_controller...
试试这个……它已经过测试……如果你打算使用 mysqli 那么它会工作得很好……你可以把你的代码放在你的控制器或模型中,但我建议把这个放在 your_model 中并调用它来自 your_controller 的函数...
public function db_backup()
{
$this->load->dbutil();
$backup =& $this->dbutil->backup();
$this->load->helper('file');
write_file('your_file_path/your_DB.zip', $backup);
}
回答by sangita
I am extremely new to CodeIgniter but I succeeded to do this backup. Try this, it will work successfully and is very easy to implement. Write the code in your controller and call the function from your view page which use for backup. Get set, go and you are done.
我对 CodeIgniter 非常陌生,但我成功地做了这个备份。试试这个,它会成功并且很容易实现。在控制器中编写代码并从用于备份的视图页面调用该函数。准备好,去吧,你就完成了。
function dbbackup()
{
$this->load->dbutil();
$backup =& $this->dbutil->backup();
$this->load->helper('file');
write_file('<?php echo base_url();?>/downloads', $backup);
$this->load->helper('download');
force_download('mybackup.gz', $backup);
}
For your full application backup, do the same procedure with the following code:
对于完整的应用程序备份,请使用以下代码执行相同的过程:
function backup()
{
$this->load->helper('download');
$this->load->library('zip');
$time = time();
$this->zip->read_dir('D:xampp/htdocs/wms/');
$this->zip->download('my_backup.'.$time.'.zip');
}
Here you can use any path of your choice.
在这里,您可以使用您选择的任何路径。
回答by Flaviano Silva
Try this!
尝试这个!
$username = "root";
$password = "root";
$hostname = "localhost";
$dbname = "raas";
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($dbname . "_" .date("Y-m-d_H-i-s").".sql"));
$command = "C:\AppServ\MySQL\bin\mysqldump --add-drop-table --host=$hostname --user=$username --password=$password ".$dbname;
system($command);

