示例#1
0
 /**
  * Do the actual file save. This function is called to save the data file AND
  * the metadata sidecar file.
  * @param \BackupMigrate\Core\File\BackupFileReadableInterface $file
  * @throws \BackupMigrate\Core\Exception\BackupMigrateException
  */
 function _saveFile(BackupFileReadableInterface $file)
 {
     // Check if the directory exists.
     $this->checkDirectory();
     // @TODO Decide what the appropriate file_exists strategy should be.
     file_unmanaged_move($file->realpath(), $this->confGet('directory') . $file->getFullName(), FILE_EXISTS_REPLACE);
 }
  /**
   * Do the actual file save. This function is called to save the data file AND
   * the metadata sidecar file.
   * @param \BackupMigrate\Core\File\BackupFileReadableInterface $file
   * @throws \BackupMigrate\Core\Exception\BackupMigrateException
   */
  function _saveFile(BackupFileReadableInterface $file) {
    // Check if the directory exists.
    $this->checkDirectory();

    copy($file->realpath(), $this->_idToPath($file->getFullName()));
    // @TODO: use copy/unlink if the temp file and the destination do not share a stream wrapper.
  }
示例#3
0
文件: FileNamer.php 项目: Wylbur/gj
 /**
  * Run on a backup. Name the backup file according to the configuration
  *
  * @param \BackupMigrate\Core\File\BackupFileReadableInterface $file
  * @return \BackupMigrate\Core\File\BackupFileReadableInterface
  */
 public function afterBackup(BackupFileReadableInterface $file)
 {
     $name = $this->confGet('filename');
     if ($this->confGet('timestamp')) {
         $name .= '-' . gmdate($this->confGet('timestamp_format'));
     }
     $file->setName($name);
     return $file;
 }
示例#4
0
 /**
  * {@inheritdoc}
  */
 function saveFile(BackupFileReadableInterface $file)
 {
     $stream_uri = $this->confGet('streamuri');
     if ($fp_out = fopen($stream_uri, 'w')) {
         $file->openForRead();
         while ($data = $file->readBytes(1024 * 512)) {
             fwrite($fp_out, $data);
         }
         fclose($fp_out);
         $file->close();
     } else {
         throw new \Exception("Cannot open the file {$stream_uri} for writing");
     }
 }
 /**
  * {@inheritdoc}
  */
 public function importFromFile(BackupFileReadableInterface $file)
 {
     if ($directory = $this->confGet('directory')) {
         // Make sure the directory ends in exactly 1 slash:
         if (substr($directory, -1) !== '/') {
             $directory = $directory . '/';
         }
         if (!file_exists($directory)) {
             throw new BackupMigrateException('The directory %dir does not exist to restore to.', array('%dir' => $directory));
         }
         if (!is_writable($directory)) {
             throw new BackupMigrateException('The directory %dir cannot be written to because of the operating system file permissions.', array('%dir' => $directory));
         }
         if (!($reader = $this->getArchiveReader())) {
             throw new BackupMigrateException('A file directory source requires an archive reader object.');
         }
         // Check that the file endings match.
         if ($reader->getFileExt() !== $file->getExtLast()) {
             throw new BackupMigrateException('This source expects a .%ext file.', array('%ext' => $archiver->getFileExt()));
         }
         $reader->setArchive($file);
         $reader->extractTo($directory);
         $reader->closeArchive();
         return TRUE;
     }
     return FALSE;
 }
示例#6
0
 /**
  * {@inheritdoc}
  */
 function saveFile(BackupFileReadableInterface $file)
 {
     // Set some default download headers.
     $headers = array(array('key' => 'Content-Disposition', 'value' => 'attachment; filename="' . $file->getFullName() . '"'), array('key' => 'Cache-Control', 'value' => 'no-cache'));
     // Set a mime-type header.
     if ($mime = $file->getMeta('mimetype')) {
         $headers[] = array('key' => 'Content-Type', 'value' => $mime);
     } else {
         // Get the mime type for this file if possible
         $mime = 'application/octet-stream';
         $mime = $this->plugins()->call('alterMime', $mime, array('ext' => $file->getExtLast()));
         $headers[] = array('key' => 'Content-Type', 'value' => $mime);
     }
     // In some circumstances, web-servers will double compress gzipped files.
     // This may help aleviate that issue by disabling mod-deflate.
     if ($file->getMeta('mimetype') == 'application/x-gzip') {
         if (function_exists('apache_setenv')) {
             apache_setenv('no-gzip', '1');
         }
         $headers[] = array('key' => 'Content-Encoding', 'value' => 'gzip');
     }
     if ($size = $file->getMeta('filesize')) {
         $headers[] = array('key' => 'Content-Length', 'value' => $size);
     }
     // Suppress the warning you get when the buffer is empty.
     @ob_end_clean();
     if ($file->openForRead()) {
         foreach ($headers as $header) {
             // To prevent HTTP header injection, we delete new lines that are
             // not followed by a space or a tab.
             // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
             $header['value'] = preg_replace('/\\r?\\n(?!\\t| )/', '', $header['value']);
             header($header['key'] . ': ' . $header['value']);
         }
         // Transfer file in 1024 byte chunks to save memory usage.
         while ($data = $file->readBytes(1024 * 512)) {
             print $data;
         }
         $file->close();
     }
     // @TODO Throw exception.
 }
示例#7
0
 /**
  * {@inheritdoc}
  */
 function saveFile(BackupFileReadableInterface $file)
 {
     $out = array();
     // Quick and dirty way to html format this output
     if ($this->confGet('format') == 'html') {
         print '<pre>';
     }
     // Output the metadata
     if ($this->confGet('showmeta')) {
         print "---------------------\n";
         print "Metadata: \n";
         print_r($file->getMetaAll());
         print "---------------------\n";
     }
     // Output the body
     if ($this->confGet('showbody')) {
         print "---------------------\n";
         print "Body: \n";
         $max = $this->confGet('maxbody');
         $chunk = min($max, 1024);
         if ($file->openForRead()) {
             // Transfer file in 1024 byte chunks to save memory usage.
             while ($max > 0 && ($data = $file->readBytes($chunk))) {
                 print $data;
                 $max -= $chunk;
             }
             $file->close();
         }
         print "---------------------\n";
     }
     // Quick and dirty way to html format this output
     if ($this->confGet('format') == 'html') {
         print '</pre>';
     }
     exit;
 }
示例#8
0
 /**
  * This will be called when all files have been added. It gives the implementation
  * a chance to clean up and commit the changes if needed.
  *
  * @return mixed
  */
 public function closeArchive()
 {
     if ($this->archive) {
         $this->archive->close();
     }
 }
示例#9
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;
 }
 /**
  * Read a multiline sql command from a file.
  *
  * Supports the formatting created by mysqldump, but won't handle multiline comments.
  * @param \BackupMigrate\Core\File\BackupFileReadableInterface $file
  * @return string
  */
 protected function _readSQLCommand(BackupFileReadableInterface $file)
 {
     $out = '';
     while ($line = $file->readLine()) {
         $first2 = substr($line, 0, 2);
         $first3 = substr($line, 0, 2);
         // Ignore single line comments. This function doesn't support multiline comments or inline comments.
         if ($first2 != '--' && ($first2 != '/*' || $first3 == '/*!')) {
             $out .= ' ' . trim($line);
             // If a line ends in ; or */ it is a sql command.
             if (substr($out, strlen($out) - 1, 1) == ';') {
                 return trim($out);
             }
         }
     }
     return trim($out);
 }