Skip to content

Commit 450d394

Browse files
jbouzekrisebastianfeldmann
authored andcommitted
add sync to azure blob
1 parent 2c084ba commit 450d394

File tree

8 files changed

+427
-1
lines changed

8 files changed

+427
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ If you are not using php 7.0 or greater already you can still use phpbu version
4242
+ openssl
4343
* Sync backups to other locations
4444
+ Amazon s3
45+
+ Azure Blob
4546
+ Dropbox
4647
+ FTP
4748
+ Google Drive

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
"php-opencloud/openstack": "Require ~3.0 to sync to OpenStack",
6868
"vlucas/phpdotenv": "Require ~2.4 to use Dotenv adapter",
6969
"google/apiclient":"Require ~2.0 to sync to Google Drive",
70-
"arhitector/yandex":"Require ~2.0 to sync to Yandex Disk"
70+
"arhitector/yandex":"Require ~2.0 to sync to Yandex Disk",
71+
"microsoft/azure-storage-blob": "Require ~1.4 to sync to Azure Blob Storage"
7172
},
7273
"bin": [
7374
"phpbu"

doc/config/sync/azureblob.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "azureblob",
3+
"options": {
4+
"connection_string": "DefaultEndpointsProtocol=[http|https];AccountName=[yourAccount];AccountKey=[yourKey]",
5+
"container_name": "mycontainer",
6+
"path": "backup"
7+
}
8+
}

doc/config/sync/azureblob.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<sync type="azureblob">
3+
<!-- mandatory -->
4+
<option name="connection_string" value="DefaultEndpointsProtocol=[http|https];AccountName=[yourAccount];AccountKey=[yourKey]" />
5+
6+
<!-- mandatory -->
7+
<option name="container_name" value="mycontainer" />
8+
9+
<!-- optional, default / -->
10+
<option name="path" value="backup" />
11+
</sync>

src/Backup/Collector/AzureBlob.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
namespace phpbu\App\Backup\Collector;
3+
4+
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
5+
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
6+
use phpbu\App\Backup\Collector;
7+
use phpbu\App\Backup\File\AzureBlob as BlobFile;
8+
use phpbu\App\Backup\Path;
9+
use phpbu\App\Backup\Target;
10+
use phpbu\App\Util;
11+
12+
/**
13+
* AzureBlob class.
14+
*
15+
* @package phpbu
16+
* @subpackage Backup
17+
* @author Sebastian Feldmann <sebastian@phpbu.de>
18+
* @author Jonathan Bouzekri <jonathan.bouzekri@gmail.com>
19+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
20+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
21+
* @link http://phpbu.de/
22+
* @since Class available since Release 5.2.7
23+
*/
24+
class AzureBlob extends Remote implements Collector
25+
{
26+
/**
27+
* @var \MicrosoftAzure\Storage\Blob\BlobRestProxy
28+
*/
29+
protected $client;
30+
31+
/**
32+
* Azure Blob Storage Container name
33+
*
34+
* @var string
35+
*/
36+
protected $containerName;
37+
38+
/**
39+
* Amazon S3 constructor.
40+
*
41+
* @param \phpbu\App\Backup\Target $target
42+
* @param \phpbu\App\Backup\Path $path
43+
* @param \MicrosoftAzure\Storage\Blob\BlobRestProxy $client
44+
* @param string $containerName
45+
*/
46+
public function __construct(Target $target, Path $path, BlobRestProxy $client, string $containerName)
47+
{
48+
$this->setUp($target, $path);
49+
$this->client = $client;
50+
$this->containerName = $containerName;
51+
}
52+
53+
/**
54+
* Collect all created backups.
55+
*/
56+
protected function collectBackups()
57+
{
58+
$listBlobsOptions = new ListBlobsOptions();
59+
$listBlobsOptions->setPrefix($this->getPrefix($this->path->getPathThatIsNotChanging()));
60+
$listBlobsOptions->setMaxResults(10);
61+
62+
do {
63+
$blobList = $this->client->listBlobs($this->containerName, $listBlobsOptions);
64+
foreach ($blobList->getBlobs() as $blob) {
65+
if ($this->isFileMatch($blob->getName())) {
66+
$file = new BlobFile($this->client, $this->containerName, $blob);
67+
$index = $this->getFileIndex($file);
68+
$this->files[$index] = $file;
69+
}
70+
}
71+
$listBlobsOptions->setContinuationToken($blobList->getContinuationToken());
72+
} while ($blobList->getContinuationToken());
73+
}
74+
75+
/**
76+
* Return prefix for querying remote files and folders
77+
*
78+
* @param string $path
79+
* @return string
80+
*/
81+
protected function getPrefix($path): string
82+
{
83+
$prefix = Util\Path::withoutLeadingSlash($path);
84+
$prefix = $prefix ? Util\Path::withTrailingSlash($prefix) : '';
85+
return $prefix;
86+
}
87+
}

src/Backup/File/AzureBlob.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
namespace phpbu\App\Backup\File;
3+
4+
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
5+
use MicrosoftAzure\Storage\Blob\Models\Blob;
6+
use phpbu\App\Exception;
7+
8+
/**
9+
* AzureBlob class.
10+
*
11+
* @package phpbu
12+
* @subpackage Backup
13+
* @author Sebastian Feldmann <sebastian@phpbu.de>
14+
* @author Jonathan Bouzekri <jonathan.bouzekri@gmail.com>
15+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
16+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
17+
* @link http://phpbu.de/
18+
* @since Class available since Release 5.2.7
19+
*/
20+
class AzureBlob extends Remote
21+
{
22+
/**
23+
* Azure Blob client.
24+
*
25+
* @var BlobRestProxy
26+
*/
27+
private $client;
28+
29+
/**
30+
* @var string
31+
*/
32+
protected $containerName;
33+
34+
/**
35+
* AzureBlob constructor.
36+
*
37+
* @param BlobRestProxy $client
38+
* @param string $containerName
39+
* @param Blob $blob
40+
*/
41+
public function __construct(BlobRestProxy $client, string $containerName, Blob $blob)
42+
{
43+
$this->client = $client;
44+
$this->containerName = $containerName;
45+
$this->filename = basename($blob->getName());
46+
$this->pathname = $blob->getName();
47+
$props = $blob->getProperties();
48+
$this->size = $props->getContentLength();
49+
$this->lastModified = $props->getLastModified()->getTimestamp();
50+
}
51+
52+
/**
53+
* Deletes the file.
54+
*
55+
* @throws \phpbu\App\Exception
56+
*/
57+
public function unlink()
58+
{
59+
try {
60+
$this->client->deleteBlob($this->containerName, $this->pathname);
61+
} catch (\Exception $exception) {
62+
throw new Exception($exception->getMessage());
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)