Backup MySQL database with CodeIgniter
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);
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.
What works best is to create a dump using 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.
//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 filewrite_file($save,$backup);// and force downloadforce_download($dbname,$backup);