Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup to google storage #27

Merged
merged 7 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ env:
# GCP Credentials
TEST_GCP_STORAGE_API_URL: "https://connection.europe-west3.gcp.keboola.com/"
TEST_GCP_STORAGE_API_TOKEN: "${{ secrets.TEST_GCP_STORAGE_API_TOKEN }}"

TEST_GCP_BUCKET: "ci-php-kbc-project-backup"
TEST_GCP_SERVICE_ACCOUNT: "${{ secrets.TEST_GCP_SERVICE_ACCOUNT }}"
jobs:
build:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -66,5 +67,7 @@ jobs:
-e TEST_AZURE_CONTAINER_NAME
-e TEST_GCP_STORAGE_API_URL
-e TEST_GCP_STORAGE_API_TOKEN
-e TEST_GCP_BUCKET
-e TEST_GCP_SERVICE_ACCOUNT
${{env.APP_IMAGE}}
composer ci
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@ TEST_AZURE_STORAGE_API_TOKEN=
TEST_AZURE_ACCOUNT_NAME=
TEST_AZURE_ACCOUNT_KEY=
TEST_AZURE_CONTAINER_NAME=
TEST_GCP_STORAGE_API_URL=
TEST_GCP_STORAGE_API_TOKEN=
TEST_GCP_BUCKET=
TEST_GCP_SERVICE_ACCOUNT=
```

- `TEST_AWS_STORAGE_API_*` variables are from the project with AWS S3 backend whhich you want to backup
- `TEST_AWS_STORAGE_API_*` variables are from the project with AWS S3 backend which you want to backup
- `TEST_AWS_*` variables are from the S3 bucket the backup will be stored to _(Use [aws-cf-template.json](./aws-cf-template.json) CloudFormation stack template to create all required AWS resources)_
- `TEST_AZURE_STORAGE_API_*` variables are from the project with Azure Blob storage backend which you want to backup
- `TEST_AZURE_ACCOUNT_` create new Storage Account in your Azure Subscription
- `TEST_AZURE_CONTAINER_NAME` container whhich will be created in your Storage Account
- `TEST_AZURE_CONTAINER_NAME` container which will be created in your Storage Account
- `TEST_GCP_STORAGE_API_*` variables are from the project with GCP Storage backend which you want to backup
- `TEST_GCP_BUCKET` bucket in your Google Storage
- `TEST_GCP_SERVICE_ACCOUNT` Service account with permissions to write to the bucket

```bash
docker-compose run --rm tests
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ services:
- TEST_AZURE_CONTAINER_NAME
- TEST_GCP_STORAGE_API_URL
- TEST_GCP_STORAGE_API_TOKEN
- TEST_GCP_BUCKET
- TEST_GCP_SERVICE_ACCOUNT
69 changes: 69 additions & 0 deletions src/GcsBackup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Keboola\ProjectBackup;

use DateTimeImmutable;
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Storage\StorageObject;
use Keboola\StorageApi\Client as StorageApi;
use Psr\Log\LoggerInterface;

class GcsBackup extends Backup
{
private array $signedUrls = [];

public function __construct(
StorageApi $sapiClient,
readonly StorageClient $storageClient,
readonly string $bucketName,
readonly string $path,
readonly bool $generateSignedUrls = false,
?LoggerInterface $logger = null,
) {
parent::__construct($sapiClient, $logger);
}

/**
* @param resource|string $content
*/
protected function putToStorage(string $name, $content): void
{
$bucket = $this->storageClient->bucket($this->bucketName);
$object = $bucket->upload($content, [
'name' => sprintf('%s%s', $this->path, $name),
]);

if ($this->generateSignedUrls) {
$this->buildTreeFromPath($object, $name);
}
}

public function backupSignedUrls(): void
{
$name = 'signedUrls.json';
$bucket = $this->storageClient->bucket($this->bucketName);
$content = (string) json_encode($this->signedUrls, JSON_PRETTY_PRINT);

$bucket->upload($content, [
'name' => sprintf('%s%s', $this->path, $name),
]);
}

private function buildTreeFromPath(StorageObject $object, string $path): void
{
$parts = explode('/', $path);
$filename = pathinfo(array_pop($parts), PATHINFO_BASENAME);

$current = &$this->signedUrls;
foreach ($parts as $part) {
if (!isset($current[$part])) {
$current[$part] = [];
}
$current = &$current[$part];
}

$current[$filename] = $object->signedUrl(new DateTimeImmutable('+2 days'));
}
}
6 changes: 5 additions & 1 deletion tests/FileClient/AbstractFileClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ protected function createTableInStorage(string $tableId): void
$tableId = sprintf('%s.%s', $bucketId, $table);

if (!$this->storageClient->bucketExists($bucketId)) {
$this->storageClient->createBucket($bucket, $stage);
$bucketName = $bucket;
if (str_starts_with($bucket, 'c-')) {
$bucketName = substr($bucket, 2);
}
$this->storageClient->createBucket($bucketName, $stage);
}
if ($this->storageClient->tableExists($tableId)) {
$this->storageClient->dropTable($tableId);
Expand Down
Loading
Loading