Exemple #1
0
 /**
  * Run after a backup. Add metadata to the file.
  *
  * @param \BackupMigrate\Core\File\BackupFileWritableInterface $file
  * @return \BackupMigrate\Core\File\BackupFileWritableInterface
  */
 public function afterBackup(BackupFileWritableInterface $file)
 {
     // Add the various metadata.
     foreach ($this->getMetaKeys() as $key) {
         $value = $this->confGet($key);
         $file->setMeta($key, $value);
     }
     return $file;
 }
 /**
  *  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;
 }
Exemple #3
0
 /**
  * Gzip decode a file.
  *
  * @param \BackupMigrate\Core\File\BackupFileReadableInterface $from
  * @param \BackupMigrate\Core\File\BackupFileWritableInterface $to
  * @return bool
  */
 protected function _ZipDecode(BackupFileReadableInterface $from, BackupFileWritableInterface $to)
 {
     $success = FALSE;
     if (class_exists('ZipArchive')) {
         $zip = new \ZipArchive();
         $res = $zip->open($to->realpath(), constant("ZipArchive::CREATE"));
         if ($res === TRUE) {
             $zip->addFile($from->realpath(), $from->getMeta('filename'));
             $success = $zip->close();
         }
     }
     return $success;
 }
Exemple #4
0
 /**
  * {@inheritdoc}
  */
 public function closeArchive()
 {
     $this->writeFooter();
     $this->archive->close();
 }