Skip to content

Commit

Permalink
Added copy.com sync support
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianfeldmann committed Feb 9, 2015
1 parent 8cb90fa commit db3201b
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Get detailed information about all the features and a 'getting started' tutorial
+ Comparing with previous backups
* Sync backups to other locations
+ Amazon s3 (work in progress)
+ copy
+ Dropbox
+ rsync
+ SFTP
Expand Down
15 changes: 14 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,21 @@

<exec executable="${basedir}/build/phar-manifest.php" output="${basedir}/build/phar/manifest.txt"/>

<copy file="${basedir}/vendor/barracuda/copy/LICENSE" tofile="${basedir}/build/phar/copy/LICENSE"/>
<copy todir="${basedir}/build/phar/copy">
<fileset dir="${basedir}/vendor/barracuda/copy/src">
<include name="**/*.php" />
<include name="**/*.crt" />
</fileset>
</copy>
<copy todir="${basedir}/build/phar/eher">
<fileset dir="${basedir}/vendor/eher/oauth/src">
<include name="**/*.php" />
</fileset>
</copy>

<copy file="${basedir}/vendor/dropbox/dropbox-sdk/License.txt" tofile="${basedir}/build/phar/dropbox/LICENSE"/>
<copy todir="${basedir}/build/phar/dropbox">
<copy todir="${basedir}/build/phar/dropbox">
<fileset dir="${basedir}/vendor/dropbox/dropbox-sdk/lib/Dropbox">
<include name="**/*.php" />
<include name="**/*.crt" />
Expand Down
5 changes: 5 additions & 0 deletions build/phar-patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
'search' => array('$this->set(CURLOPT_CAINFO', '$this->set(CURLOPT_CAPATH'),
'replace' => array('//$this->set(CURLOPT_CAINFO', '//$this->set(CURLOPT_CAPATH'),
),
array(
'path' => __DIR__ . '/phar/copy/Api.php',
'search' => array('curl_setopt($this->curl, CURLOPT_CAINFO'),
'replace' => array('//curl_setopt($this->curl, CURLOPT_CAINFO'),
),
);

foreach ( $patches as $file ) {
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"ext-dom": "*",
"ext-json": "*",
"ext-spl": "*",
"barracuda/copy": "1.1.*",
"dropbox/dropbox-sdk": "1.1.*",
"phpseclib/phpseclib": "2.0.*@dev",
"phpunit/php-timer": "~1.0.2",
Expand Down
17 changes: 17 additions & 0 deletions doc/config/sync/copycom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<sync type="copycom">
<!-- mandatory -->
<option name="app.key" value="myappkey" />

<!-- mandatory -->
<option name="app.secret" value="mycrazylongapisecret" />

<!-- mandatory -->
<option name="user.key" value="oauthuserkey" />

<!-- mandatory -->
<option name="user.secret" value="oauthusersecret" />

<!-- mandatory -->
<option name="path" value="/some/dir" />
</sync>
1 change: 1 addition & 0 deletions src/App/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ abstract class Factory
'sizediffavgpercent' => '\\phpbu\\Backup\\Check\\SizeDiffAvgPercent',
),
'sync' => array(
'copycom' => '\\phpbu\\Backup\\Sync\\Copycom',
'dropbox' => '\\phpbu\\Backup\\Sync\\Dropbox',
'ftp' => '\\phpbu\\Backup\\Sync\\Ftp',
'rsync' => '\\phpbu\\Backup\\Sync\\Rsync',
Expand Down
116 changes: 116 additions & 0 deletions src/Backup/Sync/Copycom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
namespace phpbu\Backup\Sync;

use Dropbox as dbx;
use phpbu\App\Result;
use phpbu\Backup\Sync;
use phpbu\Backup\Target;

/**
* Copycom
*
* @package phpbu
* @subpackage Backup
* @author Sebastian Feldmann <sebastian@phpbu.de>
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @link http://www.phpbu.de/
* @since Class available since Release 1.1.1
*/
class Copycom implements Sync
{
/**
* API access key
*
* @var string
*/
protected $appKey;

/**
* API access token
*
* @var string
*/
protected $appSecret;

/**
* API access key
*
* @var string
*/
protected $userKey;

/**
* API access token
*
* @var string
*/
protected $userSecret;

/**
* Remote path
*
* @var string
*/
protected $path;

/**
* (non-PHPdoc)
* @see \phpbu\Backup\Sync::setup()
*/
public function setup(array $config)
{
if (!class_exists('\\Barracuda\\Copy\\API')) {
throw new Exception('Copy api not loaded: use composer "barracuda/copy": "1.1.*" to install');
}
if (!isset($config['app.key']) || '' == $config['app.key']) {
throw new Exception('API access key is mandatory');
}
if (!isset($config['app.secret']) || '' == $config['app.secret']) {
throw new Exception('API access secret is mandatory');
}
if (!isset($config['user.key']) || '' == $config['user.key']) {
throw new Exception('User access key is mandatory');
}
if (!isset($config['user.secret']) || '' == $config['user.secret']) {
throw new Exception('User access secret is mandatory');
}
if (!isset($config['path']) || '' == $config['path']) {
throw new Exception('dropbox path is mandatory');
}
$this->appKey = $config['app.key'];
$this->appSecret = $config['app.secret'];
$this->userKey = $config['user.key'];
$this->userSecret = $config['user.secret'];
$this->path = $config['path'] . ( substr($config['path'], -1) !== '/' ? '/' : '' );
}

/**
* (non-PHPdoc)
* @see \phpbu\Backup\Sync::sync()
*/
public function sync(Target $target, Result $result)
{
$sourcePath = $target->getPathnameCompressed();
$targetPath = $this->path . $target->getFilenameCompressed();

$copy = new \Barracuda\Copy\API($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');
}
}
8 changes: 5 additions & 3 deletions src/Backup/Sync/Dropbox.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace phpbu\Backup\Sync;

use Dropbox as dbx;
use phpbu\App\Result;
use phpbu\Backup\Sync;
use phpbu\Backup\Target;
Expand Down Expand Up @@ -47,6 +46,9 @@ class Dropbox implements Sync
*/
public function setup(array $config)
{
if (!class_exists('\\Dropbox\\Client')) {
throw new Exception('Dropbox sdk not loaded: use composer "dropbox/dropbox-sdk": "1.1.*" to install');
}
if (!isset($config['token']) || '' == $config['token']) {
throw new Exception('API access token is mandatory');
}
Expand All @@ -66,9 +68,9 @@ public function sync(Target $target, Result $result)
$sourcePath = $target->getPathnameCompressed();
$dropboxPath = $this->path . $target->getFilenameCompressed();

$client = new dbx\Client($this->token, "phpbu/1.1.0");
$client = new \Dropbox\Client($this->token, "phpbu/1.1.0");

$pathError = dbx\Path::findErrorNonRoot($dropboxPath);
$pathError = \Dropbox\Path::findErrorNonRoot($dropboxPath);
if ($pathError !== null) {
throw new Exception('Invalid <dropbox-path>: ' . $pathError);
}
Expand Down

0 comments on commit db3201b

Please sign in to comment.