Welcome Guest, Not a member yet? Register   Sign In
how to create backup database
#1

(This post was last modified: 08-03-2015, 01:27 AM by R3Z4.)

hi gays
i want careate backup pdo database
my code:

PHP Code:
function backup()
    {
        
$prefs = array(
 
       'tables'        => array('category'),   // Array of tables to backup.
 
       'ignore'        => array(),                     // List of tables to omit from the backup
 
       'format'        => 'txt'                      // gzip, zip, txt
 
       'filename'      => 'mybackup.sql'             // File name - NEEDED ONLY WITH ZIP FILES
 
       'add_drop'      => TRUE                       // Whether to add DROP TABLE statements to backup file
 
       'add_insert'    => TRUE                       // Whether to add INSERT data to backup file
 
       'newline'       => "\n"                         // Newline character used in backup file
);

        
// Load the DB utility class
        
$this->load->dbutil();

        
// Backup your entire database and assign it to a variable
        
$backup =& $this->dbutil->backup($prefs);

        
// Load the file helper and write the file to your server
        
$this->load->helper('file');
        
write_file('./mybackup.gz'$backup);

        
// Load the download helper and send the file to your desktop
        
$this->load->helper('download');
        
force_download('mybackup.gz'$backup);
    } 

error:
Unsupported feature of the database platform you are using.

Filename: C:\xampp\htdocs\chercknevis\system\database\drivers\pdo\pdo_utility.php
Line Number: 97



please help me
tanx Huh
Reply
#2

What version of codeigniter are you using? My pdo_utility.php file doesn't even have 97 lines of code in it.
Reply
#3

Probably 2.x, but the result doesn't change in 3.0.

The backup feature as implemented in MySQLi (and maybe some of the other drivers) reads the structure and data of the tables and outputs very specific text to a .sql file to recreate that structure and insert the data. If this could be implemented for PDO, it would have to be implemented on the sub-driver level, since the backup is raw SQL, and can't be abstracted.

You might be able to create your own backup method which generates a migration file to recreate the database using dbforge and query builder (or active record in 2.x).
Reply
#4

(08-04-2015, 08:19 AM)mwhitney Wrote: Probably 2.x, but the result doesn't change in 3.0.

The backup feature as implemented in MySQLi (and maybe some of the other drivers) reads the structure and data of the tables and outputs very specific text to a .sql file to recreate that structure and insert the data. If this could be implemented for PDO, it would have to be implemented on the sub-driver level, since the backup is raw SQL, and can't be abstracted.

You might be able to create your own backup method which generates a migration file to recreate the database using dbforge and query builder (or active record in 2.x).

I have created a patch for this that adds the logic to the method on pdo_utility.php.
Download and extract the attached patch file.
Put the patch file in the root of your project (in the same parent directory as "system")
then open a terminal and cd to the directory where you put the patch
run the following command
Code:
patch -1p < pdo_utility_backup.patch

Here's the actual code in the file system/database/drivers/pdo/pdo_utility.php

It replaces the existing function that just returns the incompatibility error.

PHP Code:
/**
 * Export
 *
 * @param array $params Preferences
 * @return mixed
 */
 
protected function _backup($params = array())
 {
 {
 if (
count($params) === 0)
 {
 return 
FALSE;
 }

 
// Extract the prefs for simplicity
 
extract($params);

 
// Build the output
 
$output '';

 
// Do we need to include a statement to disable foreign key checks?
 
if ($foreign_key_checks === FALSE)
 {
 
$output .= 'SET foreign_key_checks = 0;'.$newline;
 }

 foreach ( (array) 
$tables as $table)
 {
 
// Is the table in the "ignore" list?
 
if (in_array($table, (array) $ignoreTRUE))
 {
 continue;
 }

 
// Get the table schema
 
$query $this->db->query('SHOW CREATE TABLE '.$this->db->escape_identifiers($this->db->database.'.'.$table));

 
// No result means the table name was invalid
 
if ($query === FALSE)
 {
 continue;
 }

 
// Write out the table schema
 
$output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline;

 if (
$add_drop === TRUE)
 {
 
$output .= 'DROP TABLE IF EXISTS '.$this->db->protect_identifiers($table).';'.$newline.$newline;
 }

 
$i 0;
 
$result $query->result_array();
 foreach (
$result[0] as $val)
 {
 if (
$i++ % 2)
 {
 
$output .= $val.';'.$newline.$newline;
 }
 }

 
// If inserts are not needed we're done...
 
if ($add_insert === FALSE)
 {
 continue;
 }

 
// Grab all the data from the current table
 
$query $this->db->query('SELECT * FROM '.$this->db->protect_identifiers($table));

 if (
$query->num_rows() === 0)
 {
 continue;
 }
 
// Fetch the field names and determine if the field is an
 // integer type. We use this info to decide whether to
 // surround the data with quotes or not

 
$field_str '';
 
$is_int = array();
 
$column_count $query->result_id->columnCount();
 for (
$i=0;$i $column_count$i++){
 
$field $query->result_id->getColumnMeta($i);

 
// Most versions of MySQL store timestamp as a string
 
$is_int[$i] = in_array($field['native_type'], array('TINY''SMALL''MEDIUM''LONG'), TRUE);

 
// Create a string of field names
 
$field_str .= $this->db->escape_identifiers($field['name']).', ';
 }

 
// Trim off the end comma
 
$field_str preg_replace('/, $/' ''$field_str);

 
// Build the insert string
 
foreach ($query->result_array() as $row)
 {
 
$val_str '';

 
$i 0;
 foreach (
$row as $v)
 {
 
// Is the value NULL?
 
if ($v === NULL)
 {
 
$val_str .= 'NULL';
 }
 else
 {
 
// Escape the data if it's not an integer
 
$val_str .= ($is_int[$i] === FALSE) ? $this->db->escape($v) : $v;
 }

 
// Append a comma
 
$val_str .= ', ';
 
$i++;
 }

 
// Remove the comma at the end of the string
 
$val_str preg_replace('/, $/' ''$val_str);

 
// Build the INSERT string
 
$output .= 'INSERT INTO '.$this->db->protect_identifiers($table).' ('.$field_str.') VALUES ('.$val_str.');'.$newline;
 }

 
$output .= $newline.$newline;
 }

 
// Do we need to include a statement to re-enable foreign key checks?
 
if ($foreign_key_checks === FALSE)
 {
 
$output .= 'SET foreign_key_checks = 1;'.$newline;
 }

 return 
$output;
 } 


Attached Files
.zip   pdo_utility_backup.zip (Size: 1.6 KB / Downloads: 1)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB