Example #1
0
 /**
  * (non-PHPDoc)
  *
  * @see    \phpbu\App\Backup\Sync::sync()
  * @param  \phpbu\App\Backup\Target $target
  * @param  \phpbu\App\Result        $result
  * @throws \phpbu\App\Backup\Sync\Exception
  */
 public function sync(Target $target, Result $result)
 {
     $sourcePath = $target->getPathname();
     $dropboxPath = $this->path . $target->getFilename();
     $client = new DropboxApi\Client($this->token, "phpbu/1.1.0");
     $pathError = DropboxApi\Path::findErrorNonRoot($dropboxPath);
     if (substr(__FILE__, 0, 7) == 'phar://') {
         DropboxApi\RootCertificates::useExternalPaths();
     }
     if ($pathError !== null) {
         throw new Exception(sprintf('Invalid \'dropbox-path\': %s', $pathError));
     }
     $size = null;
     if (stream_is_local($sourcePath)) {
         $size = filesize($sourcePath);
     }
     try {
         $fp = fopen($sourcePath, 'rb');
         $res = $client->uploadFile($dropboxPath, DropboxApi\WriteMode::add(), $fp, $size);
         fclose($fp);
     } catch (\Exception $e) {
         throw new Exception($e->getMessage(), null, $e);
     }
     $result->debug('upload: done  (' . $res['size'] . ')');
 }
Example #2
0
 /**
  * Returns the executable for this action.
  *
  * @param  \phpbu\App\Backup\Target $target
  * @return \phpbu\App\Cli\Executable
  */
 public function getExecutable(Target $target)
 {
     if (null === $this->executable) {
         $this->executable = new Tar($this->pathToCommand);
         $this->executable->archiveDirectory($this->path);
         $archiveName = Tar::isCompressorValid($target->getCompressor()->getCommand()) ? $target->getPathname() : $target->getPathnamePlain();
         $this->executable->archiveTo($archiveName)->useCompression($target->getCompressor()->getCommand())->removeSourceDirectory(true);
     }
     return $this->executable;
 }
Example #3
0
 /**
  * (non-PHPDoc)
  *
  * @see    \phpbu\App\Backup\Sync::sync()
  * @param  \phpbu\App\Backup\Target $target
  * @param  \phpbu\App\Result        $result
  * @throws \phpbu\App\Backup\Sync\Exception
  */
 public function sync(Target $target, Result $result)
 {
     $sourcePath = $target->getPathname();
     $yandexDiskPath = $this->path . $target->getFilename();
     $this->disk = new Disk($this->token);
     $size = null;
     if (stream_is_local($sourcePath)) {
         $size = filesize($sourcePath);
     }
     $file = $this->disk->resource();
     $this->createFolders();
     try {
         $file = $this->disk->resource($yandexDiskPath);
         $file->getContents();
     } catch (NotFoundException $exc) {
         $file->upload($sourcePath, true);
     }
     if ($file->has()) {
         $result->debug('upload: done  (' . $size . ')');
     } else {
         $result->debug('upload: error while uploading file');
     }
 }
Example #4
0
 /**
  * Recursive backup collecting.
  *
  * @param string  $path
  * @param integer $depth
  */
 protected function collect($path, $depth)
 {
     $dItter = new DirectoryIterator($path);
     // collect all matching subdirs and get all the backup files
     if ($depth < $this->target->countChangingPathElements()) {
         foreach ($dItter as $i => $file) {
             if ($file->isDot()) {
                 continue;
             }
             // TODO: match directory against dir-regex Target::getChangingPathElements
             if ($file->isDir()) {
                 $this->collect($file->getPathname(), $depth + 1);
             }
         }
     } else {
         // create regex to match only created backup files
         $fileRegex = Str::datePlaceholdersToRegex($this->target->getFilenameRaw());
         if ($this->target->shouldBeCompressed()) {
             $fileRegex .= '.' . $this->target->getCompressor()->getSuffix();
         }
         /** @var \SplFileInfo $file */
         foreach ($dItter as $i => $file) {
             if ($file->isDir()) {
                 continue;
             }
             // skip currently created backup
             if ($file->getPathname() == $this->target->getPathname()) {
                 continue;
             }
             if (preg_match('#' . $fileRegex . '#i', $file->getFilename())) {
                 $index = date('YmdHis', $file->getMTime()) . '-' . $i . '-' . $file->getPathname();
                 $this->files[$index] = new File($file->getFileInfo());
             }
         }
     }
 }
Example #5
0
 /**
  * (non-PHPDoc)
  *
  * @see    \phpbu\App\Backup\Sync::sync()
  * @param  \phpbu\App\Backup\Target $target
  * @param  \phpbu\App\Result        $result
  * @throws \phpbu\App\Backup\Sync\Exception
  */
 public function sync(Target $target, Result $result)
 {
     // silence phpseclib
     $old = error_reporting(0);
     $sftp = new phpseclib\Net\SFTP($this->host);
     if (!$sftp->login($this->user, $this->password)) {
         error_reporting($old);
         throw new Exception(sprintf('authentication failed for %s@%s%s', $this->user, $this->host, empty($this->password) ? '' : ' with password ****'));
     }
     error_reporting($old);
     $remoteFilename = $target->getFilename();
     $localFile = $target->getPathname();
     if ('' !== $this->remotePath) {
         $remoteDirs = explode('/', $this->remotePath);
         foreach ($remoteDirs as $dir) {
             if (!$sftp->is_dir($dir)) {
                 $result->debug(sprintf('creating remote dir \'%s\'', $dir));
                 $sftp->mkdir($dir);
             }
             $result->debug(sprintf('change to remote dir \'%s\'', $dir));
             $sftp->chdir($dir);
         }
     }
     $result->debug(sprintf('store file \'%s\' as \'%s\'', $localFile, $remoteFilename));
     $result->debug(sprintf('last error \'%s\'', $sftp->getLastSFTPError()));
     /** @noinspection PhpInternalEntityUsedInspection */
     if (!$sftp->put($remoteFilename, $localFile, phpseclib\Net\SFTP::SOURCE_LOCAL_FILE)) {
         throw new Exception(sprintf('error uploading file: %s - %s', $localFile, $sftp->getLastSFTPError()));
     }
 }
Example #6
0
 /**
  * Create the Exec to run the 'mcrypt' command.
  *
  * @param  \phpbu\App\Backup\Target $target
  * @return \phpbu\App\Cli\Executable
  */
 public function getExecutable(Target $target)
 {
     if (null == $this->executable) {
         $this->executable = new Executable\Mcrypt($this->pathToMcrypt);
         $this->executable->useAlgorithm($this->algorithm)->useKey($this->key)->useKeyFile($this->keyFile)->useConfig($this->config)->useHash($this->hash)->saveAt($target->getPathname())->deleteUncrypted(!$this->keepUncrypted)->showStdErr($this->showStdErr);
     }
     return $this->executable;
 }
Example #7
0
 /**
  * (non-PHPDoc)
  *
  * @see    \phpbu\App\Backup\Sync::sync()
  * @param  \phpbu\App\backup\Target $target
  * @param  \phpbu\App\Result        $result
  * @throws \phpbu\App\Backup\Sync\Exception
  */
 public function sync(Target $target, Result $result)
 {
     $sourcePath = $target->getPathname();
     $targetPath = $this->path . $target->getFilename();
     $options = array('adapter' => ObjectStorage_Http_Client::SOCKET, 'timeout' => 20);
     $objectStorage = new ObjectStorage($this->host, $this->user, $this->secret, $options);
     $result->debug('softlayer source: ' . $sourcePath);
     $result->debug('softlayer target: ' . $targetPath);
     try {
         /** @var \ObjectStorage_Container $container */
         $container = $objectStorage->with($this->container . $targetPath)->setLocalFile($sourcePath)->setMeta('description', 'PHPBU Backup: ' . date('r', time()))->setHeader('Content-Type', $target->getMimeType());
         $container->create();
     } catch (\Exception $e) {
         throw new Exception($e->getMessage(), null, $e);
     }
     $result->debug('upload: done');
 }
Example #8
0
 /**
  * Execute the sync
  *
  * @see    \phpbu\App\Backup\Sync::sync()
  * @param  \phpbu\App\Backup\Target $target
  * @param  \phpbu\App\Result        $result
  * @throws \phpbu\App\Backup\Sync\Exception
  */
 public function sync(Target $target, Result $result)
 {
     $sourcePath = $target->getPathname();
     $targetPath = $this->path . $target->getFilename();
     $s3 = S3Client::factory(array('signature' => 'v4', 'region' => $this->region, 'credentials' => array('key' => $this->key, 'secret' => $this->secret)));
     try {
         $fh = fopen($sourcePath, 'r');
         $s3->upload($this->bucket, $targetPath, $fh, $this->acl);
     } catch (\Exception $e) {
         throw new Exception($e->getMessage(), null, $e);
     }
     $result->debug('upload: done');
 }
Example #9
0
 /**
  * Return final archive file.
  *
  * @param  \phpbu\App\Backup\Target $target
  * @return string
  */
 public function getArchiveFile(Target $target)
 {
     return $target->getPathname();
 }
Example #10
0
 /**
  * Setup the Executable to run the 'tar' command.
  *
  * @param  \phpbu\App\Backup\Target
  * @return \phpbu\App\Cli\Executable
  */
 public function getExecutable(Target $target)
 {
     if (null == $this->executable) {
         // check if tar supports requested compression
         if ($target->shouldBeCompressed()) {
             if (!Executable\Tar::isCompressorValid($target->getCompressor()->getCommand())) {
                 $this->pathToArchive = $target->getPathnamePlain();
             } else {
                 // compression could be handled by the tar command
                 $this->pathToArchive = $target->getPathname();
                 $this->compression = $target->getCompressor()->getCommand();
             }
         } else {
             // no compression at all
             $this->pathToArchive = $target->getPathname();
         }
         $this->executable = new Executable\Tar($this->pathToTar);
         $this->executable->archiveDirectory($this->path)->useCompression($this->compression)->archiveTo($this->pathToArchive)->showStdErr($this->showStdErr);
     }
     return $this->executable;
 }
Example #11
0
 /**
  * (non-PHPDoc)
  *
  * @see    \phpbu\App\Backup\Sync::sync()
  * @param  \phpbu\App\Backup\Target $target
  * @param  \phpbu\App\Result        $result
  * @throws \phpbu\App\Backup\Sync\Exception
  */
 public function sync(Target $target, Result $result)
 {
     // silence ftp errors
     $old = error_reporting(0);
     if (!($ftpConnection = ftp_connect($this->host))) {
         throw new Exception(sprintf('Unable to connect to ftp server %s', $this->host));
     }
     if (!ftp_login($ftpConnection, $this->user, $this->password)) {
         error_reporting($old);
         throw new Exception(sprintf('authentication failed for %s@%s%s', $this->user, $this->host, empty($this->password) ? '' : ' with password ****'));
     }
     $remoteFilename = $target->getFilename();
     $localFile = $target->getPathname();
     if ('' !== $this->remotePath) {
         $remoteDirs = explode('/', $this->remotePath);
         foreach ($remoteDirs as $dir) {
             if (!ftp_chdir($ftpConnection, $dir)) {
                 $result->debug(sprintf('creating remote dir \'%s\'', $dir));
                 ftp_mkdir($ftpConnection, $dir);
                 ftp_chdir($ftpConnection, $dir);
             } else {
                 $result->debug(sprintf('change to remote dir \'%s\'', $dir));
             }
         }
     }
     $result->debug(sprintf('store file \'%s\' as \'%s\'', $localFile, $remoteFilename));
     if (!ftp_put($ftpConnection, $remoteFilename, $localFile, FTP_BINARY)) {
         $error = error_get_last();
         $message = $error['message'];
         throw new Exception(sprintf('error uploading file: %s - %s', $localFile, $message));
     }
     error_reporting($old);
 }
Example #12
0
 /**
  * (non-PHPDoc)
  *
  * @see    \phpbu\App\Backup\Sync::sync()
  * @param  \phpbu\App\Backup\Target $target
  * @param  \phpbu\App\Result        $result
  * @throws \phpbu\App\Backup\Sync\Exception
  */
 public function sync(Target $target, Result $result)
 {
     $sourcePath = $target->getPathname();
     $targetPath = $this->path . $target->getFilename();
     $copy = new CopycomApi($this->appKey, $this->appSecret, $this->userKey, $this->userSecret);
     try {
         // open a file to upload
         $fh = fopen($sourcePath, 'rb');
         // upload the file in 1MB chunks
         $parts = array();
         while ($data = fread($fh, 1024 * 1024)) {
             $part = $copy->sendData($data);
             array_push($parts, $part);
         }
         fclose($fh);
         // finalize the file
         $copy->createFile($targetPath, $parts);
     } catch (\Exception $e) {
         throw new Exception($e->getMessage(), null, $e);
     }
     $result->debug('upload: done');
 }
Example #13
0
 /**
  * Return sync source.
  *
  * @param  \phpbu\App\Backup\Target
  * @return string
  */
 public function getSyncSource(Target $target)
 {
     return $this->isDirSync ? $target->getPath() : $target->getPathname();
 }
Example #14
0
 /**
  * Create the Exec to run the 'mcrypt' command.
  *
  * @param  \phpbu\App\Backup\Target $target
  * @return \phpbu\App\Cli\Executable
  */
 public function getExecutable(Target $target)
 {
     if (null == $this->executable) {
         $this->executable = new Executable\OpenSSL($this->pathToOpenSSL);
         $this->executable->encryptFile($target->getPathname());
         // use key or password to encrypt
         if (!empty($this->certFile)) {
             $this->executable->useSSLCert($this->certFile);
         } else {
             $this->executable->usePassword($this->password)->encodeBase64(true);
         }
         $this->executable->useAlgorithm($this->algorithm)->deleteUncrypted(!$this->keepUncrypted)->showStdErr($this->showStdErr);
     }
     return $this->executable;
 }