Backup Destinations
Backblaze B2
A guide on how to set up Backblaze B2 as a cloud destination for Craft Remote Sync
Overview
Backblaze B2 is easy to set up. You need to create a new bucket and generate credentials for that bucket.
Setup
Backblaze B2 implements the AWS S3 SDK exactly, so we can use the same PHP SDK that we would use for an AWS connection.
Therefore, go get started install the official AWS PHP SDK:
composer require aws/aws-sdk-php:^3
Configure Backblaze
Login to your Backblaze account and make sure you have enabled B2 storage in your My Account > My Settings
:
Create Bucket
Create a new bucket:
Making sure it's marked "private." When created, take note of the region as we'll need to add this to our settings:
Take now of the bucket name as we'll be using it in the next step.
Create an App Key
Now go to App Keys
in the sidebar and click "Add a New Application Key".
Bear in mind that Backblaze offers two key types when programmatically accessing their API:
- a master application key
- an application key
This is a bit confusing. The former master application key gives access to everything. A non-master application key allows you to narrow down access to particular features instead.
We want to use a regular application key, not a master key (the Backblaze B2 API won't work with the PHP SDK if we use a master key)
- When adding the key, make sure to select the bucket you just created.
- Also make sure to click "Allow List All Bucket Names"
Once created, make sure to take note of these details as we'll be using them in the next step (and they don't appear again):
Configure Craft
We now need to configure Remote Sync to use our bucket. The best way to do this is to create some environment variables in our Craft project's .env
file:
BACKBLAZE_B2_KEY_ID="..."
BACKBLAZE_B2_APP_KEY="..."
BACKBLAZE_B2_REGION_NAME="us-west-002" # don't include the "s3"
BACKBLAZE_B2_BUCKET_NAME="craft-sync"
BACKBLAZE_B2_BUCKET_PATH="mysite"
And then reference these in the settings page of Remote Sync:
Alternatively you can just enter these values directly into the input boxes, but this means these values will be committed to your projects.yaml
file.
You should now be ready to start syncing files.
Optional Bucket Folder Path
You can add an optional folder path within your bucket to save files. Make sure the path doesn't beging or end with a slash and also only contains letters, numbers and -
/_
:
Valid examples:
project-xyz/sync
my-sync
Invalid example:
Project XYZ/sync
/project-xyz/sync
/my-sync
my-sync/
Troubleshooting
If you are encountering errors, first check your credentials. After that, have a look in storage/logs/web.log
for errors. Usually these issues are related to credentials or folder paths. If you're still having trouble, see the troubleshooting section.
Malformed Access Key ID
If you are noticing an error in the logs similar to:
2020-07-13 17:29:03 [-][1][-][trace][yii\base\InlineAction::runWithParams] Running action: weareferal\remotesync\controllers\RemoteSyncController::actionListDatabases()
2020-07-13 17:29:04 [-][1][-][error][Aws\S3\Exception\S3Exception] exception 'Aws\S3\Exception\S3Exception' with message 'Error executing "ListObjects" on "https://craft-sync.s3.us-west-002.backblazeb2.com/?prefix=craft-remote-sync%2Fcraft-test&encoding-type=url"; AWS HTTP error: Client error: `GET https://craft-sync.s3.us-west-002.backblazeb2.com/?prefix=craft-remote-sync%2Fcraft-test&encoding-type=url` resulted in a `403 Forbidden` response:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Error>
<Code>InvalidAccessKeyId</Code>
<Message>Malform (truncated...)
InvalidAccessKeyId (client): Malformed Access Key Id - <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Error>
<Code>InvalidAccessKeyId</Code>
<Message>Malformed Access Key Id</Message>
</Error>
Make sure that you are using a regular app key and not the master app key from your Backblaze account. To access Backblaze using the S3 PHP SDK you need to set up a specific app key that has access to your new bucket.