/**
  * {@inheritdoc}
  */
 public function push(BackupInterface $backup)
 {
     $backupDirectory = $backup->getName();
     if (!$this->flysystem->has($backupDirectory) && !$this->flysystem->createDir($backupDirectory)) {
         throw new DestinationException(sprintf('Unable to create backup directory "%s" in flysystem destination.', $backupDirectory));
     }
     $removedBackupFiles = $this->getFiles($backupDirectory);
     /**
      * @var FileInterface $backupFile
      */
     foreach ($backup->getFiles() as $backupFile) {
         if (isset($removedBackupFiles[$backupFile->getRelativePath()])) {
             unset($removedBackupFiles[$backupFile->getRelativePath()]);
         }
         $path = $backupDirectory . '/' . $backupFile->getRelativePath();
         try {
             if ($this->flysystem->has($path)) {
                 if ($backupFile->getModifiedAt() > new \DateTime('@' . $this->flysystem->getTimestamp($path))) {
                     $resource = fopen($backupFile->getPath(), 'r');
                     $this->flysystem->updateStream($path, $resource);
                     fclose($resource);
                 }
             } else {
                 $resource = fopen($backupFile->getPath(), 'r');
                 $this->flysystem->putStream($path, $resource);
                 fclose($resource);
             }
         } catch (\Exception $e) {
             throw new DestinationException(sprintf('Unable to backup file "%s" to flysystem destination.', $backupFile->getPath()), 0, $e);
         }
     }
     /**
      * @var FileInterface $removedBackupFile
      */
     foreach ($removedBackupFiles as $removedBackupFile) {
         $path = $backupDirectory . '/' . $removedBackupFile->getRelativePath();
         try {
             $this->flysystem->delete($path);
         } catch (\Exception $e) {
             throw new DestinationException(sprintf('Unable to cleanup backup destination "%s" after backup process, file "%s" could not be removed.', $backupDirectory, $path), 0, $e);
         }
     }
     $this->removeEmptyDirectories($backupDirectory);
     if (is_array($this->backups)) {
         $this->backups[$backup->getName()] = $backup;
     }
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function push(BackupInterface $backup)
 {
     $backupDirectory = $this->directory . DIRECTORY_SEPARATOR . $backup->getName();
     try {
         $this->filesystem->mkdir($backupDirectory);
     } catch (\Exception $e) {
         throw new DestinationException(sprintf('Unable to create backup directory "%s" for backup "%s" in local destination.', $backupDirectory, $backup->getName()));
     }
     $removedBackupFiles = $this->getFiles($backupDirectory);
     /**
      * @var FileInterface $backupFile
      */
     foreach ($backup->getFiles() as $backupFile) {
         if (isset($removedBackupFiles[$backupFile->getRelativePath()])) {
             unset($removedBackupFiles[$backupFile->getRelativePath()]);
         }
         try {
             $this->filesystem->copy($backupFile->getPath(), sprintf('%s%s%s', $backupDirectory, DIRECTORY_SEPARATOR, $backupFile->getRelativePath()));
         } catch (\Exception $e) {
             throw new DestinationException(sprintf('Unable to backup file "%s" to destination "%s".', $backupFile->getPath(), $this->directory), 0, $e);
         }
     }
     /**
      * @var FileInterface $removedBackupFile
      */
     foreach ($removedBackupFiles as $removedBackupFile) {
         $path = $backupDirectory . DIRECTORY_SEPARATOR . $removedBackupFile->getRelativePath();
         try {
             $this->filesystem->remove($path);
         } catch (\Exception $e) {
             throw new DestinationException(sprintf('Unable to cleanup backup destination "%s" after backup process, file "%s" could not be removed.', $backupDirectory, $path), 0, $e);
         }
     }
     $this->removeEmptyDirectories($backupDirectory);
     if (is_array($this->backups)) {
         $this->backups[$backup->getName()] = $backup;
     }
 }