Backup MySQL database with CodeIgniter

前端 未结 15 1567
一生所求
一生所求 2020-12-30 06:39

I have been looking into the user guide which came with CodeIgniter. I became very interested with the dbutil() method. Particularly the following line of code:

15条回答
  •  半阙折子戏
    2020-12-30 07:26

    If you are lucky enough to have one of the exec(), shell_exec(), system() or passthru() enabled on your server. Maybe you would want to use the following:

    public function db_backup()
    {
        $DBUSER=$this->db->username;
        $DBPASSWD=$this->db->password;
        $DATABASE=$this->db->database;
    
        $filename = $DATABASE . "-" . date("Y-m-d_H-i-s") . ".sql.gz";
        $mime = "application/x-gzip";
    
        header( "Content-Type: " . $mime );
        header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
    
        // $cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best";   
        $cmd = "mysqldump -u $DBUSER --password=$DBPASSWD --no-create-info --complete-insert $DATABASE | gzip --best";
    
        passthru( $cmd );
    
        exit(0);
    }
    

提交回复
热议问题