Skip to content

Commit 568ff13

Browse files
Merge pull request #382 from ddattee
Feat/Google Cloud Storage sync
2 parents 5b4bab1 + f2b3475 commit 568ff13

File tree

12 files changed

+744
-62
lines changed

12 files changed

+744
-62
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ If you are behind php 7.0 you can still use phpbu version [4.0.10](https://phar.
5050
+ Dropbox
5151
+ FTP
5252
+ Google Drive
53+
+ Google Cloud Storage
5354
+ OpenStack
5455
+ rsync
5556
+ SFTP

composer.json

Lines changed: 4 additions & 2 deletions
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",
@@ -79,7 +80,8 @@
7980
"google/apiclient":"Require ^2.0 to sync to Google Drive",
8081
"arhitector/yandex":"Require ^2.0 to sync to Yandex Disk",
8182
"microsoft/azure-storage-blob": "Require ^1.4 to sync to Azure Blob Storage",
82-
"phpmailer/phpmailer": "Require ^6.0 to receive logs via email"
83+
"phpmailer/phpmailer": "Require ^6.0 to receive logs via email",
84+
"google/cloud-storage": "Require ^1.42 to sync to Google Cloud Storage"
8385
},
8486
"bin": [
8587
"phpbu"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "googlecloudstorage",
3+
"options": {
4+
"secret": "backup/google/client_secret.json",
5+
"bucket": "my-bucket",
6+
"path": "remote/path"
7+
}
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<sync type="googlecloudstorage">
3+
<!-- google secret file -->
4+
<option name="secret" value="backup/google/client_secret.json"/>
5+
6+
<!-- bucket name to upload to -->
7+
<option name="bucket" value="backup/google/client_secret.json"/>
8+
9+
<!-- the remote path to upload to into the bucket -->
10+
<option name="path" value="remote/path"/>
11+
12+
<!-- optional cleanup configuration -->
13+
<option name="cleanup.type" value="quantity"/>
14+
<option name="cleanup.amount" value="10"/>
15+
</sync>

phpbu.schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@
278278
"Dropbox",
279279
"Ftp",
280280
"GoogleDrive",
281+
"GoogleCloudStorage",
281282
"RSync",
282283
"Sftp",
283284
"Softlayer",
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 $options
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+
}

0 commit comments

Comments
 (0)