Skip to content

Commit

Permalink
Add support for remote path
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Dec 24, 2022
1 parent c0eff2e commit 1fa0548
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/backup/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Config struct {
AzureStorageAccountName string `split_words:"true"`
AzureStoragePrimaryAccountKey string `split_words:"true"`
AzureStorageContainerName string `split_words:"true"`
AzureStoragePath string `split_words:"true"`
AzureStorageEndpoint string `split_words:"true" default:"https://{{ .AccountName }}.blob.core.windows.net/"`
}

Expand Down
1 change: 1 addition & 0 deletions cmd/backup/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func newScript() (*script, error) {
AccountName: s.c.AzureStorageAccountName,
PrimaryAccountKey: s.c.AzureStoragePrimaryAccountKey,
Endpoint: s.c.AzureStorageEndpoint,
RemotePath: s.c.AzureStoragePath,
}
azureBackend, err := azure.NewStorageBackend(azureConfig, logFunc)
if err != nil {
Expand Down
12 changes: 7 additions & 5 deletions internal/storage/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"sync"
"text/template"
"time"
Expand All @@ -31,6 +31,7 @@ type Config struct {
ContainerName string
PrimaryAccountKey string
Endpoint string
RemotePath string
}

// NewStorageBackend creates and initializes a new Azure Blob Storage backend.
Expand All @@ -57,7 +58,8 @@ func NewStorageBackend(opts Config, logFunc storage.Log) (storage.Backend, error
client: client,
containerName: opts.ContainerName,
StorageBackend: &storage.StorageBackend{
Log: logFunc,
DestinationPath: opts.RemotePath,
Log: logFunc,
},
}
return &storage, nil
Expand All @@ -74,11 +76,10 @@ func (b *azureBlobStorage) Copy(file string) error {
if err != nil {
return fmt.Errorf("(*azureBlobStorage).Copy: error opening file %s: %w", file, err)
}

_, err = b.client.UploadStream(
context.Background(),
b.containerName,
path.Base(file),
filepath.Join(b.DestinationPath, filepath.Base(file)),
fileReader,
nil,
)
Expand All @@ -91,8 +92,9 @@ func (b *azureBlobStorage) Copy(file string) error {
// Prune rotates away backups according to the configuration and provided
// deadline for the Azure Blob storage backend.
func (b *azureBlobStorage) Prune(deadline time.Time, pruningPrefix string) (*storage.PruneStats, error) {
lookupPrefix := filepath.Join(b.DestinationPath, pruningPrefix)
pager := b.client.NewListBlobsFlatPager(b.containerName, &container.ListBlobsFlatOptions{
Prefix: &pruningPrefix,
Prefix: &lookupPrefix,
})
var matches []string
var totalCount uint
Expand Down
1 change: 1 addition & 0 deletions test/azure/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ services:
AZURE_STORAGE_PRIMARY_ACCOUNT_KEY: Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==
AZURE_STORAGE_CONTAINER_NAME: test-container
AZURE_STORAGE_ENDPOINT: http://storage:10000/{{ .AccountName }}/
AZURE_STORAGE_PATH: 'path/to/backup'
BACKUP_FILENAME: test.tar.gz
BACKUP_CRON_EXPRESSION: 0 0 5 31 2 ?
BACKUP_RETENTION_DAYS: ${BACKUP_RETENTION_DAYS:-7}
Expand Down
4 changes: 2 additions & 2 deletions test/azure/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sleep 5
expect_running_containers "3"

docker-compose run --rm az_cli \
az storage blob download -f /dump/test.tar.gz -c test-container -n test.tar.gz
az storage blob download -f /dump/test.tar.gz -c test-container -n path/to/backup/test.tar.gz
tar -xvf ./local/test.tar.gz -C /tmp && test -f /tmp/backup/app_data/offen.db

pass "Found relevant files in untared remote backups."
Expand All @@ -32,7 +32,7 @@ sleep 5
docker-compose exec backup backup

docker-compose run --rm az_cli \
az storage blob download -f /dump/test.tar.gz -c test-container -n test.tar.gz
az storage blob download -f /dump/test.tar.gz -c test-container -n path/to/backup/test.tar.gz
test -f ./local/test.tar.gz

pass "Remote backups have not been deleted."
Expand Down

0 comments on commit 1fa0548

Please sign in to comment.