2

我可以根据 codeigniter 文档使用 dbutil 类轻松备份我的 codeigniter 应用程序数据库。现在我想备份另一个未被 codeigniter 应用程序使用但在同一服务器中的数据库。我在 google 和 stackoverflow 中进行了搜索,但没有得到任何答案。

是否有任何替代 ci 类来备份任何数据库,或者我可以在 dbutil 类中使用构建?

提前致谢

4

3 回答 3

2

我以前没有使用过 dbutil 类,但你不能在你的 config/database 文件中添加新数据库并加载它吗?IE。

$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->database('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
于 2013-10-23T14:46:25.650 回答
2

您还可以将另一个数据库对象传递给 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...';
于 2018-07-16T13:57:54.283 回答
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";

 }
}    ?>
于 2014-03-26T10:26:04.043 回答