[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.