feat: handle list for repeated files/versions

This commit is contained in:
Urko 2023-07-18 14:23:33 +02:00
parent 022d56dc8e
commit 4e4292a6b7
3 changed files with 48 additions and 29 deletions

View File

@ -1,6 +1,6 @@
BIN_DIR := ./bin BIN_DIR := ./bin
APP_NAME := backblaze-backup APP_NAME := backblaze-backup
MAIN := ./cmd/main.go MAIN := ./main.go
.PHONY: local_run .PHONY: local_run
local_run: local_run:

View File

@ -27,7 +27,7 @@ var Versions = &cobra.Command{
cfg := config.NewConfig(envFile) cfg := config.NewConfig(envFile)
bbService := services.NewBackBlaze(cfg.BbId, cfg.BbKey) bbService := services.NewBackBlaze(cfg.BbId, cfg.BbKey)
if err := bbService.ListDuplicateVersions(ctx); err != nil { if err := bbService.ListDuplicateVersions(ctx, cancel); err != nil {
log.Fatalln("bbService.ListDuplicateVersions()", err) log.Fatalln("bbService.ListDuplicateVersions()", err)
} }
}, },

View File

@ -308,7 +308,7 @@ type duplicate struct {
count int count int
} }
func (b *BackBalze) ListDuplicateVersions(ctx context.Context) error { func (b *BackBalze) ListDuplicateVersions(ctx context.Context, cancel context.CancelFunc) error {
b2Client, err := b2.NewClient(ctx, b.bbID, b.bbKey) b2Client, err := b2.NewClient(ctx, b.bbID, b.bbKey)
if err != nil { if err != nil {
return fmt.Errorf("b2.NewClient %w", err) return fmt.Errorf("b2.NewClient %w", err)
@ -320,21 +320,33 @@ func (b *BackBalze) ListDuplicateVersions(ctx context.Context) error {
return fmt.Errorf("b2Client.Bucket %w", err) return fmt.Errorf("b2Client.Bucket %w", err)
} }
wg := sync.WaitGroup{}
dups := make([]duplicate, 0) dups := make([]duplicate, 0)
log.Println("len(buckets)", len(buckets)) log.Println("len(buckets)", len(buckets))
sm := semaphore.NewWeighted(int64(b.maxWorkers))
wg.Add(len(buckets))
for _, bc := range buckets { for _, bc := range buckets {
if err := sm.Acquire(ctx, 1); err != nil {
return fmt.Errorf("sm.Acquire %w", err)
}
go func(bc *b2.Bucket) {
defer sm.Release(1)
defer wg.Done()
files := make(map[string]int, 0) files := make(map[string]int, 0)
bucketIter := bc.List(ctx, b2.ListHidden()) bucketIter := bc.List(ctx, b2.ListHidden())
if bucketIter == nil { if bucketIter == nil {
return fmt.Errorf("bucket list cannot be nil") log.Println("bucket list cannot be nil")
return
} }
for { for {
if !bucketIter.Next() { if !bucketIter.Next() {
if bucketIter.Err() != nil { if bucketIter.Err() != nil {
return fmt.Errorf("bucketIter err %w", bucketIter.Err()) log.Println("bucketIter err %w", bucketIter.Err())
return
} }
break break
} }
@ -355,9 +367,16 @@ func (b *BackBalze) ListDuplicateVersions(ctx context.Context) error {
}) })
} }
} }
}(bc)
} }
wg.Wait()
if len(dups) > 0 { if len(dups) > 0 {
return fmt.Errorf("found duplicates: %+v", dups) var builder strings.Builder
for _, dup := range dups {
builder.WriteString(fmt.Sprintf("%+v\n", dup))
}
return fmt.Errorf("found duplicates: %s", builder.String())
} }
return nil return nil
} }