• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dbutil - how to target a different group?

#1
[eluser]da_n[/eluser]
Hello. I've searched the forums and found a few topics related to my question, but no definitive answer so really hope someone can help or let me know a definite yes/no answer. I want to know if there is any way to define a different database group other than the default when using dbutil. I have a project which I want to run automated backups for several databases, but I get errors. Here is my (early) code which I have put [removed] to hide passwords etc:

database.php

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

$db['default']['hostname'] = "[removed]";
$db['default']['username'] = "[removed]";
$db['default']['password'] = "[removed]";
$db['default']['database'] = "[removed]";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

$db['db2']['hostname'] = "[removed]";
$db['db2']['username'] = "[removed]";
$db['db2']['password'] = "[removed]";
$db['db2']['database'] = "[removed]";
$db['db2']['dbdriver'] = "mysql";
$db['db2']['dbprefix'] = "";
$db['db2']['pconnect'] = FALSE;
$db['db2']['db_debug'] = TRUE;
$db['db2']['cache_on'] = FALSE;
$db['db2']['cachedir'] = "";
$db['db2']['char_set'] = "utf8";
$db['db2']['dbcollat'] = "utf8_general_ci";

controller

Code:
class Backup extends Controller {

    function Backup()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $db2 = $this->load->database('db2', TRUE);
        $this->load->dbutil();
        $this->load->helper('file');
        
        // set date
        $sum    = mktime(0, 0, 0, date("m")  , date("d"), date("Y"));
        $date    = date('Y_m_d', $sum);
        
        // create the backup
        $filename = 'backup';
        $prefs = array(
            'format'    => 'zip',
            'filename'    => "$filename_$date.sql"
        );
        
        $backup =& $this->dbutil->backup($prefs);
        write_file("/var/www/vhosts/[removed]/$filename_$date.zip", $backup);
        
        // email the backup to gmail
        $this->load->library('email');
        $this->email->from('[removed]', '[removed]');
        $this->email->to('[removed]');
        $this->email->subject("$date db backup - $filename");
        $this->email->message("$filename backup for $date");
        $this->email->attach("/var/www/vhosts/[removed]/$filename_$date.zip");
        $this->email->send();
        
        // remove backup
        unlink("/var/www/vhosts/[removed]/$filename_$date.zip");
    }
}

It would be a pain to have to set-up a whole new codeigniter project for every database, let alone hard to manage. Any help would be greatly appreciated.

Thanks.

#2
[eluser]da_n[/eluser]
Realised I should probably have mentioned the errors. There are loads that come, here are the first 4:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Backup::$db

Filename: libraries/Loader.php

Line Number: 278

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/Loader.php

Line Number: 278

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Backup::$db

Filename: libraries/Loader.php

Line Number: 278

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/Loader.php

Line Number: 278

#3
[eluser]danmontgomery[/eluser]
Instead of:
Code:
$this->dbutil->backup($prefs);

Try
Code:
$db2->dbutil->backup($prefs);

#4
[eluser]da_n[/eluser]
Thanks but I tried this before, I just get the following error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_DB_mysql_driver::$load

Filename: controllers/backup.php

Line Number: 13

Fatal error: Call to a member function dbutil() on a non-object in /var/www/vhosts/[removed]/httpdocs/system/application/controllers/backup.php on line 13


By the way the code does work if I use the default group only.

EDIT: Sorry there was a little problem with the controller code but it does work, it just needs instances of
Code:
$filename_$date
to be replaced with
Code:
$filename".'_'."$date
. The above error is still being displayed however.

#5
[eluser]danmontgomery[/eluser]
The above error is because you're calling load() on a mysql object instead of a controller object. Post line 13 of controllers/backup.php


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.