fix: local count
This commit is contained in:
parent
bec9807a75
commit
083762e1c5
12
cmd/check.go
12
cmd/check.go
|
@ -72,16 +72,17 @@ var Check = &cobra.Command{
|
||||||
|
|
||||||
logger.Info("start check")
|
logger.Info("start check")
|
||||||
defer logger.Info("end check")
|
defer logger.Info("end check")
|
||||||
|
|
||||||
localChan := make(chan backblaze.B2Local)
|
localChan := make(chan backblaze.B2Local)
|
||||||
b2Chan := make(chan backblaze.B2Local)
|
b2Chan := make(chan backblaze.B2Local)
|
||||||
doneChan := make(chan interface{}, 1)
|
countChan := make(chan int, 1)
|
||||||
go b.CompareConcurrent(ctx, backupDir, bucketName, localChan, b2Chan, doneChan)
|
go b.CompareConcurrent(ctx, backupDir, bucketName, localChan, b2Chan, countChan)
|
||||||
|
|
||||||
reportBuilder := strings.Builder{}
|
reportBuilder := strings.Builder{}
|
||||||
|
reportBuilder.WriteString(fmt.Sprintf("Local files within `%s` path already in `%s` bucket:\n", backupDir, bucketName))
|
||||||
|
|
||||||
countLocal := 0
|
countLocal := 0
|
||||||
countB2 := 0
|
countB2 := 0
|
||||||
countOK := 0
|
|
||||||
reportBuilder.WriteString(fmt.Sprintf("Local files within `%s` path already in `%s` bucket:\n", backupDir, bucketName))
|
|
||||||
|
|
||||||
cloudBuilder := strings.Builder{}
|
cloudBuilder := strings.Builder{}
|
||||||
cloudBuilder.WriteString(fmt.Sprintf("B2 files within `%s` bucket\n", bucketName))
|
cloudBuilder.WriteString(fmt.Sprintf("B2 files within `%s` bucket\n", bucketName))
|
||||||
|
@ -95,6 +96,7 @@ var Check = &cobra.Command{
|
||||||
localBuilder.WriteString("|----------------------------------------------------------|------------------|--------------------------------|------------------------------------------\n")
|
localBuilder.WriteString("|----------------------------------------------------------|------------------|--------------------------------|------------------------------------------\n")
|
||||||
countNotInCloud := 0
|
countNotInCloud := 0
|
||||||
|
|
||||||
|
countOK := 0
|
||||||
loop:
|
loop:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
@ -106,7 +108,7 @@ var Check = &cobra.Command{
|
||||||
logger.Error("Operation canceled")
|
logger.Error("Operation canceled")
|
||||||
}
|
}
|
||||||
break loop
|
break loop
|
||||||
case <-doneChan:
|
case countOK = <-countChan:
|
||||||
logger.Debugln("done chan")
|
logger.Debugln("done chan")
|
||||||
break loop
|
break loop
|
||||||
case localMsg, ok := <-localChan:
|
case localMsg, ok := <-localChan:
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kurin/blazer/b2"
|
"github.com/kurin/blazer/b2"
|
||||||
|
@ -93,7 +94,12 @@ type B2Local struct {
|
||||||
// CompareConcurrent concurrently fetches the list of local files and cloud files,
|
// CompareConcurrent concurrently fetches the list of local files and cloud files,
|
||||||
// then compares them to ensure all local files exist in the cloud.
|
// then compares them to ensure all local files exist in the cloud.
|
||||||
// Errors are sent to a provided error channel. The function will panic if an error occurs while listing files.
|
// Errors are sent to a provided error channel. The function will panic if an error occurs while listing files.
|
||||||
func (b *BackBlaze) CompareConcurrent(ctx context.Context, backupDir, bucketName string, localChan chan<- B2Local, b2Chan chan<- B2Local, doneChan chan<- interface{}) {
|
func (b *BackBlaze) CompareConcurrent(
|
||||||
|
ctx context.Context,
|
||||||
|
backupDir, bucketName string,
|
||||||
|
localChan, b2Chan chan<- B2Local,
|
||||||
|
doneChan chan<- int,
|
||||||
|
) {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
localFiles := make(map[string]File)
|
localFiles := make(map[string]File)
|
||||||
cloudFiles := make(map[string]File)
|
cloudFiles := make(map[string]File)
|
||||||
|
@ -145,6 +151,7 @@ func (b *BackBlaze) CompareConcurrent(ctx context.Context, backupDir, bucketName
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
// Now check local files that are not present in cloud
|
// Now check local files that are not present in cloud
|
||||||
|
var count atomic.Int64
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
@ -155,6 +162,7 @@ func (b *BackBlaze) CompareConcurrent(ctx context.Context, backupDir, bucketName
|
||||||
}
|
}
|
||||||
b.logger.Debugf("localFile %+v\n", localFile)
|
b.logger.Debugf("localFile %+v\n", localFile)
|
||||||
localChan <- B2Local{File: localFile, Err: nil}
|
localChan <- B2Local{File: localFile, Err: nil}
|
||||||
|
count.Add(1)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -172,5 +180,5 @@ func (b *BackBlaze) CompareConcurrent(ctx context.Context, backupDir, bucketName
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
close(localChan)
|
close(localChan)
|
||||||
close(b2Chan)
|
close(b2Chan)
|
||||||
doneChan <- nil
|
doneChan <- int(count.Load())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue