Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backup MySQL database with CodeIgniter

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); 

It is supposed to backup the currently loaded MySQL database. But unfortunately, it is not working and I get the following message:

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

What am I missing here? Any help would be really appreciated.

like image 639
muttalebm Avatar asked Dec 30 '12 17:12

muttalebm


1 Answers

//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);
like image 110
Priyanshu Choudhary Avatar answered Oct 22 '22 02:10

Priyanshu Choudhary