我可以根据codeigniter文档使用dbutil类轻松备份我的codeigniter app数据库。现在我想要备份另一个不是由codeigniter应用程序使用但在同一服务器中的数据库。 我在谷歌搜索以及stackoverflow但没有得到任何答案。
是否有备用ci类来备份任何数据库,或者我可以在dbutil类中使用build?
提前致谢
答案 0 :(得分:2)
我之前没有使用过dbutil类,但你不能只在配置/数据库文件中添加新数据库并加载它吗?即
$db['new_db_name']['hostname'] = "localhost"; // localhost if same server
$db['new_db_name']['username'] = "un"; // your db username
$db['new_db_name']['password'] = 'pass'; // your db password
$db['new_db_name']['database'] = "new_db_name"; // your db name
$db['new_db_name']['dbdriver'] = "mysql";
$db['new_db_name']['dbprefix'] = "";
$db['new_db_name']['pconnect'] = TRUE;
$db['new_db_name']['db_debug'] = TRUE;
$db['new_db_name']['cache_on'] = FALSE;
$db['new_db_name']['cachedir'] = "";
$db['new_db_name']['char_set'] = "utf8";
$db['new_db_name']['dbcollat'] = "utf8_general_ci";
然后$ this-> load->数据库('new_db_name');
//无论dbutil函数......
修改强>
我刚测试了这个,似乎dbutil类使用了$ this-> db对象。因此,如果将$this->load->database('new_db_name', TRUE)
的结果设置为$this->db
(第二个'TRUE'参数以返回db对象),那么它可以工作,即:
$this->db = $this->load->database('new_db_name', TRUE);
$this->load->dbutil();
// do whatever
答案 1 :(得分:0)
试用此代码
<?php
class Backup extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->database(); // Loads the database setting
}
public function backup_db()
{
/* Store All Table name in an Array */
$return="";
$allTables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$allTables[] = $row[0];
}
foreach($allTables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE IF EXISTS '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; }
else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n";
}// Create Backup Folder
$folder = 'Database_Backup/';
if (!is_dir($folder))
mkdir($folder, 0755, true);
chmod($folder, 0755);
$date = date('m-d-Y-H-i-s', time());
$filename = $folder."Medi_Hospi-".$date;
$handle = fopen($filename.'.sql','w+');
fwrite($handle,$return);
fclose($handle);
echo "Backup of Database Taken";
}
} ?>
答案 2 :(得分:0)
如果您要管理的数据库不是默认数据库,您还可以将另一个数据库对象传递给DB Utility加载器: $ this-> myutil = $ this-> load-> dbutil($ this-> other_db,TRUE);
示例:
$this->other_db = $this->load->database('other_db', TRUE);
$this->myutil = $this->load->dbutil($this->other_db, TRUE);
$prefs = array(
'format' => 'zip',
'filename' => $file_name . '.sql'
);
$backup = $this->myutil->backup($prefs);
$db_name = 'backup-on-' . date("Y-m-d-H-i-s") . '.zip';
$save = './assets/backups/' . $folder . '/' . $db_name;
$this->load->helper('file');
write_file($save, $backup);
echo 'Done...';