2022-08-18 10:11:13 +02:00
|
|
|
// Copyright 2022 - Offen Authors <hioffen@posteo.de>
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-08-18 08:52:09 +02:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-08-18 10:11:13 +02:00
|
|
|
"fmt"
|
2022-08-18 08:52:09 +02:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
"github.com/offen/docker-volume-backup/internal/storage"
|
|
|
|
"github.com/offen/docker-volume-backup/internal/utilities"
|
|
|
|
)
|
|
|
|
|
|
|
|
type s3Storage struct {
|
|
|
|
*storage.StorageBackend
|
|
|
|
client *minio.Client
|
|
|
|
bucket string
|
|
|
|
storageClass string
|
|
|
|
}
|
|
|
|
|
2022-08-18 10:11:13 +02:00
|
|
|
// Config contains values that define the configuration of a S3 backend.
|
|
|
|
type Config struct {
|
|
|
|
Endpoint string
|
|
|
|
AccessKeyID string
|
|
|
|
SecretAccessKey string
|
|
|
|
IamRoleEndpoint string
|
|
|
|
EndpointProto string
|
|
|
|
EndpointInsecure bool
|
|
|
|
RemotePath string
|
|
|
|
BucketName string
|
|
|
|
StorageClass string
|
|
|
|
}
|
|
|
|
|
2022-08-18 08:52:09 +02:00
|
|
|
// NewStorageBackend creates and initializes a new S3/Minio storage backend.
|
2022-08-18 10:11:13 +02:00
|
|
|
func NewStorageBackend(opts Config, logFunc storage.Log) (storage.Backend, error) {
|
2022-08-18 08:52:09 +02:00
|
|
|
|
|
|
|
var creds *credentials.Credentials
|
2022-08-18 10:11:13 +02:00
|
|
|
if opts.AccessKeyID != "" && opts.SecretAccessKey != "" {
|
2022-08-18 08:52:09 +02:00
|
|
|
creds = credentials.NewStaticV4(
|
2022-08-18 10:11:13 +02:00
|
|
|
opts.AccessKeyID,
|
|
|
|
opts.SecretAccessKey,
|
2022-08-18 08:52:09 +02:00
|
|
|
"",
|
|
|
|
)
|
2022-08-18 10:11:13 +02:00
|
|
|
} else if opts.IamRoleEndpoint != "" {
|
|
|
|
creds = credentials.NewIAM(opts.IamRoleEndpoint)
|
2022-08-18 08:52:09 +02:00
|
|
|
} else {
|
2022-08-18 10:11:13 +02:00
|
|
|
return nil, errors.New("NewStorageBackend: AWS_S3_BUCKET_NAME is defined, but no credentials were provided")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
options := minio.Options{
|
|
|
|
Creds: creds,
|
2022-08-18 10:11:13 +02:00
|
|
|
Secure: opts.EndpointProto == "https",
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
2022-08-18 10:11:13 +02:00
|
|
|
if opts.EndpointInsecure {
|
2022-08-18 08:52:09 +02:00
|
|
|
if !options.Secure {
|
2022-08-18 10:11:13 +02:00
|
|
|
return nil, errors.New("NewStorageBackend: AWS_ENDPOINT_INSECURE = true is only meaningful for https")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
transport, err := minio.DefaultTransport(true)
|
|
|
|
if err != nil {
|
2022-09-15 10:04:12 +02:00
|
|
|
return nil, fmt.Errorf("NewStorageBackend: failed to create default minio transport: %w", err)
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
transport.TLSClientConfig.InsecureSkipVerify = true
|
|
|
|
options.Transport = transport
|
|
|
|
}
|
|
|
|
|
2022-08-18 10:11:13 +02:00
|
|
|
mc, err := minio.New(opts.Endpoint, &options)
|
2022-08-18 08:52:09 +02:00
|
|
|
if err != nil {
|
2022-08-18 10:11:13 +02:00
|
|
|
return nil, fmt.Errorf("NewStorageBackend: error setting up minio client: %w", err)
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &s3Storage{
|
|
|
|
StorageBackend: &storage.StorageBackend{
|
2022-08-18 10:11:13 +02:00
|
|
|
DestinationPath: opts.RemotePath,
|
2022-08-18 08:52:09 +02:00
|
|
|
Log: logFunc,
|
|
|
|
},
|
|
|
|
client: mc,
|
2022-08-18 10:11:13 +02:00
|
|
|
bucket: opts.BucketName,
|
|
|
|
storageClass: opts.StorageClass,
|
2022-08-18 08:52:09 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the storage backend
|
|
|
|
func (v *s3Storage) Name() string {
|
|
|
|
return "S3"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy copies the given file to the S3/Minio storage backend.
|
|
|
|
func (b *s3Storage) Copy(file string) error {
|
|
|
|
_, name := path.Split(file)
|
|
|
|
|
|
|
|
if _, err := b.client.FPutObject(context.Background(), b.bucket, filepath.Join(b.DestinationPath, name), file, minio.PutObjectOptions{
|
|
|
|
ContentType: "application/tar+gzip",
|
|
|
|
StorageClass: b.storageClass,
|
|
|
|
}); err != nil {
|
2022-10-12 21:15:17 +02:00
|
|
|
if errResp := minio.ToErrorResponse(err); errResp.Message != "" {
|
|
|
|
return fmt.Errorf("(*s3Storage).Copy: error uploading backup to remote storage: [Message]: '%s', [Code]: %s, [StatusCode]: %d", errResp.Message, errResp.Code, errResp.StatusCode)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("(*s3Storage).Copy: error uploading backup to remote storage: %w", err)
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
2022-08-18 10:11:13 +02:00
|
|
|
b.Log(storage.LogLevelInfo, b.Name(), "Uploaded a copy of backup `%s` to bucket `%s`.", file, b.bucket)
|
2022-08-18 08:52:09 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prune rotates away backups according to the configuration and provided deadline for the S3/Minio storage backend.
|
|
|
|
func (b *s3Storage) Prune(deadline time.Time, pruningPrefix string) (*storage.PruneStats, error) {
|
|
|
|
candidates := b.client.ListObjects(context.Background(), b.bucket, minio.ListObjectsOptions{
|
|
|
|
WithMetadata: true,
|
|
|
|
Prefix: filepath.Join(b.DestinationPath, pruningPrefix),
|
|
|
|
Recursive: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
var matches []minio.ObjectInfo
|
|
|
|
var lenCandidates int
|
|
|
|
for candidate := range candidates {
|
|
|
|
lenCandidates++
|
|
|
|
if candidate.Err != nil {
|
2022-08-18 10:11:13 +02:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"(*s3Storage).Prune: Error looking up candidates from remote storage! %w",
|
2022-08-18 08:52:09 +02:00
|
|
|
candidate.Err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if candidate.LastModified.Before(deadline) {
|
|
|
|
matches = append(matches, candidate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stats := &storage.PruneStats{
|
|
|
|
Total: uint(lenCandidates),
|
|
|
|
Pruned: uint(len(matches)),
|
|
|
|
}
|
|
|
|
|
2022-08-18 10:11:13 +02:00
|
|
|
if err := b.DoPrune(b.Name(), len(matches), lenCandidates, "remote backup(s)", func() error {
|
2022-08-18 08:52:09 +02:00
|
|
|
objectsCh := make(chan minio.ObjectInfo)
|
|
|
|
go func() {
|
|
|
|
for _, match := range matches {
|
|
|
|
objectsCh <- match
|
|
|
|
}
|
|
|
|
close(objectsCh)
|
|
|
|
}()
|
|
|
|
errChan := b.client.RemoveObjects(context.Background(), b.bucket, objectsCh, minio.RemoveObjectsOptions{})
|
|
|
|
var removeErrors []error
|
|
|
|
for result := range errChan {
|
|
|
|
if result.Err != nil {
|
|
|
|
removeErrors = append(removeErrors, result.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(removeErrors) != 0 {
|
|
|
|
return utilities.Join(removeErrors...)
|
|
|
|
}
|
|
|
|
return nil
|
2022-08-18 10:11:13 +02:00
|
|
|
}); err != nil {
|
|
|
|
return stats, err
|
|
|
|
}
|
2022-08-18 08:52:09 +02:00
|
|
|
|
|
|
|
return stats, nil
|
|
|
|
}
|