Skip to content

Commit 5fca080

Browse files
committed
Add google cloud storage sync adapter
1 parent 5b4bab1 commit 5fca080

File tree

8 files changed

+647
-1
lines changed

8 files changed

+647
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
"php-opencloud/openstack": "^3.0",
6666
"arhitector/yandex": "^2.0",
6767
"microsoft/azure-storage-blob": "^1.4",
68-
"phpmailer/phpmailer": "^6.0"
68+
"phpmailer/phpmailer": "^6.0",
69+
"google/cloud-storage": "^1.42"
6970
},
7071
"suggest": {
7172
"sebastianfeldmann/ftp": "Require ^0.9.2 to sync to an FTP server",
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace phpbu\App\Backup\Collector;
4+
5+
use Google\Cloud\Storage\Bucket;
6+
use Google\Cloud\Storage\StorageObject;
7+
use phpbu\App\Backup\Collector;
8+
use phpbu\App\Backup\File;
9+
use phpbu\App\Backup\Path;
10+
use phpbu\App\Backup\Target;
11+
12+
/**
13+
* GoogleCloud class.
14+
*
15+
* @package phpbu
16+
* @subpackage Backup
17+
* @author David Dattee <[email protected]>
18+
* @copyright Sebastian Feldmann <[email protected]>
19+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
20+
* @link http://phpbu.de/
21+
*/
22+
class GoogleCloudStorage extends Remote implements Collector
23+
{
24+
/**
25+
* Bucket to read from.
26+
*
27+
* @var Bucket
28+
*/
29+
private Bucket $bucket;
30+
31+
/**
32+
* @param Target $target
33+
* @param Bucket $bucket
34+
* @param Path $path
35+
*/
36+
public function __construct(Target $target, Bucket $bucket, Path $path)
37+
{
38+
$this->setUp($target, $path);
39+
$this->bucket = $bucket;
40+
}
41+
42+
/**
43+
* Collect all created backups.
44+
*
45+
* @return void
46+
*/
47+
protected function collectBackups()
48+
{
49+
/** @var StorageObject $object */
50+
foreach ($this->bucket->objects() as $object) {
51+
if ($this->isFileMatch($this->path->getPath() . '/' . $object->name())) {
52+
$file = new File\GoogleCloudStorage($object);
53+
54+
$this->files[$this->getFileIndex($file)] = $file;
55+
}
56+
}
57+
}
58+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace phpbu\App\Backup\File;
4+
5+
use Google\Cloud\Storage\StorageObject;
6+
use phpbu\App\Exception;
7+
8+
/**
9+
* Google Drive file class.
10+
*
11+
* @package phpbu
12+
* @subpackage Backup
13+
* @author David Dattee <[email protected]>
14+
* @copyright Sebastian Feldmann <[email protected]>
15+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
16+
* @link http://phpbu.de/
17+
*/
18+
class GoogleCloudStorage extends Remote
19+
{
20+
/**
21+
* Google Storage Object
22+
*
23+
* @var StorageObject
24+
*/
25+
private StorageObject $object;
26+
27+
/**
28+
* Constructor.
29+
*
30+
* @param StorageObject $googleStorageObject
31+
*/
32+
public function __construct(StorageObject $googleStorageObject)
33+
{
34+
$this->object = $googleStorageObject;
35+
$this->filename = $this->object->name();
36+
$this->pathname = $this->object->name();
37+
$this->size = (int)$this->object->info()['size'];
38+
$this->lastModified = strtotime($this->object->info()['updated']);
39+
}
40+
41+
/**
42+
* Deletes the file from Google Cloud Storage.
43+
*
44+
* @throws Exception
45+
*/
46+
public function unlink()
47+
{
48+
try {
49+
$this->object->delete();
50+
} catch (\Exception $e) {
51+
throw new Exception($e->getMessage());
52+
}
53+
}
54+
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
3+
namespace phpbu\App\Backup\Sync;
4+
5+
use Google\Cloud\Storage\StorageClient;
6+
use phpbu\App\Backup\Collector;
7+
use phpbu\App\Backup\Path;
8+
use phpbu\App\Backup\Target;
9+
use phpbu\App\Configuration;
10+
use phpbu\App\Result;
11+
use phpbu\App\Util;
12+
13+
/**
14+
* Google Drive
15+
*
16+
* @package phpbu
17+
* @subpackage Backup
18+
* @author David Dattee <[email protected]>
19+
* @copyright Sebastian Feldmann <[email protected]>
20+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
21+
* @link http://phpbu.de/
22+
*/
23+
class GoogleCloudStorage implements Simulator
24+
{
25+
use Cleanable;
26+
27+
/**
28+
* Google Gloud Storage client.
29+
*
30+
* @var StorageClient
31+
*/
32+
private $client;
33+
34+
/**
35+
* Google json secret file.
36+
*
37+
* @var string
38+
*/
39+
private $secret;
40+
41+
/**
42+
* Bucket to upload to
43+
*
44+
* @var string
45+
*/
46+
private $bucket;
47+
48+
/**
49+
* Path to upload to in the bucket
50+
*
51+
* @var string
52+
*/
53+
private $parent;
54+
55+
/**
56+
* (non-PHPDoc)
57+
*
58+
* @param array $config
59+
* @throws \phpbu\App\Exception
60+
* @see \phpbu\App\Backup\Sync::setup()
61+
*/
62+
public function setup(array $options)
63+
{
64+
if (!class_exists('\\Google\\Cloud\\Storage\\StorageClient')) {
65+
throw new Exception('google cloud api client not loaded: use composer to install "google/cloud-storage"');
66+
}
67+
if (!Util\Arr::isSetAndNotEmptyString($options, 'secret')) {
68+
throw new Exception('google secret json file is mandatory');
69+
}
70+
if (!Util\Arr::isSetAndNotEmptyString($options, 'bucket')) {
71+
throw new Exception('bucket to upload to is mandatory');
72+
}
73+
$this->parent = Util\Arr::getValue($options, 'path', '');
74+
75+
$this->setupAuthFiles($options);
76+
$this->setUpCleanable($options);
77+
}
78+
79+
/**
80+
* Make sure google authentication files exist and determine absolute path to them.
81+
*
82+
* @param array $config
83+
* @throws \phpbu\App\Backup\Sync\Exception
84+
*/
85+
private function setupAuthFiles(array $config)
86+
{
87+
$secret = Util\Path::toAbsolutePath($config['secret'], Configuration::getWorkingDirectory());
88+
if (!file_exists($secret)) {
89+
throw new Exception(sprintf('google secret json file not found at %s', $secret));
90+
}
91+
92+
$this->secret = $secret;
93+
$this->bucket = $config['bucket'];
94+
}
95+
96+
/**
97+
* Execute the Sync
98+
*
99+
* @param \phpbu\App\Backup\Target $target
100+
* @param \phpbu\App\Result $result
101+
* @throws \phpbu\App\Backup\Sync\Exception
102+
* @see \phpbu\App\Backup\Sync::sync()
103+
*/
104+
public function sync(Target $target, Result $result)
105+
{
106+
try {
107+
$this->client = $this->createGoogleCloudClient();
108+
$bucket = $this->client->bucket($this->bucket);
109+
$hash = $this->calculateHash($target->getPathname());
110+
111+
$sentObject = $bucket->upload(
112+
fopen($target->getPathname(), 'rb'),
113+
[
114+
'name' => ($this->parent ? $this->parent . '/' : '') . $target->getFilename(),
115+
'metadata' => [
116+
'crc32c' => $hash,
117+
],
118+
],
119+
);
120+
121+
$result->debug(sprintf('upload: done: %s', $sentObject->name()));
122+
$this->cleanup($target, $result);
123+
} catch (\Exception $e) {
124+
throw new Exception($e->getMessage(), 0, $e);
125+
}
126+
}
127+
128+
private function calculateHash(string $filePath): string
129+
{
130+
return base64_encode(hex2bin(hash_file('crc32c', $filePath)));
131+
}
132+
133+
/**
134+
* Simulate the sync execution.
135+
*
136+
* @param \phpbu\App\Backup\Target $target
137+
* @param \phpbu\App\Result $result
138+
*/
139+
public function simulate(Target $target, Result $result)
140+
{
141+
$result->debug('sync backup to google cloud storage' . PHP_EOL);
142+
143+
$this->isSimulation = true;
144+
$this->simulateRemoteCleanup($target, $result);
145+
}
146+
147+
/**
148+
* Setup google api client and google drive service.
149+
*/
150+
protected function createGoogleCloudClient(): StorageClient
151+
{
152+
if (!$this->client) {
153+
$this->client = new StorageClient(['keyFilePath' => $this->secret]);
154+
}
155+
156+
return $this->client;
157+
}
158+
159+
/**
160+
* Creates collector for remote cleanup.
161+
*
162+
* @param \phpbu\App\Backup\Target $target
163+
* @return \phpbu\App\Backup\Collector
164+
*/
165+
protected function createCollector(Target $target): Collector
166+
{
167+
return new Collector\GoogleCloudStorage(
168+
$target,
169+
$this->createGoogleCloudClient()->bucket($this->bucket),
170+
new Path($this->parent)
171+
);
172+
}
173+
}

src/Factory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class Factory
7777
'dropbox' => '\\phpbu\\App\\Backup\\Sync\\Dropbox',
7878
'ftp' => '\\phpbu\\App\\Backup\\Sync\\Ftp',
7979
'googledrive' => '\\phpbu\\App\\Backup\\Sync\\GoogleDrive',
80+
'googlecloud' => '\\phpbu\\App\\Backup\\Sync\\GoogleCloudStorage',
8081
'rsync' => '\\phpbu\\App\\Backup\\Sync\\Rsync',
8182
'sftp' => '\\phpbu\\App\\Backup\\Sync\\Sftp',
8283
'softlayer' => '\\phpbu\\App\\Backup\\Sync\\SoftLayer',
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace phpbu\App\Backup\Collector;
4+
5+
use Google\Cloud\Storage\Bucket;
6+
use Google\Cloud\Storage\StorageObject;
7+
use phpbu\App\Backup\Path;
8+
use phpbu\App\Backup\Target;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/**
12+
* Google Drive collector test.
13+
*
14+
* @package phpbu
15+
* @subpackage tests
16+
* @author David Dattée <[email protected]>
17+
* @copyright Sebastian Feldmann <[email protected]>
18+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
19+
* @link http://www.phpbu.de/
20+
*/
21+
class GoogleCloudStorageTest extends TestCase
22+
{
23+
/**
24+
* Tests GoogleDrive::getBackupFiles
25+
*/
26+
public function testCollector()
27+
{
28+
$path = '/collector/static-dir/';
29+
$filename = 'foo-%Y-%m-%d-%H_%i.txt';
30+
$target = new Target($path, $filename, strtotime('2014-12-07 04:30:57'));
31+
$bucket = $this->createMock(Bucket::class);
32+
$path = $this->createMock(Path::class);
33+
34+
$remoteFileList = [
35+
[
36+
'name' => $target->getFilename(),
37+
'size' => 100,
38+
'last_modified' => '2018-05-08 14:14:54.0 +00:00',
39+
],
40+
[
41+
'name' => 'foo-2000-12-01-12_00.txt',
42+
'size' => 200,
43+
'last_modified' => '2000-12-01 12:00:00.0 +00:00',
44+
],
45+
[
46+
'name' => 'not-matching-2000-12-01-12_00.txt',
47+
'size' => 300,
48+
'last_modified' => '2000-12-01 12:00:00.0 +00:00',
49+
],
50+
];
51+
52+
$remoteFileList = array_map(
53+
function ($item) {
54+
return $this->createGoogleStorageObjectFileStub($item);
55+
},
56+
$remoteFileList,
57+
);
58+
59+
$bucket
60+
->expects($this->once())
61+
->method('objects')
62+
->willReturn($remoteFileList);
63+
64+
$collector = new GoogleCloudStorage($target, $bucket, $path);
65+
$files = $collector->getBackupFiles();
66+
67+
$this->assertCount(2, $files);
68+
$this->assertArrayHasKey('975672000-foo-2000-12-01-12_00.txt-1', $files);
69+
$this->assertEquals(
70+
'foo-2000-12-01-12_00.txt',
71+
$files['975672000-foo-2000-12-01-12_00.txt-1']->getFilename(),
72+
);
73+
}
74+
75+
/**
76+
* Creates Google Storage Object file class mock.
77+
*
78+
* @param array $data
79+
* @return StorageObject
80+
*/
81+
private function createGoogleStorageObjectFileStub(array $data): StorageObject
82+
{
83+
$object = $this->createMock(StorageObject::class);
84+
$object->method('name')->willReturn($data['name']);
85+
$object->method('info')->willReturn([
86+
'size' => $data['size'],
87+
'updated' => $data['last_modified'],
88+
]);
89+
90+
return $object;
91+
}
92+
}

0 commit comments

Comments
 (0)