2017-05-28 08:19:01 +00:00
|
|
|
package b2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-19 11:39:48 +00:00
|
|
|
"hash"
|
2017-05-28 08:19:01 +00:00
|
|
|
"io"
|
2017-09-24 18:04:23 +00:00
|
|
|
"net/http"
|
2017-05-28 08:19:01 +00:00
|
|
|
"path"
|
2022-08-28 13:00:30 +00:00
|
|
|
"sync"
|
2022-08-28 12:51:20 +00:00
|
|
|
"time"
|
2017-05-28 08:19:01 +00:00
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2022-10-15 14:23:39 +00:00
|
|
|
"github.com/restic/restic/internal/backend/layout"
|
2023-06-08 11:04:34 +00:00
|
|
|
"github.com/restic/restic/internal/backend/location"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-05-28 08:19:01 +00:00
|
|
|
|
|
|
|
"github.com/kurin/blazer/b2"
|
2022-10-21 20:48:17 +00:00
|
|
|
"github.com/kurin/blazer/base"
|
2017-05-28 08:19:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// b2Backend is a backend which stores its data on Backblaze B2.
|
|
|
|
type b2Backend struct {
|
2017-09-18 10:13:35 +00:00
|
|
|
client *b2.Client
|
|
|
|
bucket *b2.Bucket
|
|
|
|
cfg Config
|
|
|
|
listMaxItems int
|
2022-10-15 14:23:39 +00:00
|
|
|
layout.Layout
|
2022-10-21 20:48:17 +00:00
|
|
|
|
|
|
|
canDelete bool
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-08-21 09:20:03 +00:00
|
|
|
// Billing happens in 1000 item granlarity, but we are more interested in reducing the number of network round trips
|
|
|
|
const defaultListMaxItems = 10 * 1000
|
2017-09-18 10:13:35 +00:00
|
|
|
|
2017-06-03 15:39:57 +00:00
|
|
|
// ensure statically that *b2Backend implements restic.Backend.
|
|
|
|
var _ restic.Backend = &b2Backend{}
|
|
|
|
|
2023-06-08 11:04:34 +00:00
|
|
|
func NewFactory() location.Factory {
|
|
|
|
return location.NewHTTPBackendFactory(ParseConfig, location.NoPassword, Create, Open)
|
|
|
|
}
|
|
|
|
|
2022-08-28 13:00:30 +00:00
|
|
|
type sniffingRoundTripper struct {
|
|
|
|
sync.Mutex
|
|
|
|
lastErr error
|
|
|
|
http.RoundTripper
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sniffingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
res, err := s.RoundTripper.RoundTrip(req)
|
|
|
|
if err != nil {
|
|
|
|
s.Lock()
|
|
|
|
s.lastErr = err
|
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2017-09-24 18:04:23 +00:00
|
|
|
func newClient(ctx context.Context, cfg Config, rt http.RoundTripper) (*b2.Client, error) {
|
2023-06-08 13:28:07 +00:00
|
|
|
if cfg.AccountID == "" {
|
|
|
|
return nil, errors.Fatalf("unable to open B2 backend: Account ID ($B2_ACCOUNT_ID) is empty")
|
|
|
|
}
|
|
|
|
if cfg.Key.String() == "" {
|
|
|
|
return nil, errors.Fatalf("unable to open B2 backend: Key ($B2_ACCOUNT_KEY) is empty")
|
|
|
|
}
|
|
|
|
|
2022-08-28 13:00:30 +00:00
|
|
|
sniffer := &sniffingRoundTripper{RoundTripper: rt}
|
|
|
|
opts := []b2.ClientOption{b2.Transport(sniffer)}
|
2017-05-28 08:19:01 +00:00
|
|
|
|
2022-08-28 12:51:20 +00:00
|
|
|
// if the connection B2 fails, this can cause the client to hang
|
|
|
|
// cancel the connection after a minute to at least provide some feedback to the user
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Minute)
|
|
|
|
defer cancel()
|
2021-08-04 20:56:18 +00:00
|
|
|
c, err := b2.NewClient(ctx, cfg.AccountID, cfg.Key.Unwrap(), opts...)
|
2022-08-28 12:51:20 +00:00
|
|
|
if err == context.DeadlineExceeded {
|
2022-08-28 13:00:30 +00:00
|
|
|
if sniffer.lastErr != nil {
|
|
|
|
return nil, sniffer.lastErr
|
|
|
|
}
|
2022-08-28 12:51:20 +00:00
|
|
|
return nil, errors.New("connection to B2 failed")
|
|
|
|
} else if err != nil {
|
2017-05-28 08:19:01 +00:00
|
|
|
return nil, errors.Wrap(err, "b2.NewClient")
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens a connection to the B2 service.
|
2017-11-22 11:27:29 +00:00
|
|
|
func Open(ctx context.Context, cfg Config, rt http.RoundTripper) (restic.Backend, error) {
|
2017-05-28 08:19:01 +00:00
|
|
|
debug.Log("cfg %#v", cfg)
|
|
|
|
|
2017-11-22 11:27:29 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2017-05-28 08:19:01 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2017-09-24 18:04:23 +00:00
|
|
|
client, err := newClient(ctx, cfg, rt)
|
2017-05-28 08:19:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket, err := client.Bucket(ctx, cfg.Bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Bucket")
|
|
|
|
}
|
|
|
|
|
|
|
|
be := &b2Backend{
|
|
|
|
client: client,
|
|
|
|
bucket: bucket,
|
|
|
|
cfg: cfg,
|
2022-10-15 14:23:39 +00:00
|
|
|
Layout: &layout.DefaultLayout{
|
2017-05-28 08:19:01 +00:00
|
|
|
Join: path.Join,
|
|
|
|
Path: cfg.Prefix,
|
|
|
|
},
|
2017-10-26 18:22:16 +00:00
|
|
|
listMaxItems: defaultListMaxItems,
|
2022-10-21 20:48:17 +00:00
|
|
|
canDelete: true,
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return be, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create opens a connection to the B2 service. If the bucket does not exist yet,
|
|
|
|
// it is created.
|
2017-11-22 11:27:29 +00:00
|
|
|
func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (restic.Backend, error) {
|
2017-05-28 08:19:01 +00:00
|
|
|
debug.Log("cfg %#v", cfg)
|
|
|
|
|
2017-11-22 11:27:29 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2017-05-28 08:19:01 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2017-09-24 18:04:23 +00:00
|
|
|
client, err := newClient(ctx, cfg, rt)
|
2017-05-28 08:19:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
attr := b2.BucketAttrs{
|
|
|
|
Type: b2.Private,
|
|
|
|
}
|
|
|
|
bucket, err := client.NewBucket(ctx, cfg.Bucket, &attr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "NewBucket")
|
|
|
|
}
|
|
|
|
|
|
|
|
be := &b2Backend{
|
|
|
|
client: client,
|
|
|
|
bucket: bucket,
|
|
|
|
cfg: cfg,
|
2022-10-15 14:23:39 +00:00
|
|
|
Layout: &layout.DefaultLayout{
|
2017-05-28 08:19:01 +00:00
|
|
|
Join: path.Join,
|
|
|
|
Path: cfg.Prefix,
|
|
|
|
},
|
2017-10-26 18:22:16 +00:00
|
|
|
listMaxItems: defaultListMaxItems,
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 10:28:10 +00:00
|
|
|
_, err = be.Stat(ctx, restic.Handle{Type: restic.ConfigFile})
|
|
|
|
if err != nil && !be.IsNotExist(err) {
|
2017-05-28 08:19:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-03 10:28:10 +00:00
|
|
|
if err == nil {
|
2017-05-28 08:19:01 +00:00
|
|
|
return nil, errors.New("config already exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
return be, nil
|
|
|
|
}
|
|
|
|
|
2017-09-18 10:13:35 +00:00
|
|
|
// SetListMaxItems sets the number of list items to load per request.
|
|
|
|
func (be *b2Backend) SetListMaxItems(i int) {
|
|
|
|
be.listMaxItems = i
|
|
|
|
}
|
|
|
|
|
2021-08-07 20:20:49 +00:00
|
|
|
func (be *b2Backend) Connections() uint {
|
|
|
|
return be.cfg.Connections
|
|
|
|
}
|
|
|
|
|
2017-05-28 08:19:01 +00:00
|
|
|
// Location returns the location for the backend.
|
|
|
|
func (be *b2Backend) Location() string {
|
|
|
|
return be.cfg.Bucket
|
|
|
|
}
|
|
|
|
|
2020-12-19 11:39:48 +00:00
|
|
|
// Hasher may return a hash function for calculating a content hash for the backend
|
|
|
|
func (be *b2Backend) Hasher() hash.Hash {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-01 18:07:29 +00:00
|
|
|
// HasAtomicReplace returns whether Save() can atomically replace files
|
|
|
|
func (be *b2Backend) HasAtomicReplace() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:27 +00:00
|
|
|
// IsNotExist returns true if the error is caused by a non-existing file.
|
|
|
|
func (be *b2Backend) IsNotExist(err error) bool {
|
2022-10-08 10:37:18 +00:00
|
|
|
// blazer/b2 does not export its error types and values,
|
|
|
|
// so we can't use errors.{As,Is}.
|
|
|
|
for ; err != nil; err = errors.Unwrap(err) {
|
|
|
|
if b2.IsNotExist(err) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2017-06-15 11:40:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 04:59:16 +00:00
|
|
|
// Load runs fn with a reader that yields the contents of the file at h at the
|
|
|
|
// given offset.
|
|
|
|
func (be *b2Backend) Load(ctx context.Context, h restic.Handle, length int, offset int64, fn func(rd io.Reader) error) error {
|
2023-04-07 21:02:35 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2018-01-17 04:59:16 +00:00
|
|
|
return backend.DefaultLoad(ctx, h, length, offset, be.openReader, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (be *b2Backend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
|
2017-05-28 08:19:01 +00:00
|
|
|
name := be.Layout.Filename(h)
|
|
|
|
obj := be.bucket.Object(name)
|
|
|
|
|
|
|
|
if offset == 0 && length == 0 {
|
2023-04-07 21:02:35 +00:00
|
|
|
return obj.NewReader(ctx), nil
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pass a negative length to NewRangeReader so that the remainder of the
|
|
|
|
// file is read.
|
|
|
|
if length == 0 {
|
|
|
|
length = -1
|
|
|
|
}
|
|
|
|
|
2023-04-07 21:02:35 +00:00
|
|
|
return obj.NewRangeReader(ctx, offset, int64(length)), nil
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save stores data in the backend at the handle.
|
2018-03-03 13:20:54 +00:00
|
|
|
func (be *b2Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
|
2017-06-03 15:39:57 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2017-05-28 08:19:01 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
name := be.Filename(h)
|
|
|
|
obj := be.bucket.Object(name)
|
|
|
|
|
2020-12-19 11:29:02 +00:00
|
|
|
// b2 always requires sha1 checksums for uploaded file parts
|
2017-05-28 08:19:01 +00:00
|
|
|
w := obj.NewWriter(ctx)
|
|
|
|
n, err := io.Copy(w, rd)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_ = w.Close()
|
|
|
|
return errors.Wrap(err, "Copy")
|
|
|
|
}
|
|
|
|
|
2020-12-18 22:41:29 +00:00
|
|
|
// sanity check
|
|
|
|
if n != rd.Length() {
|
|
|
|
return errors.Errorf("wrote %d bytes instead of the expected %d bytes", n, rd.Length())
|
|
|
|
}
|
2017-05-28 08:19:01 +00:00
|
|
|
return errors.Wrap(w.Close(), "Close")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns information about a blob.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (be *b2Backend) Stat(ctx context.Context, h restic.Handle) (bi restic.FileInfo, err error) {
|
2017-05-28 08:19:01 +00:00
|
|
|
name := be.Filename(h)
|
|
|
|
obj := be.bucket.Object(name)
|
|
|
|
info, err := obj.Attrs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return restic.FileInfo{}, errors.Wrap(err, "Stat")
|
|
|
|
}
|
2018-01-20 18:34:38 +00:00
|
|
|
return restic.FileInfo{Size: info.Size, Name: h.Name}, nil
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the blob with the given name and type.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (be *b2Backend) Remove(ctx context.Context, h restic.Handle) error {
|
2022-08-21 09:11:00 +00:00
|
|
|
// the retry backend will also repeat the remove method up to 10 times
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
obj := be.bucket.Object(be.Filename(h))
|
2022-10-21 20:48:17 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if be.canDelete {
|
|
|
|
err = obj.Delete(ctx)
|
|
|
|
if err == nil {
|
|
|
|
// keep deleting until we are sure that no leftover file versions exist
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
code, _ := base.Code(err)
|
|
|
|
if code == 401 { // unauthorized
|
|
|
|
// fallback to hide if not allowed to delete files
|
|
|
|
be.canDelete = false
|
|
|
|
debug.Log("Removing %v failed, falling back to b2_hide_file.", h)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// hide adds a new file version hiding all older ones, thus retries are not necessary
|
|
|
|
err = obj.Hide(ctx)
|
2022-08-21 09:11:00 +00:00
|
|
|
}
|
2022-10-21 20:48:17 +00:00
|
|
|
|
2022-08-21 09:11:00 +00:00
|
|
|
// consider a file as removed if b2 informs us that it does not exist
|
|
|
|
if b2.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.Wrap(err, "Delete")
|
2021-10-09 20:57:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-21 09:11:00 +00:00
|
|
|
return errors.New("failed to delete all file versions")
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 21:53:49 +00:00
|
|
|
// List returns a channel that yields all names of blobs of type t.
|
2018-01-20 18:34:38 +00:00
|
|
|
func (be *b2Backend) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
|
2017-06-03 15:39:57 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2018-01-20 18:34:38 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2018-05-25 21:53:49 +00:00
|
|
|
prefix, _ := be.Basedir(t)
|
2023-04-07 21:02:35 +00:00
|
|
|
iter := be.bucket.List(ctx, b2.ListPrefix(prefix), b2.ListPageSize(be.listMaxItems))
|
2018-01-20 18:34:38 +00:00
|
|
|
|
2018-05-25 21:53:49 +00:00
|
|
|
for iter.Next() {
|
|
|
|
obj := iter.Object()
|
|
|
|
|
|
|
|
attrs, err := obj.Attrs(ctx)
|
|
|
|
if err != nil {
|
2018-01-20 18:34:38 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-05-28 08:19:01 +00:00
|
|
|
|
2018-05-25 21:53:49 +00:00
|
|
|
fi := restic.FileInfo{
|
|
|
|
Name: path.Base(obj.Name()),
|
|
|
|
Size: attrs.Size,
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 21:53:49 +00:00
|
|
|
if err := fn(fi); err != nil {
|
|
|
|
return err
|
2018-01-20 18:34:38 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-18 16:07:19 +00:00
|
|
|
return iter.Err()
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes all restic keys in the bucket. It will not remove the bucket itself.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (be *b2Backend) Delete(ctx context.Context) error {
|
2023-04-07 21:56:16 +00:00
|
|
|
return backend.DefaultDelete(ctx, be)
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close does nothing
|
|
|
|
func (be *b2Backend) Close() error { return nil }
|