2023-08-27 21:30:19 +02:00
|
|
|
package backblaze
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
2023-08-29 11:40:07 +02:00
|
|
|
"path"
|
2023-08-27 21:30:19 +02:00
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
2023-09-13 21:18:57 +02:00
|
|
|
"sync/atomic"
|
2023-09-13 08:35:38 +02:00
|
|
|
"time"
|
2023-08-27 21:30:19 +02:00
|
|
|
|
|
|
|
"github.com/kurin/blazer/b2"
|
|
|
|
)
|
|
|
|
|
2023-09-13 08:35:38 +02:00
|
|
|
type File struct {
|
|
|
|
Path string
|
|
|
|
Size int // file size in bytes
|
|
|
|
ModTime time.Time
|
|
|
|
IsUploaded *bool
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:30:19 +02:00
|
|
|
// localFiles lists the local files in the given backup directory and sends them to a channel.
|
|
|
|
// It closes the channel after all files are listed.
|
2023-09-13 08:35:38 +02:00
|
|
|
func (b *BackBlaze) localFiles(backupDir string, fileChan chan<- File) error {
|
2023-08-27 21:30:19 +02:00
|
|
|
defer close(fileChan)
|
|
|
|
// Walk the directory and send files to the channel
|
|
|
|
err := filepath.WalkDir(backupDir, func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !d.IsDir() {
|
2023-09-13 08:35:38 +02:00
|
|
|
i, err := d.Info()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("d.Info: %w", err)
|
|
|
|
}
|
|
|
|
fileChan <- File{Path: filepath.Base(path), Size: int(i.Size()), ModTime: i.ModTime()}
|
2023-08-27 21:30:19 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-09-13 08:35:38 +02:00
|
|
|
return fmt.Errorf("error walking the directory: %w", err)
|
2023-08-27 21:30:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// b2BucketFiles lists the files in the given B2 bucket and sends them to a channel.
|
|
|
|
// It closes the channel after all files are listed.
|
2023-09-13 08:35:38 +02:00
|
|
|
func (b *BackBlaze) b2BucketFiles(ctx context.Context, bucketName string, fileChan chan<- File) error {
|
2023-08-27 21:30:19 +02:00
|
|
|
bucket, err := b.b2Client.Bucket(ctx, bucketName)
|
|
|
|
defer close(fileChan)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("b2Client.Bucket %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketIter := bucket.List(ctx, b2.ListHidden())
|
|
|
|
if bucketIter == nil {
|
|
|
|
return errors.New("bucket list cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if !bucketIter.Next() {
|
|
|
|
if bucketIter.Err() != nil {
|
|
|
|
return fmt.Errorf("bucketIter err %w", bucketIter.Err())
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if bucketIter.Object() == nil {
|
|
|
|
return errors.New("bucketIter Object is nil")
|
|
|
|
}
|
2023-08-29 11:40:07 +02:00
|
|
|
|
2023-09-13 08:35:38 +02:00
|
|
|
fileName := path.Base(bucketIter.Object().Name())
|
|
|
|
attrs, err := bucketIter.Object().Attrs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("bucketIter.Object().Attrs %s err %w", fileName, bucketIter.Err())
|
|
|
|
}
|
|
|
|
isUploaded := attrs.Status != b2.Uploaded
|
|
|
|
fileChan <- File{Path: path.Base(fileName), Size: int(attrs.Size), IsUploaded: &isUploaded, ModTime: attrs.UploadTimestamp}
|
2023-08-27 21:30:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrLocalNotInCloud error = errors.New("exists locally but not in the cloud")
|
2023-09-13 08:35:38 +02:00
|
|
|
var ErrCloudNotInLocal error = errors.New("exists on B2 but not locally")
|
2023-08-27 21:30:19 +02:00
|
|
|
|
2023-08-28 11:01:57 +02:00
|
|
|
type B2Local struct {
|
2023-09-13 08:35:38 +02:00
|
|
|
File File
|
|
|
|
Err error
|
2023-08-27 21:30:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CompareConcurrent concurrently fetches the list of local files and cloud files,
|
|
|
|
// 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.
|
2023-09-13 21:18:57 +02:00
|
|
|
func (b *BackBlaze) CompareConcurrent(
|
|
|
|
ctx context.Context,
|
|
|
|
backupDir, bucketName string,
|
|
|
|
localChan, b2Chan chan<- B2Local,
|
|
|
|
doneChan chan<- int,
|
|
|
|
) {
|
2023-08-27 21:30:19 +02:00
|
|
|
var wg sync.WaitGroup
|
2023-09-13 08:35:38 +02:00
|
|
|
localFiles := make(map[string]File)
|
|
|
|
cloudFiles := make(map[string]File)
|
|
|
|
localFileChan := make(chan File)
|
|
|
|
b2FileChan := make(chan File)
|
2023-08-27 21:30:19 +02:00
|
|
|
|
|
|
|
// Local listing
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for f := range localFileChan {
|
2023-09-13 08:35:38 +02:00
|
|
|
if _, ok := localFiles[f.Path]; ok {
|
|
|
|
panic(fmt.Errorf("local file already exists in map: %s", f.Path))
|
2023-08-27 21:30:19 +02:00
|
|
|
}
|
2023-09-13 08:35:38 +02:00
|
|
|
b.logger.Debugf("local file %+v\n", f)
|
|
|
|
localFiles[f.Path] = f
|
2023-08-27 21:30:19 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := b.localFiles(backupDir, localFileChan); err != nil {
|
|
|
|
panic(fmt.Errorf("b.LocalFilesWithB2: %w", err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Cloud listing
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for f := range b2FileChan {
|
2023-09-13 08:35:38 +02:00
|
|
|
if _, ok := cloudFiles[f.Path]; ok {
|
2023-10-01 14:35:50 +02:00
|
|
|
panic(fmt.Errorf(`cloud file already exists in map: %s\n you should run 'backblazebackup cleanup --bucket "%s"'`, f.Path, b.options.Bucket))
|
2023-08-27 21:30:19 +02:00
|
|
|
}
|
2023-09-13 08:35:38 +02:00
|
|
|
b.logger.Debugf("B2 file %+v\n", f)
|
|
|
|
cloudFiles[f.Path] = f
|
2023-08-27 21:30:19 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err := b.b2BucketFiles(ctx, bucketName, b2FileChan); err != nil {
|
|
|
|
panic(fmt.Errorf("b.LocalFilesWithB2: %w", err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for both to complete
|
|
|
|
wg.Wait()
|
|
|
|
|
2023-08-28 11:01:57 +02:00
|
|
|
// Now check local files that are not present in cloud
|
2023-09-13 21:18:57 +02:00
|
|
|
var count atomic.Int64
|
2023-08-27 21:30:19 +02:00
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2023-09-13 08:35:38 +02:00
|
|
|
for path, localFile := range localFiles {
|
|
|
|
if _, exists := cloudFiles[path]; !exists {
|
|
|
|
localChan <- B2Local{File: localFile, Err: ErrLocalNotInCloud}
|
2023-08-28 11:01:57 +02:00
|
|
|
continue
|
2023-08-27 21:30:19 +02:00
|
|
|
}
|
2023-09-13 08:35:38 +02:00
|
|
|
b.logger.Debugf("localFile %+v\n", localFile)
|
|
|
|
localChan <- B2Local{File: localFile, Err: nil}
|
2023-09-13 21:18:57 +02:00
|
|
|
count.Add(1)
|
2023-08-27 21:30:19 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Now check cloud files that are not in local
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2023-09-13 08:35:38 +02:00
|
|
|
for path, cloudFile := range cloudFiles {
|
|
|
|
if _, exists := localFiles[path]; !exists {
|
|
|
|
b2Chan <- B2Local{File: cloudFile, Err: ErrCloudNotInLocal}
|
|
|
|
continue
|
2023-08-27 21:30:19 +02:00
|
|
|
}
|
2023-09-13 08:35:38 +02:00
|
|
|
b2Chan <- B2Local{File: cloudFile, Err: nil}
|
2023-08-27 21:30:19 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
2023-09-13 08:35:38 +02:00
|
|
|
close(localChan)
|
|
|
|
close(b2Chan)
|
2023-09-13 21:18:57 +02:00
|
|
|
doneChan <- int(count.Load())
|
2023-08-27 21:30:19 +02:00
|
|
|
}
|