/** * Get the sql to insert the data for a given table */ protected function _dumpTableSQLToFile(BackupFileWritableInterface $file, $table) { // If this is a view, do not export any data if (empty($table['engine'])) { return 0; } // Otherwise export the table data. $rows_per_line = 30; //$this->confGet('rows_per_line');//variable_get('backup_migrate_data_rows_per_line', 30); $bytes_per_line = 2000; //$this->confGet('bytes_per_line'); variable_get('backup_migrate_data_bytes_per_line', 2000); $lines = 0; $result = $this->query("SELECT * FROM `" . $table['name'] . "`"); $rows = $bytes = 0; // Escape backslashes, PHP code, special chars $search = array('\\', "'", "", "\n", "\r", ""); $replace = array('\\\\', "''", '\\0', '\\n', '\\r', '\\Z'); while ($result && ($row = $result->fetch_assoc())) { // DB Escape the values. $items = array(); foreach ($row as $key => $value) { $items[] = is_null($value) ? "null" : "'" . str_replace($search, $replace, $value) . "'"; // @TODO: escape binary data } // If there is a row to be added. if ($items) { // Start a new line if we need to. if ($rows == 0) { $file->write("INSERT INTO `" . $table['name'] . "` VALUES "); $bytes = $rows = 0; } else { $file->write(","); } // Write the data itself. $sql = implode(',', $items); $file->write('(' . $sql . ')'); $bytes += strlen($sql); $rows++; // Finish the last line if we've added enough items if ($rows >= $rows_per_line || $bytes >= $bytes_per_line) { $file->write(";\n"); $lines++; $bytes = $rows = 0; } } } // Finish any unfinished insert statements. if ($rows > 0) { $file->write(";\n"); $lines++; } return $lines; }
/** * Gzip encode a file. * * @param \BackupMigrate\Core\File\BackupFileReadableInterface $from * @param \BackupMigrate\Core\File\BackupFileWritableInterface $to * @return bool */ protected function _ZipEncode(BackupFileReadableInterface $from, BackupFileWritableInterface $to) { $success = FALSE; if (class_exists('ZipArchive')) { $zip = new \ZipArchive(); if ($zip->open($from->realpath())) { $filename = $zip->getNameIndex(0); if ($fp_in = $zip->getStream($filename)) { while (!feof($fp_in)) { $to->write(fread($fp_in, 1024 * 512)); } fclose($fp_in); $success = TRUE; } } } return $success; }
/** * Write a footer to mark the end of the archive. */ private function writeFooter() { // ----- Write the last 0 filled block for end of archive $v_binary_data = pack('a1024', ''); $this->archive->write($v_binary_data); }