douzhang3822 2017-12-06 03:53
浏览 185

从Codeigniter备份还原数据库

I was trying to make an automated database backup using built-in CodeIgniter $this->dbutil->backup().

The script is:

    $this->load->dbutil();

    $backup = $this->dbutil->backup(array(
        'tables'        => array(),   // Array of tables to backup.
        'ignore'        => array('regencies', 'villages', 'provinces'),                     // List of tables to omit from the backup
        'add_drop'      => TRUE,                        // Whether to add DROP TABLE statements to backup file
        'add_insert'    => TRUE,                        // Whether to add INSERT data to backup file
        'newline'       => "
"                         // Newline character used in backup file
    ));

    $this->load->helper('file');

    $latest = md5(uniqid());

    write_file(APPPATH . 'backup/'. $latest .'.gz', $backup);

The scripts run just fine. But the problem comes when i wan't to restore. It comes like this:

sql result of the codeigniter backup

As a result of that i cannot restore it from CLI (mysql -u root -p dbname < db.sql) and from Navicat/Sequel Pro.

restore database from cli

The question is, how to restore that it to database again? (Notice that there's no quot after VALUES)

  • 写回答

2条回答 默认 最新

  • dozc58418381 2017-12-06 04:12
    关注

    This is a simple SQL syntax issue. String literals need to be enclosed in single quotes.

    INSERT INTO `foo` (`bar`,`bell`) VALUES ( Hey diddle diddle , cat and fiddle )
    

    won't work. We need

    INSERT INTO `foo` (`bar`,`bell`) VALUES ('Hey diddle diddle','cat and fiddle')
                                             ^-----------------^ ^--------------^
    

    Take a closer look at the actual SQL statement you are trying to execute.

    Your "unloader" is going to need to be smarter, enclosing string and date values in single quotes when they get put into a SQL statement. (For convenience, numeric literals can be enclosed in single quotes as well, so you don't need to discriminate, just enclose all values in the VALUES list in single quotes.

    Also be aware of what will happen when a value includes a single quote. For example:

     ... VALUES ( 'O'Leary' , ... 
                  ^_^     
    

    That's also going to be an error. Single quotes within a value will need to be escaped. One way to do that in MySQL is to precede a single quote with another single quote, like this:

     ... VALUES ( 'O''Leary' , ... 
                  ^--------^
    

    This would be valid, and would evaluate to a string containing a single single quote O'Leary

    Also, MySQL will interpret a backslash character in a string literal as an escape character. For example:

     ... VALUES ( 'this probably 
    ot what we want' , 
                                 ^^       
    

    That sequence is going to interpreted as a newline character, not as the character n. So, backslash characters are also going to need to be escaped.

    et al.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器