Skip to content

Commit db3201b

Browse files
Added copy.com sync support
1 parent 8cb90fa commit db3201b

File tree

8 files changed

+160
-4
lines changed

8 files changed

+160
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Get detailed information about all the features and a 'getting started' tutorial
2020
+ Comparing with previous backups
2121
* Sync backups to other locations
2222
+ Amazon s3 (work in progress)
23+
+ copy
2324
+ Dropbox
2425
+ rsync
2526
+ SFTP

build.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,21 @@
105105

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

108+
<copy file="${basedir}/vendor/barracuda/copy/LICENSE" tofile="${basedir}/build/phar/copy/LICENSE"/>
109+
<copy todir="${basedir}/build/phar/copy">
110+
<fileset dir="${basedir}/vendor/barracuda/copy/src">
111+
<include name="**/*.php" />
112+
<include name="**/*.crt" />
113+
</fileset>
114+
</copy>
115+
<copy todir="${basedir}/build/phar/eher">
116+
<fileset dir="${basedir}/vendor/eher/oauth/src">
117+
<include name="**/*.php" />
118+
</fileset>
119+
</copy>
120+
108121
<copy file="${basedir}/vendor/dropbox/dropbox-sdk/License.txt" tofile="${basedir}/build/phar/dropbox/LICENSE"/>
109-
<copy todir="${basedir}/build/phar/dropbox">
122+
<copy todir="${basedir}/build/phar/dropbox">
110123
<fileset dir="${basedir}/vendor/dropbox/dropbox-sdk/lib/Dropbox">
111124
<include name="**/*.php" />
112125
<include name="**/*.crt" />

build/phar-patch.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
'search' => array('$this->set(CURLOPT_CAINFO', '$this->set(CURLOPT_CAPATH'),
88
'replace' => array('//$this->set(CURLOPT_CAINFO', '//$this->set(CURLOPT_CAPATH'),
99
),
10+
array(
11+
'path' => __DIR__ . '/phar/copy/Api.php',
12+
'search' => array('curl_setopt($this->curl, CURLOPT_CAINFO'),
13+
'replace' => array('//curl_setopt($this->curl, CURLOPT_CAINFO'),
14+
),
1015
);
1116

1217
foreach ( $patches as $file ) {

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"ext-dom": "*",
2828
"ext-json": "*",
2929
"ext-spl": "*",
30+
"barracuda/copy": "1.1.*",
3031
"dropbox/dropbox-sdk": "1.1.*",
3132
"phpseclib/phpseclib": "2.0.*@dev",
3233
"phpunit/php-timer": "~1.0.2",

doc/config/sync/copycom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<sync type="copycom">
3+
<!-- mandatory -->
4+
<option name="app.key" value="myappkey" />
5+
6+
<!-- mandatory -->
7+
<option name="app.secret" value="mycrazylongapisecret" />
8+
9+
<!-- mandatory -->
10+
<option name="user.key" value="oauthuserkey" />
11+
12+
<!-- mandatory -->
13+
<option name="user.secret" value="oauthusersecret" />
14+
15+
<!-- mandatory -->
16+
<option name="path" value="/some/dir" />
17+
</sync>

src/App/Factory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ abstract class Factory
4343
'sizediffavgpercent' => '\\phpbu\\Backup\\Check\\SizeDiffAvgPercent',
4444
),
4545
'sync' => array(
46+
'copycom' => '\\phpbu\\Backup\\Sync\\Copycom',
4647
'dropbox' => '\\phpbu\\Backup\\Sync\\Dropbox',
4748
'ftp' => '\\phpbu\\Backup\\Sync\\Ftp',
4849
'rsync' => '\\phpbu\\Backup\\Sync\\Rsync',

src/Backup/Sync/Copycom.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
namespace phpbu\Backup\Sync;
3+
4+
use Dropbox as dbx;
5+
use phpbu\App\Result;
6+
use phpbu\Backup\Sync;
7+
use phpbu\Backup\Target;
8+
9+
/**
10+
* Copycom
11+
*
12+
* @package phpbu
13+
* @subpackage Backup
14+
* @author Sebastian Feldmann <sebastian@phpbu.de>
15+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
16+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
17+
* @link http://www.phpbu.de/
18+
* @since Class available since Release 1.1.1
19+
*/
20+
class Copycom implements Sync
21+
{
22+
/**
23+
* API access key
24+
*
25+
* @var string
26+
*/
27+
protected $appKey;
28+
29+
/**
30+
* API access token
31+
*
32+
* @var string
33+
*/
34+
protected $appSecret;
35+
36+
/**
37+
* API access key
38+
*
39+
* @var string
40+
*/
41+
protected $userKey;
42+
43+
/**
44+
* API access token
45+
*
46+
* @var string
47+
*/
48+
protected $userSecret;
49+
50+
/**
51+
* Remote path
52+
*
53+
* @var string
54+
*/
55+
protected $path;
56+
57+
/**
58+
* (non-PHPdoc)
59+
* @see \phpbu\Backup\Sync::setup()
60+
*/
61+
public function setup(array $config)
62+
{
63+
if (!class_exists('\\Barracuda\\Copy\\API')) {
64+
throw new Exception('Copy api not loaded: use composer "barracuda/copy": "1.1.*" to install');
65+
}
66+
if (!isset($config['app.key']) || '' == $config['app.key']) {
67+
throw new Exception('API access key is mandatory');
68+
}
69+
if (!isset($config['app.secret']) || '' == $config['app.secret']) {
70+
throw new Exception('API access secret is mandatory');
71+
}
72+
if (!isset($config['user.key']) || '' == $config['user.key']) {
73+
throw new Exception('User access key is mandatory');
74+
}
75+
if (!isset($config['user.secret']) || '' == $config['user.secret']) {
76+
throw new Exception('User access secret is mandatory');
77+
}
78+
if (!isset($config['path']) || '' == $config['path']) {
79+
throw new Exception('dropbox path is mandatory');
80+
}
81+
$this->appKey = $config['app.key'];
82+
$this->appSecret = $config['app.secret'];
83+
$this->userKey = $config['user.key'];
84+
$this->userSecret = $config['user.secret'];
85+
$this->path = $config['path'] . ( substr($config['path'], -1) !== '/' ? '/' : '' );
86+
}
87+
88+
/**
89+
* (non-PHPdoc)
90+
* @see \phpbu\Backup\Sync::sync()
91+
*/
92+
public function sync(Target $target, Result $result)
93+
{
94+
$sourcePath = $target->getPathnameCompressed();
95+
$targetPath = $this->path . $target->getFilenameCompressed();
96+
97+
$copy = new \Barracuda\Copy\API($this->appKey, $this->appSecret, $this->userKey, $this->userSecret);
98+
99+
try {
100+
// open a file to upload
101+
$fh = fopen($sourcePath, 'rb');
102+
// upload the file in 1MB chunks
103+
$parts = array();
104+
while ($data = fread($fh, 1024 * 1024)) {
105+
$part = $copy->sendData($data);
106+
array_push($parts, $part);
107+
}
108+
fclose($fh);
109+
// finalize the file
110+
$copy->createFile($targetPath, $parts);
111+
} catch (\Exception $e) {
112+
throw new Exception($e->getMessage(), null, $e);
113+
}
114+
$result->debug('upload: done');
115+
}
116+
}

src/Backup/Sync/Dropbox.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace phpbu\Backup\Sync;
33

4-
use Dropbox as dbx;
54
use phpbu\App\Result;
65
use phpbu\Backup\Sync;
76
use phpbu\Backup\Target;
@@ -47,6 +46,9 @@ class Dropbox implements Sync
4746
*/
4847
public function setup(array $config)
4948
{
49+
if (!class_exists('\\Dropbox\\Client')) {
50+
throw new Exception('Dropbox sdk not loaded: use composer "dropbox/dropbox-sdk": "1.1.*" to install');
51+
}
5052
if (!isset($config['token']) || '' == $config['token']) {
5153
throw new Exception('API access token is mandatory');
5254
}
@@ -66,9 +68,9 @@ public function sync(Target $target, Result $result)
6668
$sourcePath = $target->getPathnameCompressed();
6769
$dropboxPath = $this->path . $target->getFilenameCompressed();
6870

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

71-
$pathError = dbx\Path::findErrorNonRoot($dropboxPath);
73+
$pathError = \Dropbox\Path::findErrorNonRoot($dropboxPath);
7274
if ($pathError !== null) {
7375
throw new Exception('Invalid <dropbox-path>: ' . $pathError);
7476
}

0 commit comments

Comments
 (0)