Ver Mensaje Individual
  #1 (permalink)  
Antiguo 11/07/2011, 20:42
privatefta
(Desactivado)
 
Fecha de Ingreso: septiembre-2010
Mensajes: 498
Antigüedad: 13 años, 7 meses
Puntos: 5
duda con guardar datos de una bd

hola es que tengo este codigo para guardar la base de datos

proceso_respaldar_bd.php

Código PHP:
Ver original
  1. <?php
  2.  
  3. require_once("includes/db_connect.php");
  4. require_once("cron/sql_dump.php");
  5. $server = new server();
  6. $nombre_base = 'nombre';
  7. $nombre2 = 'funciones/' . $nombre_base;
  8. $nombre_archivo = $nombre2 . '-' . date('Y-m-d-H-i-s') . '.bak';
  9.  
  10. // Hacer el dump
  11. $dump = new MySQLDump();
  12. $sql = $dump->dumpDatabase( $nombre_base );
  13. // Abrir y guardar el dump
  14. $archivo = fopen($nombre_archivo, 'w');
  15. fwrite( $archivo, $sql, strlen($sql) );
  16. fclose( $archivo );
  17.  
  18. ?>

sql_dump.php

Código PHP:
Ver original
  1. <?php
  2.  
  3. /**
  4.  * Dump data from MySQL database
  5.  *
  6.  * $dump = new MySQLDump();
  7.  * print $dump->dumpDatabase("mydb");
  8.  *
  9.  */
  10. class MySQLDump {
  11.  
  12.  
  13.     /**
  14.      * Dump data and structure from MySQL database
  15.      *
  16.      * @param string $database
  17.      * @return string
  18.      */
  19.     function dumpDatabase($database) {
  20.         $structure = '';
  21.         $data = '';
  22.  
  23.  
  24.         // Connect to database
  25.         $db = @mysql_select_db($database);
  26.  
  27.         if (!empty($db)) {
  28.  
  29.             // Get all table names from database
  30.             $c = 0;
  31.             $result = mysql_list_tables($database);
  32.             for($x = 0; $x < mysql_num_rows($result); $x++) {
  33.            
  34.                 //echo mysql_tablename($result, $x) . '<br>';
  35.                 //continue;
  36.                
  37.                 $table = mysql_tablename($result, $x);
  38.                 if (!empty($table)) {
  39.                     $arr_tables[$c] = mysql_tablename($result, $x);
  40.                     $c++;
  41.                 }
  42.             }
  43.  
  44.             // List tables
  45.             $dump = '';
  46.             for ($y = 0; $y < count($arr_tables); $y++){
  47.                
  48.                 // DB Table name
  49.                 $table = $arr_tables[$y];
  50.  
  51.                 // Structure Header
  52.                 $structure .= "-- \n";
  53.                 $structure .= "-- Table structure for table `{$table}` \n";
  54.                 $structure .= "-- \n\n";
  55.  
  56.                 // Dump Structure
  57.                 $structure .= "DROP TABLE IF EXISTS `{$table}`; \n";
  58.                 $structure .= "CREATE TABLE `{$table}` (\n";
  59.                 $result = mysql_db_query($database, "SHOW FIELDS FROM `{$table}`");
  60.                 while($row = mysql_fetch_object($result)) {
  61.  
  62.                     $structure .= "  `{$row->Field}` {$row->Type}";
  63.                     $structure .= (!empty($row->Default)) ? " DEFAULT '{$row->Default}'" : false;
  64.                     $structure .= ($row->Null != "YES") ? " NOT NULL" : false;
  65.                     $structure .= (!empty($row->Extra)) ? " {$row->Extra}" : false;
  66.                     $structure .= ",\n";
  67.  
  68.                 }
  69.  
  70.                 $structure = ereg_replace(",\n$", "", $structure);
  71.  
  72.                 // Save all Column Indexes in array
  73.                // unset($index);
  74.                $index = '';
  75.                 $result = mysql_db_query($database, "SHOW KEYS FROM `{$table}`");
  76.                 while($row = mysql_fetch_object($result)) {
  77.  
  78.                     if (($row->Key_name == 'PRIMARY') AND ($row->Index_type == 'BTREE')) {
  79.                         $index['PRIMARY'][$row->Key_name] = $row->Column_name;
  80.                     }
  81.  
  82.                     if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '0') AND ($row->Index_type == 'BTREE')) {
  83.                         $index['UNIQUE'][$row->Key_name] = $row->Column_name;
  84.                     }
  85.  
  86.                     if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '1') AND ($row->Index_type == 'BTREE')) {
  87.                         $index['INDEX'][$row->Key_name] = $row->Column_name;
  88.                     }
  89.  
  90.                     if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '1') AND ($row->Index_type == 'FULLTEXT')) {
  91.                         $index['FULLTEXT'][$row->Key_name] = $row->Column_name;
  92.                     }
  93.  
  94.                 }
  95.                
  96.                 // Return all Column Indexes of array
  97.                 if (is_array($index)) {
  98.                     foreach ($index as $xy => $columns) {
  99.  
  100.                         $structure .= ",\n";
  101.  
  102.                         $c = 0;
  103.                         foreach ($columns as $column_key => $column_name) {
  104.  
  105.                             $c++;
  106.  
  107.                             $structure .= ($xy == "PRIMARY") ? "  PRIMARY KEY  (`{$column_name}`)" : false;
  108.                             $structure .= ($xy == "UNIQUE") ? "  UNIQUE KEY `{$column_key}` (`{$column_name}`)" : false;
  109.                             $structure .= ($xy == "INDEX") ? "  KEY `{$column_key}` (`{$column_name}`)" : false;
  110.                             $structure .= ($xy == "FULLTEXT") ? "  FULLTEXT `{$column_key}` (`{$column_name}`)" : false;
  111.  
  112.                             $structure .= ($c < (count($index[$xy]))) ? ",\n" : false;
  113.  
  114.                         }
  115.  
  116.                     }
  117.  
  118.                 }
  119.  
  120.                 $structure .= "\n);\n\n";
  121.  
  122.                 // Header
  123.                 $structure .= "-- \n";
  124.                 $structure .= "-- Dumping data for table `$table` \n";
  125.                 $structure .= "-- \n\n";
  126.  
  127.                 // Dump data
  128.                 unset($data);
  129.                 $result     = mysql_query("SELECT * FROM `$table`");
  130.                 $num_rows   = mysql_num_rows($result);
  131.                 $num_fields = mysql_num_fields($result);
  132.                 $data='';
  133.                 for ($i = 0; $i < $num_rows; $i++) {
  134.            
  135.                     $row = mysql_fetch_object($result);
  136.                     $data .= "INSERT INTO `$table` (";
  137.  
  138.                     // Field names
  139.                     for ($x = 0; $x < $num_fields; $x++) {
  140.  
  141.                         $field_name = mysql_field_name($result, $x);
  142.  
  143.                         $data .= "`{$field_name}`";
  144.                         $data .= ($x < ($num_fields - 1)) ? ", " : false;
  145.  
  146.                     }
  147.  
  148.                     $data .= ") VALUES (";
  149.  
  150.                     // Values
  151.                     for ($x = 0; $x < $num_fields; $x++) {
  152.                         $field_name = mysql_field_name($result, $x);
  153.  
  154.                         $data .= "'" . str_replace('\"', '"', mysql_escape_string($row->$field_name)) . "'";
  155.                         $data .= ($x < ($num_fields - 1)) ? ", " : false;
  156.  
  157.                     }
  158.  
  159.                     $data.= ");\n";
  160.                 }
  161.  
  162.                 $data.= "\n";
  163.  
  164.                 $dump .= $structure . $data;
  165.                 $dump .= "-- --------------------------------------------------------\n\n";
  166.  
  167.             }
  168.  
  169.             return $dump;
  170.  
  171.         }
  172.  
  173.     }
  174.  
  175. }
  176.  
  177. ?>

pero tengo un problema que me funciona en un servidor en windows con appserver

pero en un servidor linux me tira eror

Warning: fopen(backop/DB-2011-07-11-20-40-00.bak) [function.fopen]: failed to open stream: Permission denied in /var/www/prueba/cron/proceso_respaldar_bd.php on line 14

Warning: fwrite(): supplied argument is not a valid stream resource in /var/www/prueba/cron/proceso_respaldar_bd.php on line 15

Warning: fclose(): supplied argument is not a valid stream resource in /var/www/prueba/cron/proceso_respaldar_bd.php on line 16